ぼんさい塾

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

広告

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

NetBeansによる演習 (4)

2012-11-27 11:39:57 | 暮らし

progJ.pdf
progJ-s.pdf
progJ-e.pdf

記事一覧

2013-07-06: ソース追加


                       ファイルの分割

progJ-e.pdf にprogJ.pdf の[#20]-[#27]に対応する部分の演習を追加しました.

//#28%1
//Main.java
package myproj;
public class Main {
  public static //-
  void main (String[ ] args){
    Complex z = new Complex(2, -3);
    z.mult(new Complex(1, 1));
    System.out.println(z.abs( ));
  }
}
class Complex{
  double r, i;
  Complex(double x, double y) //-
  {r = x; i = y;}
  void add(Complex z) //-
  {r += z.r; i += z.i;}
  void mult(Complex z){
    double tmp = r * z.r - i * z.i;
    i = r * z.i + i * z.r; r = tmp;
  }
  //------------------------------------
  void sub(Complex z) //-
  {r -= z.r; i -= z.i;}
  void div(Complex z){
    double a2 = z.r * z.r + z.i * z.i;
    r = (r * z.r + i * z.i)/a2;
    i = (i * z.r - r * z.i)/a2;
  }
  //------------------------------------
  double abs( ){
    return Math.sqrt(r * r + i * i);
  }
}
--------------------------------------------------------------------------------
//#28%2
//Complex.java
package myproj;
class Complex{
  double r, i;
  Complex(double x, double y) //-
  {r = x; i = y;}
  void add(Complex z) //-
  {r += z.r; i += z.i;}
  void mult(Complex z){
    double 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);
  }
  //----------------
  void load(Complex z) //-
  {r = z.r; i = z.i;}
  double re( ){return r;}
  double im( ){return i;}
  void conj( ){i = -i;}
  double abs2( ){return r * r + i * i;}
  //----------------
  void recip( ){
    double a2 = this.abs2();
    r = r/a2; i = -i/a2;
  }
  void mult(double x){r *= x; i *= x;}
  Complex( ){r = i = 0;}
  Complex(double x){r = x; i = 0;}
  //----------------
}
//Main.java
package myproj;
public class Main {
  public static void main (String[ ] args){
    Complex z = new Complex(2, -3);
    z.mult(new Complex(1, 1));
    z.conj( );
    System.out.println(
      z.re( ) +", "+ z.im( ) +", "+ z.abs2( )
    );
    //------------------------
    z = new Complex(3, 4);
    z.recip(); z.mult(25);
    System.out.println(z.re( ) +", "+ z.im( ));
    //------------------------
  }
}
--------------------------------------------------------------------------------
//#28%4
//List.java
package myproj;
class List{
  Cell top; int sz; //size
  List( ){top = new Cell(""); sz = 0;}
  void insert(String s, int k){
    //if(k<1 || k>sz+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(k<1 || k>sz){return;}
    Cell c1 = top;
    while(k > 1){c1 = c1.n; k--;}
    c1.n = c1.n.n; sz--;
  }
  /*
  String show(int k){
    if(k<1 || k>sz){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;
    while(c1.n != null){
      c1 = c1.n; if(c1 == null) break;
      System.out.print(c1.d + " ");
    }
    System.out.println( ); 
  }
}
//Main.java
package myproj;
public class Main{
  public static void main(String[] args) {
    List x = new List( );
    x.ins("a", x.sz + 1);
    x.ins("b", x.sz + 1);
    x.ins("c", x.sz + 1); x.show( );
    x.ins("m", 1); x.ins("n", 3);
    x.show( ); x.del(1); x.del(3);
    x.del(x.sz); x.show( );
  }
}


 

コメント

NetBeansによる演習 (3)

2012-11-23 13:04:33 | 暮らし

progJ.pdf
progJ-s.pdf
progJ-e.pdf

記事一覧

2013-07-06:ソース追加

 
                   段階的詳細化

progJ-e.pdf にprogJ.pdf の[#16]-[#17]に対応する部分の演習を追加しました.

//#18%6
//Main.java
package myproj;
public class Main {
  static void sortS(int[ ] x){
    int k, n = x.length;
    int i, m, xk;
    for(k = 0; k < n; k++){
      //x[k] ~ x[n-1]中の最大を探す
      m = k;
      for(i = k + 1; i < n; i++){
        //更新
        if(x[i] > x[m]){m = i;}
      }
      //最大のものとx[k]を交換する
      xk = x[k]; x[k] = x[m];
      x[m] = xi;
    }
  }//selection sortという
  public static
  void main (String[ ] args){
    int[ ] y = {4, 7, 0, 2, 5, 8, 1};
    int i;
    sortS(y);
    //y[0] ~ y[6]を表示
    System.out.print(y[0]);
    for(i = 1; i < y.length; i++){
      System.out.print(", " + y[i]);
    }  
    System.out.println( );
  }
}
--------------------------------------------------------------------------------
//#18%7
//Main.java
package myproj;
import java.io.*;
public class Main {
  public static
  void main (String[ ] args)
  throws IOException{
    InputStream is = System.in;
    /*----------------------------------
    BufferedReader r; String s;
    r = new BufferedReader(
          new InputStreamReader(
            System.in
          )
        );
    s = r.readLine();
    ----------------------------------*/
    InputStreamReader ir;
    BufferedReader br;
    String s;
    ir = new InputStreamReader(is);
    br = new BufferedReader(ir);
    System.out.print("s = ");
    s = br.readLine();
    System.out.println(s);
    //----------------------------------
    int n = 0;
    try{
      s = br.readLine();
      n = Integer.parseInt(s);
    }
    catch(NumberFormatException e){
      System.out.println("?");
    }
    System.out.println("n = " + n);
    //----------------------------------
    PrintStream so;
    so = new PrintStream(System.out);
    so.println("OK?");
  }
}

 

コメント

NetBeansによる演習 (2)

2012-11-22 18:19:55 | 暮らし

progJ.pdf
progJ-s.pdf
progJ-e.pdf

記事一覧

2013-07-06: ソース追加


                          べき乗の計算

progJ-e.pdf にprogJ.pdf の[#14]-[#15]に対応する部分の演習を追加しました.

//#18%5
//Main.java
package myproj;
public class Main {
  static void add(int p[ ], int k){
    p[0] += k;
  }
  static double power(int n, int k){
    int i; double x = 1;
    for(i = 0; i < k; i++){x *= n;}
    /*----------------------------------
    double[] y = new double[8];
    int[] m = new int[8];
    y[0] = n; m[0] = 1;
    for(i = 1; i < 8; i++){
      m[i] = 2 * m[i-1];
      y[i] = y[i-1] * y[i-1];
    }
    while(i > 0){
      i--;
      if(k >= m[i]){x *= y[i]; k -= m[i];}
    }
    ----------------------------------*/
    return x;
  } 
  static int gcd(int n, int k){
    if(k >= n || k < 0) return 0;
    if(n % k == 0) return k;
    else return gcd(k, (n % k));
  } 
  public static //-
  void main (String[ ] args){
    int[ ] q = {3};  add(q, 5);
    System.out.println(q[0]);
    System.out.println(power(5, 13));
    System.out.println(gcd(30, 18));
  }
}

 

コメント

NetBeansによる演習 (1)

2012-11-18 20:59:56 | 暮らし

progJ.pdf
progJ-s.pdf
progJ-e.pdf

記事一覧

2013-07-06: ソース追加

 
                                配列もどきの宣言

progJ-e.pdf にprogJ.pdf の[#10]-[#13]に対応する部分の演習を追加しました.

//#18%1
//Main.java
package myproj;
public class Main {
  public static void main(String[ ] args) {
    int i = 3, k = -2;
    System.out.println("i+k="+(i+k));
    //-------------------------------------
    System.out.println("i+k="+ i + k);
    System.out.println(i + k + "=i+k"));
    System.out.println(4*i+5.0);
    //-------------------------------------
  }
}
--------------------------------------------------------------------------------
//#18%3
//Main.java
package myproj;
public class Main {
  public static
  void main (String[ ] args){
    char x; int y; double z;
    x = 65; y = x; z = y;
    //----------------------------------
    int[ ] m1 = {10, 11, 12};
    int[ ][ ] m2 = {{4, 5}, {6, 7}};
    x = (char)('0' + m1.length);
    y = m1[0]; z = m2[1][0];
    //----------------------------------
    int[ ] n1 = new int[4];
    int[ ][ ] n2 = new int[2][3];
    x = '-'; y = n1[3]; z = n2[1][2];
    //----------------------------------
    char[ ] m = {'A', 'B', 'C'};
    char[ ][ ] n = {m, new char[4]};
    x = n[0][2]; z = y = n[1].length;
    System.out.println(n[1]);
    System.out.println(
      x + ", " + y + ", " + z
    );
  }
}

コメント

「CBD%:式の計算」(Java)

2012-11-13 18:31:26 | 暮らし
progJ.pdf
progJ-s.pdf
progJ-e.pdf

記事一覧
 
                                   型変換

progJ-s.pdf に「式の計算」を追加しました.
※ ウソを書いていたので修正しました.(11/18? 追記)

コメント