Assume we have a Flowerclass (*), and two of its methods are setColour(String colour)and setHeight(double height). What is wrong with the following code? Explain your answer.
Flower[] f = new Flower[2]; f[0] = new Flower(); f[0].setColour("Red"); f[0].setHeight(4.0); f[1].setColour("Blue"); f[1].setHeight(3.5); ````   缺少一个 f[1] = new Flower(); # Question4   Assume we have a Flowerclass(\*), and two of its methods are setColour(String colour)and setHeight(double height). What is wrong with the following code? Explain your answer. ```java Flower[] f = new Flower[2]; f[0] = new Flower(); f[0].setColour("Red"); f[0].setHeight(4.0); f[1] = new Flower(); f[1].setColour("Blue"); f[1].setHeight(3.5); f[2] = new Flower(); f[2].setColour("Pink"); f[2].setHeight(2.5);
同上,也是超界,把2改成3就对了。
Question5(好)
Assume we have a Flowerclass (*), and two of its methods are setColour(String colour)and setHeight(double height). What is wrong with the following code? Explain your answer.
1 2 3 4 5 6 7 8 9 10
//No initialize i
publicclassTest_Q5{ publicstaticvoidmain(String[] args){ int i; i = i + 5; System.out.println("i = " + i); } }
Local Variables are not given a defined value! Instance variables are. 可以 int i = 0;
Truck mycar= new Truck(); System.out.println(mycar); mycar.m1(); mycar.m2();
输出内容: vroomT truck 1 car 1
Question10
1 2 3 4 5 6 7 8 9 10
publicclassTest_Q10{ publicstaticvoidmain(String[] args){ String s = "6"; int n = 3; double d = 4.5; System.out.println(s + n + d);
}
}
输出内容:634.5
Question11
Identify which statements are TRUE and which are FALSE. Justify your answers. 1.subclass has direct access to its superclass’ private data and methods. 2.A class can extend more than one superclass. 3.An abstract class must contain at least one abstract method. 4.An abstract class must not contain any instance variables.
A class Animalhas a subclass Dog. Which of the following is TRUE? a) Dog cannot have subclasses. b) Dog has no other parent than Animal. c) Animal can have only one subclass. d) Dog cannot have siblings. 答案: b 解析:a)狗可以有子类,比如母狗,公狗等。 c)动物可以分子类,比如猫,兔子等 d)狗也可以有兄弟姐妹,比如上面说的猫和兔子
Question13
1 2 3 4 5 6 7
publicclassTest_Q13a{ publicstaticvoidmain(String[] args){ String s1 = new String("aaa"); String s2 = new String("aaa"); System.out.println(s1==s2); } }
false
1 2 3 4 5 6 7
publicclassTest_Q13b{ publicstaticvoidmain(String[] args){ String s1 = new String("aaa"); String s2 = new String("aaa"); System.out.println(s1.equals(s2)); } }