Question

In: Computer Science

For Questions 1-3: consider the following code: public class A { private int number; protected String...

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.

Solutions

Expert Solution

  1. The output will be:

A() called

B() called

Explanation:

It is given in the code that A is the superclass of B.

The constructor super() is invoked in constructor of B, hence A() is called and display on the screen and then B() is called. Therefore, constructor of B will print after the constructor of A.

  1. The output will be:

A() called

B() called

A version of foo2() called

B version of foo2() called

Explanation:

It is given in the code that A is the superclass of B.

The constructor super() is invoked in constructor of B, hence A() is called and display on the screen and then B() is called. Therefore, constructor of B will print after the constructor of A.

The foo2() method is invoked in B class. In the method of foo2(), A (superclass) is invoked which results in last two lines of output.

  1. The output will be:

A() called

B() called

A version of foo3() called

B version of foo3()

Hi foo3

Explanation:

It is given in the code that A is the superclass of B.

The constructor super() is invoked in constructor of B, hence A() is called and display on the screen and then B() is called. Therefore, constructor of B will print after the constructor of A.

Line 3 and line 4 is printed because foo3() method is called in class A and then foo3 method of class B is called.

Line 5 is printed because, A (superclass) is appended with string (“foo3”) in class B.

  1. The error is coming because, java do not allow the classes to extend from multiple super classes. In the given code, Class N trying to extend multiple class that are String and integer. Therefore, the problem is occurred.

Fixing the problem:

Include 2 super class as member in a new class. After including them, extend that new class with class N.

You can also use the interfaces in order to overcome this problem, but first method is more optimized.


Related Solutions

1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content)...
Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content) { // 3           this.content = content.toUpperCase(); // 4      } // 5      public String toString() { // 6           return content.toUpperCase(); // 7      } // 8      public static void main(String[] args) { // 9           UpperCaseString upperString =              new UpperCaseString("Hello, Cleo!"); // 10           System.out.println(upperString); // 11      } // 12 } // 13 THE FOLLOWING 3 QUESTIONS REFER...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first < second) return "less than"; else if (first == second) return "equal to"; else return "greater than";       }    // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); int first = input.nextInt(); System.out.print("Enter second integer: "); int second = input.nextInt(); System.out.println("The first integer is " + comparison(first, second) + " the...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt()
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt() = 0;     virtual void fight(Monster&);     string getName() const; }; class GiantMonster : public Monster {     protected:         int height;          public:         GiantMonster(string, int, int);         virtual void trample(); }; class Dinosaur : public GiantMonster {     public:     Dinosaur(string, int, int);     void hunt();     void roar(); }; class Kraken : protected GiantMonster {     public:     Kraken(string, int, int);     virtual void hunt();     void sinkShip(); }; Indicate if the code snippets below are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT