詳しくは公式マニュアルをお読みください。
Javaって、こんな風にも書ける、とか、こうとも言う、とか、色々有るので、
以下は雰囲気だけのざっくりとした説明です。
作成中です。ミス有ったら後免。
// Beaker_1.java 2016.10.13 by Ryn public class Beaker_1 { // ビーカーの仕様 private String label = "A"; // Aというラベルが貼ってある private int size = 500; // サイズは、500ml private int water = 0; // 最初は、からっぽ }
// Scientist_1.java 2016.10.13 by Ryn public class Scientist_1 { public static void main(String[] args) { Beaker_1 a = new Beaker_1(); // ビーカー a を実体化 } }
Beaker_1 a; a = new Beaker_1();
1行目 a -+--------+- | addr | ← Beaker_1クラスの a の -+--------+- 先頭アドレスを記憶する為のメモリ領域を確保 ↑ 2行目 ↑ a.label a.size a.water -+--------+--------+--------+- | addr | 500 | 0 | ← a の為のメモリ領域を、 -+--------+--------+--------+- Beaker_1クラスに必要な大きさで確保 ↑ ↑ a.label のメモリ領域の先頭アドレス(String は参照型=文字列は長さが不定なので)
// Beaker_2.java 2016.10.13 by Ryn public class Beaker_2 { private String label = "A"; private int size = 500; private int water = 0; public String getLabel() { // ラベル名を String で返すゲッターメソッド return label; } public int getSize() { // サイズを int で返すゲッターメソッド return size; } public int getWater() { // 水の量を int で返すゲッターメソッド return water; } }
// Sientist_2.java 2016.10.13 by Ryn public class Scientist_2 { public static void main(String[] args) { Beaker_2 a = new Beaker_2(); System.out.printf("label[ %-6s ] size = %4dml / water = %4dml¥n", a.getLabel(), a.getSize(), a.getWater()); } }
// Beaker_3.java 2016.10.13 by Ryn public class Beaker_3 { private String label = "A"; private int size = 500; private int water = 0; public void dispData() { System.out.printf("label[ %-6s ] size = %4dml / water = %4dml¥n", label, size, water); } }
// Scientist_3.java 2016.10.13 by Ryn public class Scientist_3 { public static void main(String[] args) { Beaker_3 a = new Beaker_3(); a.dispData(); } }
// Beaker_4.java 2016.10.13 by Ryn public class Beaker_4 { private String label = ""; private int size = 500; private int water = 0; public void setLabel(String label) { // ラベルを貼るセッターメソッド this.label = label; } public void dispData() { System.out.printf("label[ %-6s ] size = %4dml / water = %4dml¥n", label, size, water); } }
// Scientist_4.java 2016.10.13 by Ryn public class Scientist_4 { public static void main(String[] args) { Beaker_4 a = new Beaker_4(); a.setLabel("Red"); Beaker_4 b = new Beaker_4(); b.setLabel("Green"); a.dispData(); b.dispData(); } }
// Beaker_5.java 2016.10.13 by Ryn public class Beaker_5 { private String label = ""; private int size = 500; private int water = 0; public void setLabel(String label) { this.label = label; } public void moveWater(int water) { int i = this.water + water; System.out.printf("label[ %-6s ] %3dml + %3dml = %4dml", label, this.water, water, i); if(i <= size && i >= 0) this.water = i; else System.out.print(" *Error!*"); System.out.println(); } public void dispData() { System.out.printf("label[ %-6s ] size = %4dml / water = %4dml¥n", label, size, water); } }
import java.util.Scanner; // Scientist_5.java 2016.10.13 by Ryn public class Scientist_5 { public static void main(String[] args) { Beaker_5 a = new Beaker_5(); a.setLabel("Red"); a.dispData(); int water; Scanner sc = new Scanner(System.in); System.out.print("water? : "); water = sc.nextInt(); while(water != 0){ a.moveWater(water); a.dispData(); System.out.print("water? : "); water = sc.nextInt(); } sc.close(); System.out.println("終了"); } }
// Beaker_6.java 2016.10.13 by Ryn public class Beaker_6 { private String label; private int size; private int water = 0; public Beaker_6(String label, int size) { // コンストラクタ this.label = label; this.size = size; } public void moveWater(int water) { int i = this.water + water; System.out.printf("label[ %-6s ] %3dml + %3dml = %4dml", label, this.water, water, i); if(i <= size && i >= 0) this.water = i; else System.out.print(" *Error!*"); System.out.println(); } public void dispData() { System.out.printf("label[ %-6s ] size = %4dml / water = %4dml¥n", label, size, water); } }
import java.util.Scanner; // Scientist_6.java 2016.10.13 by Ryn public class Scientist_6 { public static void main(String[] args) { Beaker_6 a = new Beaker_6("Red",500); Beaker_6 b = new Beaker_6("Green",720); a.dispData(); b.dispData(); int water; Scanner sc = new Scanner(System.in); System.out.print("water? : "); water = sc.nextInt(); while(water != 0){ a.moveWater(water); b.moveWater(water); System.out.println(); a.dispData(); b.dispData(); System.out.print("water? : "); water = sc.nextInt(); } sc.close(); System.out.println("終了"); } }
//Beaker_7.java 2016.11.30 by Ryn public class Beaker_7 { private String label; private int size; private int water = 0; public Beaker_7(String label, int size) { this.label = label; this.size = size; } public int getSize() { return size; } public int getWater() { return water; } public void moveWater(int water) { int i = this.water + water; // System.out.printf("label[ %-6s ] %3dml + %3dml = %4dml", // label, this.water, water, i); if(i <= size && i >= 0) this.water = i; // else // System.out.print(" *Error!*"); // // System.out.println(); } public void dispData() { System.out.printf("label[ %-6s ] size = %4dml / water = %4dml¥n", label, size, water); } }
//Scientist_7.java 2016.11.30 by Ryn public class Scientist_7 { // swap method public static boolean swap(Beaker_7 x, Beaker_7 y) { if(x.getSize() == y.getSize()){ // サイズが同じか? Beaker_7 temp = new Beaker_7("Temp", x.getSize()); x.dispData(); // swap before y.dispData(); temp.dispData(); temp.moveWater(x.getWater()); // x → temp x.moveWater(- x.getWater()); x.moveWater(y.getWater()); // y → x y.moveWater(- y.getWater()); y.moveWater(temp.getWater()); // temp → y temp.moveWater(- temp.getWater()); System.out.println(" ↓"); x.dispData(); // swap after y.dispData(); temp.dispData(); return true; // 成功した }else return false; // サイズが違った } // main method public static void main(String[] args) { Beaker_7 a = new Beaker_7("A",500); // ビーカー Beaker_7 b = new Beaker_7("B",500); a.moveWater(350); // 水を入れた b.moveWater(200); a.dispData(); // main before b.dispData(); System.out.println(" ↓"); if(! swap(a, b)) // 入れ替え System.out.println("False"); System.out.println(" ↓"); // main after a.dispData(); b.dispData(); } }
//Beaker_8.java 2016.10.13 by Ryn public class Beaker_8 { private String label = "?"; private int size = 500; private int water = 0; public Beaker_8(String label, int size) { // コンストラクタ this.label = label; this.size = size; } public Beaker_8(String label) { // コンストラクタのオーバーロード this.label = label; } public Beaker_8(int size) { this.size = size; } public Beaker_8() { } public void dispData() { System.out.printf("label[ %-6s ] size = %4dml / water = %4dml¥n", label, size, water); } }
//Scientist_8.java 2016.10.13 by Ryn public class Scientist_8 { public static void main(String[] args) { Beaker_8 a = new Beaker_8(); Beaker_8 b = new Beaker_8("Red"); Beaker_8 c = new Beaker_8(350); Beaker_8 d = new Beaker_8("Green",720); a.dispData(); b.dispData(); c.dispData(); d.dispData(); } }
// Beacker_8.java 2016.10.13 by Ryn public class Beaker_8 { private String label = "?"; private int size = 500; private int water = 0; public Beaker_8(String label, int size) { // コンストラクタ this.label = label; this.size = size; } public Beaker_8(String label) { // コンストラクタのオーバーロード this.label = label; } public Beaker_8(int size) { this.size = size; } public Beaker_8() { } public void dispData() { System.out.printf("label[ %-6s ] size = %4dml / water = %4dml¥n", label, size, water); } }
// Scientist_9.java 2016.10.13 by Ryn public class Scientist_9 { public static void main(String[] args) { Beaker_8 a[] = new Beaker_8[4]; a[0] = new Beaker_8(); a[1] = new Beaker_8("Red"); a[2] = new Beaker_8(350); a[3] = new Beaker_8("Green",720); for(int i = 0; i < 4; i++) a[i].dispData(); } }
Beaker_8 a[]; a = new Beaker_8[4]; a[0] = new Beaker_8();
1行目 a[] -+----+- |addr| ←Beaker_8クラスの配列 a の各要素へのポインタの為の配列の -+----+- 先頭アドレスを記憶する為のメモリ領域を確保 ↑ 2行目 ↑ a[0] a[1] a[2] a[3] -+----+----+----+----+- |addr|NULL|NULL|NULL| ← 配列 a の Beaker_8クラスの各要素の -+----+----+----+----+- 先頭アドレスを記憶する為のメモリ領域を確保 ↑ 4行目 ↑ a[0].label a[0].size a[0].water -+----------+----------+----------+- | addr | 500 | 0 | ← a[0]の為のメモリ領域を、 -+----------+----------+----------+- Beaker_8クラスに必要な大きさで確保 ↑ ↑ a[0].label のメモリ領域の先頭アドレス(String は参照型=文字列は長さが不定なので)