CS计算机代考程序代写 compiler 10 11 12 13 14

10 11 12 13 14
1
2
3 4} 5
6
7
8 9}
CS 61B Inheritance Spring 2021 Exam Prep Discussion 4: February 8, 2021
1 Athletes
Suppose we have the Person, Athlete, and SoccerPlayer classes defined below.
class Person {
void speakTo(Person other) { System.out.println(“kudos”); } void watch(SoccerPlayer other) { System.out.println(“wow”); }
class Athlete extends Person {
void speakTo(Athlete other) { System.out.println(“take notes”); } void watch(Athlete other) { System.out.println(“game on”); }
class SoccerPlayer extends Athlete {
void speakTo(Athlete other) { System.out.println(“respect”); } void speakTo(Person other) { System.out.println(“hmph”); }
}
(a) For each line below, write what, if anything, is printed after its execution. Write CE if there is a compiler error and RE if there is a runtime error. If a line errors, continue executing the rest of the lines.
1 Person itai = new Person();
2
3 SoccerPlayer shivani = new Person(); 4
5 Athlete sohum = new SoccerPlayer(); 6
7 Person jack = new Athlete();
8
9 Athlete anjali = new Athlete();
10
11 SoccerPlayer chirasree = new SoccerPlayer(); 12
13 itai.watch(chirasree);
14
15 jack.watch(sohum);
16
17 itai.speakTo(sohum);
18
19 jack.speakTo(anjali);
20

2 Inheritance
21 anjali.speakTo(chirasree); 22
23 sohum.speakTo(itai);
24
25 chirasree.speakTo((SoccerPlayer) sohum); 26
27 sohum.watch(itai);
28
29 sohum.watch((Athlete) itai);
30
31 ((Athlete) jack).speakTo(anjali);
32
33 ((SoccerPlayer) jack).speakTo(chirasree); 34
35 ((Person) chirasree).speakTo(itai);
(b) You may have noticed that jack.watch(sohum) produces a compile error. In- terestingly, we can resolve this error by adding casting! List two fixes that would resolve this error. The first fix should print wow. The second fix should print game on. Each fix may cast either jack or sohum.
1.
2.
(c) Now let¡¯s try resolving as many of the remaining errors from above by adding or removing casting! For each error that can be resolved with casting, write the modified function call below. Note that you cannot resolve a compile error by creating a runtime error! Also note that not all, or any, of the errors may be resolved.

6 7 8 9
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 36 37 38 39 40
2 Dynamic Method Selection
Modify the code below so that the max method of DMSList works properly. As- sume all numbers inserted into DMSList are positive, and we only insert using insertFront. You may not change anything in the given code. You may only fill in blanks. You may not need all blanks. (Spring ¡¯16, MT1)
public class DMSList { private IntNode sentinel; public DMSList() {
sentinel = new IntNode(-1000, _____________________);
public class IntNode { public int item;
public IntNode next;
public IntNode(int i, IntNode h) {
item = i;
next = h; }
public int max() {
return Math.max(item, next.max());
} }
public _________________________________ { ____________________________________________________________ ____________________________________________________________ ____________________________________________________________ ____________________________________________________________ ____________________________________________________________ ____________________________________________________________ ____________________________________________________________
____________________________________________________________
}
/* Returns 0 if list is empty. Otherwise, returns the max element. */ public int max() {
return sentinel.next.max(); }
public void insertFront(int x) { sentinel.next = new IntNode(x, sentinel.next); } }
1
2
3
4 5}
Inheritance 3

10 11 12 13 14 15 16 17 18 19 20 21
int
4 Inheritance
3 Challenge: A Puzzle
Consider the partially filled classes for A and B as defined below:
1 public class A {
2 public static void main(String[] args) {
3
4
5}
6
7 int 8
9}
___ y = new ___(); ___ z = new ___();
fish(A other) { return 1;
fish(B other) {
return 2; }
}
class B @Override
extends A {
int fish(B other) { return 3;
} }
Note that the only missing pieces of the classes above are static/dynamic types! Fill in the four blanks with the appropriate static/dynamic type ¡ª A or B ¡ª such that the following are true:
1. y.fish(z) equals z.fish(z)
2. z.fish(y) equals y.fish(y)
3. z.fish(z) does not equal y.fish(y)