Question

This assignment is an individual assignment. For Questions 1-3: consider the following code: public class A...

This assignment is an individual assignment.

For Questions 1-3: consider the following code:

public class A

{

private int number;

protected String name;

public double price;

public A()

{

System.out.println(“A() called”);

}

private void foo1()

{

System.out.println(“A version of foo1() called”);

}

protected int foo2()

{

Sysem.out.println(“A version of foo2() called);

return number;

}

public String foo3()

{

System.out.println(“A version of foo3() called”);

Return “Hi”;

}

}//end class A

public class B extends A

{

private char service;

public B()

{

   super();

   System.out.println(“B() called”);

}

public void foo1()

{

System.out.println(“B version of foo1() called”);

}

protected int foo2()

{

int n = super.foo2();

System.out.println(“B version of foo2() called”);

return (n+5);

}

public String foo3()

{

String temp = super.foo3();

System.out.println(“B version of foo3()”);

return (temp+” foo3”);

}

}//end class B

public class C extends B

{

public C()

{

super();

System.out.println();

}

public void foo1()

{

System.out.println(“C version of foo1() called”);

}

}//end class C

Assignment

  1. (20 pts) What is the output of this code sequence?

B b1 = new B();

  1. (20 pts) What is the output of this code sequence?

B b3 = new B();

int n = b3.foo2();

  1. (20 pts) What is the output of the following code?

//b4 is a B object reference

System.out.println(b4.foo3());

  1. (40 pts) You coded the following class:

public class N extends String, Integer

{

}

When you compile, you get the following message:

N.java:1: ‘{‘ expected

public class N extends String, Integer

                                               ^

1 error

Explain what the problem is and how to fix it.

Homework Answers

Answer #1

Hi,

The following is the code for the current assignment. I have explained the output along with the comments.

If you have any questions or issues with the solution, please leave me a comment. I shall help you in resolving them.  

Thank you.

Q1

Output is

A() called
B() called

As "A" is the super class of "B", the constructor of the super class is invoked in B's constructor - super() and hence, "A() called" gets display first and then the "B() called" in the B's constructor is printed

Q2

Output is

A() called
B() called
A version of foo2() called
B version of foo2() called

For first 2 lines in output, the explanation is same as the Q1. The next two lines get printed as the foo2() method in B class is invoked. In that method, the super class ie. A's foo2() is invoked and hence the next two lines are printed

Q3.

Output is

A() called
B() called
A version of foo3() called
B version of foo3()
Hi foo3

For explanation for the first 2 lines refer to Q1, the next lines are printed from foo3 method in A class and then the line in B's foo3 method.

The last line, the "Hi" is returned from the super class method ie A is appended with a string literal ("foo3") in B class and printed.

Q4. The class definition of Class N extends multiple class - String and Integer. However Java doesnt supports classes to extend from Multiple Super classes. Hence, the syntax error is reported.

To fix the problem, you can include 2 super class as member in a new class and then extends that class with class N.

The other solution is to use Interfaces.

//Code

//A.java

public class A
{

   private int number;
   protected String name;
   public double price;

   public A()
   {
       System.out.println("A() called");
   }

   private void foo1()
   {
       System.out.println("A version of foo1() called");
   }

   protected int foo2()
   {
       System.out.println("A version of foo2() called");
       return number;
   }

   public String foo3()
   {
       System.out.println("A version of foo3() called");
       return "Hi";
   }

}//end class A

//B.java

public class B extends A
{

   private char service;

   public B()
   {
       super();
       System.out.println("B() called");
   }

   public void foo1()
   {
       System.out.println("B version of foo1() called");
   }

   protected int foo2()
   {
       int n = super.foo2();
       System.out.println("B version of foo2() called");
       return (n+5);
   }

   public String foo3()
   {
       String temp = super.foo3();
       System.out.println("B version of foo3()");
       return (temp+ " foo3");
   }

}//end class B

//C.java

public class C extends B
{

   public C()
   {
       super();
       System.out.println();
   }

   public void foo1()
   {
       System.out.println("C version of foo1() called");
   }

}//end class C

//Class N


//Here N extends new class - Z which includes String and Integer as members
public class N extends Z {

   public N() {
       s = "hello";
       i = 10;
   }  
}

//Class Z


public class Z {
  
   protected String s;
   protected Integer i;

}

//TestApp.java


public class TestApp {

   public static void main(String[] args) {

       System.out.println("Q1 -> ");
       B b1 = new B();

       System.out.println("Q2 -> ");

       B b3 = new B();
       int n = b3.foo2();
      
       System.out.println("Q3 -> ");
       //b4 is a B object reference
       B b4 = new B();
       System.out.println(b4.foo3());
   }
  
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Consider the following code snippet: // A.java public class A { private String name; protected A(String...
Consider the following code snippet: // A.java public class A { private String name; protected A(String n) { name = n; } } // B.java public class B extends A { public B(String n) { super(n); } public String toString() { return name; } } What's wrong with the above code? Select all that apply. This code will encounter an access error at runtime. This code will not compile, because A's constructor is not declared public. This code will not...
what output is produced by the following code and explain how it works. public class A...
what output is produced by the following code and explain how it works. public class A { int a = 1; int b = 2; public int getSum(int a, int b) {     this.a+=a;     this.b+=b;     return this.a + this.b; } } public class B extends A { int a = 3; int b = 4; public int getSum(int a, int b) {     this.b=a;     super.b=b+b;     return super.a+this.b; } } public class q2 { public static void...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
Identify and explain which type of polymorphism technique is used in the following code A- class...
Identify and explain which type of polymorphism technique is used in the following code A- class Calculator { public void add(int i, int j) { System.out.println("Integer addition"); } public void add(int i, double j) { System.out.println("Integer and double addition"); } public void add(double i, double j) { System.out.println("Double addition"); } } B- public class MyClass { void test(int a) { System.out.println("a: " + a); } void test(int a, int b) { System.out.println("a and b: " + a + "," +...
Which method is correct to access the value of count? public class Person { private String...
Which method is correct to access the value of count? public class Person { private String name; private int age; private static int count = 0; } A. private int getCount() {return (static)count;} B. public static int getCount() {return count;} C. public int getCount() {return static count;} D. private static int getCount() {return count;} How can you print the value of count? public class Person { private String name; private int age; private static int count=0; public Person(String a, int...
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
I have a 2 class code and it works everything is fine and runs as it...
I have a 2 class code and it works everything is fine and runs as it supposed too. What will the UML be for both classes. Here's the code, any help will be awsome, Thanks. import java.util.Scanner; public class PayRollDemo { public static void main(String[] args) { double payRate; int hours; PayRoll pr = new PayRoll(); Scanner keyboard = new Scanner(System.in); for (int index = 0; index < 7 ; index++ ) { System.out.println(); System.out.println("EmployeeID:" + pr.getEmployeeID(index)); System.out.println(); System.out.println("Enter the...