ぼんさい塾

ぼんさいノートと補遺に関する素材や注釈です.ミスが多いので初稿から1週間を経た重要な修正のみ最終更新日を残しています.

広告

※このエリアは、60日間投稿が無い場合に表示されます。記事を投稿すると、表示されなくなります。

例で考えるJavaプログラミング (5)

2012-10-27 00:45:51 | 暮らし
progJ.pdf

記事一覧

      Runnable インターフェイスの実装

progJ.pdf に [#40] ~ [#44] を追加しました.行数が予定を大幅に超過したのでサンプルプログラムを変更するかも知れません.
※ NetBeans の IDE では補足(3)の "in.txt" はプロジェクトのフォルダの直下(src や build の外)に置いてください.
※ P43.java は不良品です---とりあえず応急処置をしました(11/06 追記).

補足:(1) P40.java
import myLib.*;
public class P40 {
/* コメントにしなくてもエラーにならない.
  static class Now{
    int n;
    Now(int k){n = k;}
    void start( ){
      System.out.println("n = " + n);
    }
  }
コメントにしないとこちらが勝つ */
  public static void main(String[ ] args) {
    Now n = new Now(4); n.start( );
  }
}
//Now.java
package myLib;
import java.util.Date;
public class Now extends Thread{
  public int cnt;
  public Now(int k){cnt = k;}
  @Override
  public void run( ){
    Date d;
    while(cnt > 0){
      d = new Date( );
      System.out.println(cnt + ": " + d); cnt--;
      try{
        sleep(1000 * cnt);
      }catch(InterruptedException e){ }
    }
  }
}
(2) P43.java
import myLib.*;
import java.io.*;
class QueR extends Que implements Runnable {
  QueR( ){super( );}
  public void run( ){
    BufferedReader r; String s; char c;
    r = new BufferedReader(new InputStreamReader(System.in));
    try{
      s = r.readLine( ); c = s.charAt(0);
      while(c != 'e'){
        if(c == 'i'){insert(s.substring(2));}
        else if(c == 'd'){delete( );}
        else if(c == 's'){show( );}
        //System.out.println("begin-sleep");
        Thread.yield();
        //Thread.sleep(1000);//無効
        //System.out.println("end-sleep");
        s = r.readLine(); c = s.charAt(0);
      }
    }catch(IOException e){ }
    //catch(InterruptedException e){ }
  }
}
public class P43{
  public static void main(String[] args) {
    Now n1 = new Now(4); Now n2 = new Now(4);
    n1.setPriority(Thread.MIN_PRIORITY);
    n2.setPriority(Thread.MAX_PRIORITY);
    QueR q1 = new QueR( ); QueR q2 = new QueR( );
    n1.start( );
    q1.run( ); q2.run( ); q1.show( ); q2.show( );
    n2.start( );
  }
}
//List.java
package myLib;
class Cell{
  String d; Cell n;
  Cell(String s){d = s; n = null;}
}
public class List{
  public Cell top;
  public int sz;
  public List(){top = new Cell(" "); sz = 0;}
  public void insert(String s, int k){
    if(ksz+1){return;}
    Cell c1 = top, c2 = new Cell(s);
    while(k > 1){c1 = c1.n; k--;}
    if(c1.n != null){c2.n = c1.n;}
    c1.n = c2; sz++;
  }
  public void delete(int k){
    if(ksz){return;}
    Cell c1 = top;
    while(k > 1){c1 = c1.n; k--;}
    c1.n = c1.n.n; sz--;
  }
  public String show(int k){
    if(ksz){return "?";}
    Cell c1 = top.n; int i = k;
    while(k > 1){c1 = c1.n; k--;}
    System.out.println(i + ": " + c1.d);
    return c1.d;
  }
  public void show(){
    Cell c1 = top.n; int i;
    if(sz < 1){System.out.print("empty");}
    for(i = 1; i       System.out.print(c1.d + " ");
      if(i < sz ){c1 = c1.n;}
    }
    System.out.println();
  }
}
//Que.java
package myLib;
public class Que extends List{
  Cell last;
  public Que( ){ super( ); last = top; }
  public void insert(String s){
    last.n = new Cell(s);
    last = last.n; sz++;
  }
  public String delete( ){
    Cell c = top.n;
    delete(1); if(sz == 0){last = top;}
    return c.d;
  }
}
(3) P45.java
import java.io.*;
class Check{
  StringBuffer b;
  Check( ){b = new StringBuffer(256);}
  void rFile( ){
    FileReader r; int c;
    try{
      r = new FileReader("in.txt");
      while((c = r.read( )) != -1){
        b.append((char) c);
      }
      r.close( ); System.out.println("OK?");
    }catch(IOException e){
      b.append("not found");
    }
  }
  void wFile(){
    FileWriter w; String s;
    try{
      b.append("Hello\nworld.");
      w = new FileWriter("out.txt");
      for(int k = 0; k < b.length( ); k++){
        w.write(b.charAt(k));
      }
      w.close( ); System.out.println("OK?");
    }catch(IOException e){b.append("skipped");}
  }
}
public class P45 {
  public static void main(String[] args){
    Check x = new Check( ); x.rFile( );
    System.out.println(x.b);
    x = new Check( ); x.wFile( );
    System.out.println(x.b);
//
    StringBuffer sb = new StringBuffer(10);
    sb.append(123.67); sb.append((char) 65); sb.append(503);
    System.out.println(sb);
  }
}

コメント

例で考えるJavaプログラミング (4)

2012-10-20 22:48:20 | 暮らし
progJ.pdf

記事一覧

          List のサブクラス Que のメソッド

progJ.pdf に [#30] ~ [#37] を追加しました.

補足:(1) P34.java
public class P34{
  public static void main(String[] args) {
    String s = "-123"; int k;
    k = Integer.parseInt(s);
    s = Double.toString(k + 4.5);
    System.out.println(k + ", " + s);
    //
    //k = Integer.parseInt(" " + s);
    //k = Integer.parseInt(s + ".6");
    System.out.println(s + ", " + k);
    //
    Object obj = k;
    System.out.println(s=Integer.toHexString(k));
    Integer i = new Integer(k);
    System.out.println(i.intValue());
    //System.out.println(k.intValue());
  }
}
(2) P35.java
import java.util.*;
public class P35{
  public static void main(String[ ] args) {
    String s = "123 + 4 + 56 + =";
    //
    Que q = new Que( );
    StringTokenizer st = new StringTokenizer(s);
    while(st.hasMoreTokens( )){
      q.insert(st.nextToken( ));
    }
    //
    int x = 0, y = 0; String sq = q.delete( );
    while(sq.charAt(0) != '='){
      if(sq.charAt(0) == '+'){y += x;}
      else{x = Integer.parseInt(sq);}
      sq = q.delete( );
    }
    System.out.println(y);
  }
}
//List.java (デフォルトパッケージ,別ファイル可)
class Cell{
  String d; Cell n;
  Cell(String s){d = s; n = null;}
}
class List{
  Cell top; int sz;
  List(){top = new Cell(" "); sz = 0;}
  void insert(String s, int k){
    if(ksz+1){return;}
    Cell c1 = top, c2 = new Cell(s);
    while(k > 1){c1 = c1.n; k--;}
    if(c1.n != null){c2.n = c1.n;}
    c1.n = c2; sz++;
  }
  void delete(int k){
    if(ksz){return;}
    Cell c1 = top;
    while(k > 1){c1 = c1.n; k--;}
    c1.n = c1.n.n; sz--;
  }
  String show(int k){
    if(ksz){return "?";}
    Cell c1 = top.n; int i = k;
    while(k > 1){c1 = c1.n; k--;}
    System.out.println(i + ": " + c1.d);
    return c1.d;
  }
  void show(){
    Cell c1 = top.n; int i;
    if(sz < 1){System.out.print("empty");}
    for(i = 1; i < sz + 1; i++){     
      System.out.print(c1.d + " ");
      if(i < sz ){c1 = c1.n;}
    }
    System.out.println();
  }
}
class Que extends List{
  Cell last;
  Que( ){ super( ); last = top; }
  void insert(String s){
    last.n = new Cell(s);
    last = last.n; sz++;
  }
  String delete( ){
    Cell c = top.n;
    delete(1); if(sz == 0){last = top;}
    return c.d;
  }
}
(3) P36.java
class Node{
  String d; int ix; Node ln, rn;
  Node(String s, int k){
     d = s; ix = k; ln = rn = null;
  }
}
/* abstract */ class Tree{
  Node root;
  Tree( ){root = new Node("", 0);}
  //abstract void insert(String s, int k);
  //abstract void delete( );
  void show(String s, Node n){
    if(n == null) return;
    show(s+"L", n.ln);
    System.out.println(s + ": " + n.ix + " " + n.d);
    show(s+"R", n.rn);
  }
}
public class P36{
  public static void main(String[ ] args) {
    Tree t = new Tree(); Node rootR = t.root.rn;
    Node tmp; String s = "a";
    tmp = new Node(s, 7); rootR = tmp;
    tmp = new Node(s, 5); rootR.ln = tmp;
    tmp = new Node(s, 9); rootR.rn = tmp;
    tmp = new Node(s, 2); rootR.ln.ln = tmp;
    t.show("", rootR);
  }
}

コメント

例で考えるJavaプログラミング (3)

2012-10-15 20:13:42 | 暮らし
progJ.pdf

記事一覧
     
       メソッドの擬似コード (stepwise refinement

progJ.pdf に [#25] ~ [#27] を追加しました.
※ P22と同様の理由でP26も変更.(10/23)

補足:(1) P26.java
class Cell{
  String d; Cell n;
  Cell(String s){d = s; n = null;}
}
class List{
  Cell top; int sz;
  List(){top = new Cell(" "); sz = 0;}
  void insert(String s, int k){
    if(ksz+1){return;}
    Cell c1 = top, c2 = new Cell(s);
    while(k > 1){c1 = c1.n; k--;}
    if(c1.n != null) c2.n = c1.n;
    c1.n = c2; sz++;
  }
  void delete(int k){
    if(ksz){return;}
    Cell c1 = top;
    while(k > 1){c1 = c1.n; k--;}
    c1.n = c1.n.n; sz--;
  }
  String show(int k){
    if(ksz){return "?";}
    Cell c1 = top.n; int i = k;
    while(k > 1){c1 = c1.n; k--;}
    System.out.println(i + ": " + c1.d);
    return c1.d;
  }
  void show(){
    Cell c1 = top.n; int i;
    if(sz < 1){System.out.print("empty");}
    for(i = 1; i       System.out.print(c1.d + " ");
      if(i < sz ){c1 = c1.n;}
    }
    System.out.println();
  }
}
public class P26{
  public static void main(String[] args) {
    List x = new List();
    x.insert("k", 1); x.insert("t", 2);
    x.insert("h", 3); x.insert("m", 4); x.show();
    x.insert("s", 2); x.show();
    x.delete(4); x.show();
    //
    System.out.println();
    String s;
    s = x.show(0) + x.show(1) + x.show(x.sz) + x.show(x.sz+1);
    System.out.println(s);
    x.insert("xy", 1); x.insert("end", x.sz+1); x.show();
    x.delete(1); x.delete(x.sz); x.show();
    while(x.sz > 0){x.delete(x.sz);} x.show();
  }
}

コメント

Javaプログラミング (2)

2012-10-13 15:28:26 | 暮らし
progJ.pdf

記事一覧
 
                   複素数の計算

progJ.pdf に [#20] ~ [#24] を追加しました.行数に余裕が無く四苦八苦.
※ 構造体の拡張ということでComplexも主クラス内に置いていましたが多くの参考書との対応が悪いので
外に出しました --- 最初の予定は#2:主クラス内>#3:パッケージ内>#4:別パッケージ.(10/23)

補足:(1) P22.java
class Complex{
  double r,i;
  Complex(double x, double y){r = x; i = y;}
  Complex(double x){r = x; i = 0;}
  void add(double x, double y){
    r += x; i += y;
  }
  void mult(Complex z){
    double tmp;
    tmp = r * z.r - i * z.i;
    i = r * z.i + i * z.r; r = tmp;
  }
  double abs(){
    return Math.sqrt(r * r + i * i );
  }
}
public class P22{
  public static void main(String[] args) {
    Complex z = new Complex(2, -3);
    z.mult(new Complex(1, 1));
    System.out.println("|z|=" + z.abs());
    //
    Complex w;
    w = z; z = new Complex(-4, 7);
    System.out.println(z.r + " " + z.i);
    System.out.println(w.r + " " + w.i);
    //w.r == 5.0, w.i == -1.0 コピーコンストラクタ不要?
  }
}
(2) P24.java
public class P24{
  public static void main(String[] args) {
    int i; long k; double x = -3.4;
    k = Math.round(Math.abs(x));
    i = (int) k; x = Math.abs(-5);
    System.out.println(i + ", " + x);
    //
    float y = (float) -3.4;
    i = Math.round(Math.abs(y)); k = Math.round(Math.abs(y));
    System.out.println(i + ", " + k);
    i = (int) Math.abs(y); k = (long) Math.abs(y-0.2);
    System.out.println(i + ", " + k);
    //i = Math.round(Math.abs(x));
    //k = 123; i = k;
    i = 123 * 1000 * 1000 * 1000;
    System.out.println("i=" + i);
  }
}

コメント

Javaプログラミング (1)

2012-10-06 22:53:01 | 暮らし

progJ.pdf

記事一覧


                擬似コード

ようやく progJ.pdf 「1.簡単な例」の初稿ができました.

補足:(1) P11.java
public class P11{
  public static void main(String[ ] args){
    int i=2, k=3;
    System.out.println("i+k="+(i+k));
    //
    int[] m1, n1={0,1,2}; int[][] m2, n2={new int[2], new int[3]};
    m1 = new int[3]; n2[1][2] = 4;
    m2 = new int[2][]; m2[1] = new int[3];
    String s = "abc"; System.out.println(n2[1][2]+","+s);
  }
}
(2) P15.java
public class P15{
  static void add(int[] p, int k){p[0] += k;}
  public static void main(String[ ] args){
    int[] n={2,3};
    add(n, 4); System.out.println(n[0]);
  }
}
(3) P17.java
import java.io.*;
public class P17{
  public static void main(String[ ] args) throws IOException{
    BufferedReader r; String s; StringBuffer b = null;
    try{
      r = new BufferedReader(new InputStreamReader(System.in));
      s = r.readLine(); b = new StringBuffer(s);
    }catch(IOException e){System.out.println(e);}
    System.out.println("b="+b);
  }
}
 

コメント