CS计算机代考程序代写 compiler Java CS 61B Inheritance Spring 2021 Discussion 4: Feburary 8, 2021

CS 61B Inheritance Spring 2021 Discussion 4: Feburary 8, 2021
1
(a)
Raining Cats and Dogs
Below, four classes are defined. What would Java do after executing the main method in the TestAnimal class? Next to each blank, if something is printed write it down. If there is an error, write whether it is a runtime error or compile time error, and then proceed through the rest of the code as if the erroneous line were not there. Lines in blue cover casting.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
public void greet() {System.out.println(“Animal ” + name + ” says: ” + this.noise);} public void play() {System.out.println(“Woo it is so much fun being an animal!”)}
}
public class Cat extends Animal { public Cat(String name, int age) {
super(name, age);
this.noise = “Meow!”; }
@Override
public void greet() {System.out.println(“Cat ” + name + ” says: ” + this.noise);}
public void play(String expr) {System.out.println(“Woo it is so much fun being a cat!” + expr)} }
public class Dog extends Animal { public Dog(String name, int age) {
super(name, age);
noise = “Woof!”;
}
@Override
public void greet() {System.out.println(“Dog ” + name + ” says: ” + this.noise);}
public class Animal {
public String name, noise; public int age;
1
2
3
4
5
6
7
8 9}
public Animal(String name, int age) { this.name = name;
this.age = age;
this.noise = “Huh?”;

2 Inheritance
36
37 public void play(int happiness) {
38 if (happiness > 10) {
39 System.out.println(“Woo it is so much fun
40 }
41 }
42 }
43
44 public class TestAnimal {
45 public static void main(String[] args) {
46 Animal a = new Animal(“Pluto”, 10);
47 Cat c = new Cat(“Garfield”, 6);
48 Dog d = new Dog(“Fido”, 4);
49 a.greet();
50 c.greet();
51 d.greet();
52 c.play();
53 c.play(“:)”) //
54 a=c;
55 ((Cat) a).greet(); //
56 ((Cat) a).play(“:D”);
57 a.play(14);
58 ((Dog) a).play(12); // ______________________
59 a.greet(); // ______________________
60 c = a; // ______________________
61 }
62 }
(b) Spoiler alert! There is an error on the last line, line 60. How could we fix this error?
// ______________________
// ______________________
// ______________________
// ______________________
______________________
______________________
// ______________________
// ______________________
being a dog!”)

2 An Exercise in Inheritance Misery
Cross out any lines that result in compiler errors, as well as subsequent lines that would fail because of the compiler error. Put an X through runtime errors (if any). Don¡¯t just limit your search to main, there could be errors in classes A,B,C. What does D.main output after removing these lines?
System.out.println(“Am1-> ” + x); } System.out.println(“Am2-> ” + this.x); } x = 99; }
System.out.println(“Bm2-> ” + x); }
System.out.println(“Bm2y-> ” + y); }
System.out.println(“Bm3-> ” + “called”); }
System.out.println(“Cm2-> ” + super.x); } System.out.println(“Cm4-> ” + super.super.x); } System.out.println(“Cm5-> ” + y); }
public class A {
public int x = 5; public void m1() { public void m2() { public void update() {
1
2
3
4
5 6}
7 public class B extends A{
8 public void m2() {
9 public void m2(int y){
10 public void m3() {
11 }
12 public class C extends B{
13 publicinty=x+ 1;
14 public void m2() {
15 public void m4() {
16 public void m5() {
17 }
18 public class D {
19 public static void main
20 Ba0=new A();
21 a0.m1();
22 a0.m2(16);
23 Ab0=new B();
24 System.out.println(b0.x);
25 b0.m1();
26 b0.m2();
27 b0.m2(61);
28 Bb1=new B();
29 b1.m2(61);
30 b1.m3();
31 Ac0=new C();
32 c0.m2();
33 Cc1=(A)
34 Aa1=(A) c0;
35 Cc2=(C) a1;
36 c2.m3();
37 c2.m4();
38 c2.m5();
39 ((C) c0).m3();
40 (C) c0.m2();
41 b0.update();
new C();
(String[] args) {
Inheritance 3

4 Inheritance b0.m1();
42
43 } 44 }