Question: Consider the following two classes. class Vehicle{ private int numMiles; public Vehicle(int nm){ numMiles = nm; } public String toString(){ return numMiles: + numMiles

Consider the following two classes.

class Vehicle{

private int numMiles;

public Vehicle(int nm){

numMiles = nm;

}

public String toString(){

return "numMiles: " + numMiles + " ";

}

}

class Bus extends Vehicle{

private int numPassengers;

public Bus(int np, int nm){

super(nm);

numPassengers = np;

}

public String toString(){

/* missing code */

}

}

Assume that the following code segment appears in a client program.

Bus bus = new Bus(40, 300);

System.out.println(bus);

Furthermore, assume that the following output should be the result of executing the code segment.

numMiles: 300

numPassengers: 40

Which of the following can be used to replace the /* missing code */ in method toString so that the desired output is achieved?

(A) return super.toString() + "numPassengers: " + numPassengers;

(B) super.toString();

return "numPassengers: " + numPassengers;

(C) Vehicle.toString();

return "numPassengers: " + numPassengers;

(D) return "numMiles: " + numMiles + " " + "numPassengers: "

+ numPassengers;

(E) None of the above. It is not possible to achieve that output without a Vehicle object.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!