3章 プリミティブと参照
この章は変数についてです。
プログラミングマグネット
これらの材料で、
int y = 0; ref = index [y]; islands [0] = "Bermuda"; islands [1] = "Fiji"; islands [2] = "Azores"; islands [3] = "Cozumel"; // この行はテキストでは islands [3] == "Cozumel";と誤記? int ref; while (y < 4){ System.out.println(islands [ref]); index [0] = 1; index [1] = 3; index [2] = 0; index [3] = 2; String [] islands = new String [4]; System.out.print("island = "); int [] index = new int [4]; y = y + 1; class TestArrays{ public static void main(String [] args){
下のような結果にせよということです。
今回は、材料を眺めていておおよそ見当がつきました。
以下のように並べました。
class TestArrays{ public static void main(String [] args){ String [] islands = new String [4]; islands [0] = "Bermuda"; islands [1] = "Fiji"; islands [2] = "Azores"; islands [3] = "Cozumel"; int [] index = new int [4]; index [0] = 1; index [1] = 3; index [2] = 0; index [3] = 2; int y = 0; // [miss1of2] この行をwhileの内に入れるミス while (y < 4){ int ref; //テキストでこの行はwhileの上にある ref = index [y]; System.out.print("island = "); // [miss2of2] System.out print と記述 System.out.println(islands [ref]); y = y + 1; } } }
二度ほどのミスで完成できました。
プールパズル
四分の二はできましたが、四分の一はたどり着けず、残りの四分の一は、よく分からない感じです。
この結果を、
このコードで導きます。
class Triangle{ double area; int height; int length; public static void main(String [] args){ int x = 0; Triangle [] ta = new Triangle [4]; while(x < 4){ ta [x] = new Triangle(); ta [x].height = (x + 1) *2; ta [x].length = x + 4; ta [x].setArea(); // 下のvoid setArea()内やここをどう書くかが浮かばなかった System.out.print("triangle " + x + ", area"); System.out.println(" = " + ta [x].area); x = x + 1; } int y= x; x = 27; Triangle t5 = ta [2]; // いきなり t5 って何?? ta [2].area = 343; System.out.print("y =" + y); System.out.println(",t5 area = " + t5.area); // ここの以上5,6行の意味がよく分からない } void setArea(){ area = (height * length) / 2; } }
それ以外はだいたい分かりました。