| <演習1>の解答例 |
| class iPhone { int storage; // ストレージ(GB) int weight; // 重さ(g) double disp_size; // ディスプレイサイズ(inch) } class Ensyu0801 { public static void main(String[] args) { iPhone ipSE = new iPhone(); iPhone ip13 = new iPhone(); System.out.println("<iPhoneSE>"); ipSE.storage = 256; ipSE.weight = 144; ipSE.disp_size = 4.7; System.out.println("容量は " + ipSE.storage); System.out.println("重さは " + ipSE.weight); System.out.println("画面の大きさは " + ipSE.disp_size); System.out.println("\n<iPhone13>"); ip13.storage = 512; ip13.weight = 173; ip13.disp_size = 6.1; System.out.println("容量は " + ip13.storage); System.out.println("重さは " + ip13.weight); System.out.println("画面の大きさは " + ip13.disp_size); } } |
| <演習2>の解答例 |
| class iPhone { int storage; // ストレージ(GB) int weight; // 重さ(g) double disp_size; // ディスプレイサイズ(inch) void setStorage( int s ){ storage = s; } void setWeight( int w ){ weight = w; } void setDispSize( double ds ){ disp_size = ds; } } class Ensyu0802 { public static void main(String[] args) { iPhone ipSE = new iPhone(); iPhone ip13 = new iPhone(); System.out.println("<iPhoneSE>"); ipSE.setStorage( 256 ); ipSE.setWeight( 144 ); ipSE.setDispSize( 4.7 ); System.out.println("容量は " + ipSE.storage); System.out.println("重さは " + ipSE.weight); System.out.println("画面の大きさは " + ipSE.disp_size); System.out.println("\n<iPhone13>"); ip13.setStorage( 512 ); ip13.setWeight( 173 ); ip13.setDispSize( 6.1 ); System.out.println("容量は " + ip13.storage); System.out.println("重さは " + ip13.weight); System.out.println("画面の大きさは " + ip13.disp_size); } } |
| <演習3>の解答例 |
| class iPhone { int storage; // ストレージ(GB) int weight; // 重さ(g) double disp_size; // ディスプレイサイズ(inch) void show(){ System.out.println("容量は " + storage); System.out.println("重さは " + weight); System.out.println("画面の大きさは " + disp_size); } void setStorage( int s ){ storage = s; } void setWeight( int w ){ weight = w; } void setDispSize( double ds ){ disp_size = ds; } } class Ensyu0803 { public static void main(String[] args) { iPhone ipSE = new iPhone(); iPhone ip13 = new iPhone(); System.out.println("<iPhoneSE>"); ipSE.setStorage( 256 ); ipSE.setWeight( 144 ); ipSE.setDispSize( 4.7 ); ipSE.show(); System.out.println("\n<iPhone13>"); ip13.setStorage( 512 ); ip13.setWeight( 173 ); ip13.setDispSize( 6.1 ); ip13.show(); } } |
| <演習4>の解答例 |
| class iPhone { int storage; // ストレージ(GB) int weight; // 重さ(g) double disp_size; // ディスプレイサイズ(inch) void setSpec( int s, int w, double ds ){ // まとめて設定 storage = s; weight = w; disp_size = ds; } void show(){ System.out.println("容量は " + storage); System.out.println("重さは " + weight); System.out.println("画面の大きさは " + disp_size); } void setStorage( int s ){ storage = s; } void setWeight( int w ){ weight = w; } void setDispSize( double ds ){ disp_size = ds; } } class Ensyu0804 { public static void main(String[] args) { iPhone ipSE = new iPhone(); iPhone ip13 = new iPhone(); System.out.println("<iPhoneSE>"); ipSE.setSpec( 256, 144, 4.7 ); ipSE.show(); System.out.println("\n<iPhone13>"); ip13.setSpec( 512, 173, 6.1 ); ip13.show(); } } |
| <演習5>の解答例 |
| class Keisan { int val; void setVal( int v ){ val = v; } void addVal( int v ){ val += v; } int getVal(){ return val; } } class Ensyu0805 { public static void main(String[] args) { Keisan a = new Keisan(); a.setVal( 5 ); // 整数値を設定 System.out.println(" (1) val = " + a.getVal() ); a.addVal( 10 ); // 値を加算 System.out.println(" (2) val = " + a.getVal() ); } } |
| <演習6>の解答例 |
| class Keisan { int val; // 3つの値の合計をvalに代入する void setGoukei( int v1, int v2, int v3 ){ val = v1 + v2 + v3; } void setVal( int v ){ val = v; } void addVal( int v ){ val += v; } int getVal(){ return val; } } class Ensyu0806 { public static void main(String[] args) { Keisan a = new Keisan(); a.setVal( 5 ); // 整数値を設定 System.out.println(" (1) val = " + a.getVal() ); a.addVal( 10 ); // 値を加算 System.out.println(" (2) val = " + a.getVal() ); a.setGoukei( 9, 5, 7 ); // 合計を設定 System.out.println(" (3) val = " + a.getVal() ); } } |