public class Game{ public void players() { System.out.println(“1 or more”); } }
ID: 3765560 • Letter: P
Question
public class Game{
public void players() { System.out.println(“1 or more”); }
}
public class Chess extends Game {
public void boardSize() { System.out.println(“8 by 8”); }
public void players() { System.out.println(“2”); }
}
public class ThreeDChess extends Chess{
public void boardSize() {System.out.println(“7 levels”); }
public void hasMoveableBoard() { System.out.println(“true”); }
}
Assume that these statements have already executed:
Game g = new Game();
Chess c = new Chess();
ThreeDChess t = new ThreeDChess();
Object og = g;
Game gc = c;
Game gt = t;
// Add new statements here ONE AT A TIME
I recommend copying the questions below into a text editor and then typing in your answers.
1) What are the static and dynamic types for the variables
og
gc
gt
2) Explain what would happen if each of the following statements were inserted ONE AT A TIME in the spot marked above. Answer the following questions for each statement:
Would the statement cause a syntax error?
If not, what are the static and dynamic types of the variable declared?
Game gx = gc; // Answer:
Game gx = og; // Answer:
Game gx = (Game)og; // Answer:
Chess cx = (Chess)gt; // Asnwer:
ThreeDChess tx = (ThreeDChess)gc; //Answer:
3) Finally, explain what would happen if each of the following statements were inserted ONE AT A TIME in the spot marked above. Answer the following questions for each statement:
Would the statement cause a syntax error?
If not, would the statement cause a ClassCastException?
If not, then explain what output is shown
gc.players(); // Answer:
og.players(); // Answer:
t.players(); // Answer:
((ThreeDChess)gc).hasMoveableBoard(); // Answer:
((ThreeDChess)gt).hasMoveableBoard(); // Answer:
Explanation / Answer
==========================================================
1) What are the static and dynamic types for the variables
og
gc
gt
----------
Answer :
----------
From the declaration of statements
Game g = new Game();
Chess c = new Chess();
ThreeDChess t = new ThreeDChess();
Object og = g;
Game gc = c;
Game gt = t;
The varibles are g,c,t,og,gc,gt. All variables are dynamic type.
==========================================================
2) Explain what would happen if each of the following statements were inserted ONE AT A TIME in the spot marked above. Answer the following questions for each statement:
Would the statement cause a syntax error?
If not, what are the static and dynamic types of the variable declared?
Game gx = gc; // Answer: No error. Since gc is of object of Game class.
Game gx = og; // Answer: Gives error, Can not covert from Object to Game class.
Game gx = (Game)og; // Answer: No error, Since Typecasting is added.
Chess cx = (Chess)gt; // Asnwer: No error, Since Typecasting is added from Game to Chess class.
ThreeDChess tx = (ThreeDChess)gc; //Answer:No error, Since Typecasting is added from Game to ThreeDChess class.
==========================================================
3) Finally, explain what would happen if each of the following statements were inserted ONE AT A TIME in the spot marked above. Answer the following questions for each statement:
Would the statement cause a syntax error?
If not, would the statement cause a ClassCastException?
If not, then explain what output is shown
gc.players(); // Answer: 2 is printed, Since gc is refering object of Chess class.
og.players(); // Answer: Syntax error. og is referening Object class.
t.players(); // Answer: 2 is printed, Since t is refering the ThreeDChess class, Which is being calling its super class method
((ThreeDChess)gc).hasMoveableBoard(); // Answer: Throws exception java.lang.ClassCastException: Chess cannot be cast to ThreeDChess. Since gc refering Chess class only.
((ThreeDChess)gt).hasMoveableBoard(); // Answer: true is printed. hasMoveableBoard method of ThreeDChess class. Since gt is refering ThreeDChess class.
==========================================================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.