Question

public class Game{ public void players() { System.out.println(“1 or more”); } } public class Chess extends...

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:

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
public class Game{ public void players() { System.out.println(“1 or more”); } } public class Chess extends...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • public class Polymorph public static class CAL public void printHello System.out.println("Hya"); public static class w extends...

    public class Polymorph public static class CAL public void printHello System.out.println("Hya"); public static class w extends CAC public void printHello System.out.println("Howdy"); public static class PB extends WE public void printtello System.out.println("Hello") public static void main(String[] args) { A l = new CÁO: CA ab - (CA) new W(); CA ac - (CA) new PC); Wbc - (W) new P80; 33.printHello(); ab.printHello(); ac.printHello(); be.printHello(); Type your answer, do not copy/paste

  • What is the output of the following code? class parent { public void someFunction() { System.out.println("Parent...

    What is the output of the following code? class parent { public void someFunction() { System.out.println("Parent Function"); } } public class child extends parent { public void someFunction() { System.out.println("Child Function"); } public static void main(String args) { parent p1 = new parent(); parent p2 = new child(); child c1 = new child(); p1.someFunction(); p2.someFunction(); c1.someFunction(); } }

  • Please help to answer ASAP Class Parent {Parent(String s) {System.out.println("S1");}} class Child extends Parent {public Child()...

    Please help to answer ASAP Class Parent {Parent(String s) {System.out.println("S1");}} class Child extends Parent {public Child() {System.out.println("S2");}} Identify the correct statements for the above code snippet super();//automatically inserted here in class file of Child super("Hello");//inserted here in class file of Child compilation error Compiles successfully

  • Consider the following Java classes: class A{ public int foo () { return 1; } public...

    Consider the following Java classes: class A{ public int foo () { return 1; } public void message () { System.out.println( "A" + foo()); } } class B extends A { public int foo() {return 2; } } class C extends B { public void message () { System.out.println( "C" + foo()); } } (i) What are the outputs of the following code? (ii) What would be the outputs if Java used static dispatching rather than dynamic dispatching? B b...

  • Consider the following codes: public class TestThread extends Thread {     public static void main(String[] args)...

    Consider the following codes: public class TestThread extends Thread {     public static void main(String[] args) {         TestThread thread = new TestThread();     }     @Override     public void run() {         printMyName();     }     private void printMyName() {         System.out.println("Thread is running");     } } Test Stem / Question Choices 1: What method should you invoke to start the thread TestThread? A: start() B: run() C: No. Thread will run automatically when executed. D: TestThread is not...

  • **JAVA*JAVA Question 1 How many k's values will be printed out? public static void main(Stringl args)...

    **JAVA*JAVA Question 1 How many k's values will be printed out? public static void main(Stringl args) for (int k-1: k10:k if (k8) continue: System.out.println k) Question 2 3.5 pts Analyze the following code. Which one is correct? package p public class A package p2: import p1. public class B extends Af int i- 5 private int j- 10: public A0I public static void main(Stringll args) B b new B0: System.out.println(a.i+", "+ajt", "+b.it""+bj): a.i is printable None of them b.i is...

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • class Upper { private int i; private String name; public Upper(int i){ name = "Upper"; this.i...

    class Upper { private int i; private String name; public Upper(int i){ name = "Upper"; this.i = i;} public void set(Upper n){ i = n.show();} public int show(){return i;} } class Middle extends Upper { private int j; private String name; public Middle(int i){ super(i+1); name = "Middle"; this.j = i;} public void set(Upper n){ j = n.show();} public int show(){return j;} } class Lower extends Middle { private int i; private String name; public Lower(int i){ super(i+1); name =...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

  • # Java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class Test extends Application {...

    # Java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class Test extends Application {   @Override // Override the start method in the Application class   public void start(Stage primaryStage) {     // Create a button and place it in the scene     Button btOK = new Button("OK");     btOK.setOnAction(e -> System.out.println("OK 1"));     btOK.setOnAction(e -> System.out.println("OK 2"));     Scene scene = new Scene(btOK, 200, 250);     primaryStage.setTitle("MyJavaFX"); // Set the stage title     primaryStage.setScene(scene); // Place the scene in the stage     primaryStage.show(); // Display the stage...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT