Question
Suppose I have the following classes: public class Vehicle { private int numberOfWheels = 0; private String cargoCapacity; protected String color = blue; Vehicle(){ this.cargoCapacity
Suppose I have the following classes: public class Vehicle { private int numberOfWheels = 0; private String cargoCapacity; protected String color = "blue"; Vehicle(){ this.cargoCapacity = "small"; } Vehicle(int w, String c){ this.numberOfWheels = w; this.cargoCapacity = c; } public int getNumberOfWheels(){ return this.numberOfWheels; } public String getCargoCapacity(){ return this.cargoCapacity; } @Override public String toString(){ return "Vehicle: " + numberOfWheels + " " + cargoCapacity; } public void steer(String d){ if(d.equals("left")){ System.out.println("Turning left."); } else {
System.out.println("Turning right."); } } public void operate(){ System.out.println(this.toString()); if(this instanceof Car){ ((Car)this).drive(); }else{ System.out.println("No operation."); } } } public class Car extends Vehicle { private boolean isElectric = false; Car(){ } Car(boolean e, String cargo){ super(4, cargo); this.isElectric = e; this.color = "green"; } @Override public String toString(){ return this.color + " Car: " + getNumberOfWheels() + " " + getCargoCapacity(); } public boolean getIsElectric(){ return this.isElectric; } public void drive(){ System.out.println("Vroom!"); } } 1. Suppose I have the code: Car c = new Car(); System.out.println(c.getNumberOfWheels());
1. What will be printed to the command line? Why?
2. Suppose I have the code: Vehicle v = new Car(); v.drive(); What will be the result of this code? Explain. 3. Write two lines of code, the first to create Car object with 4 wheels and small cargo capacity, and the next to print out if it is electric. 4. When I create car with the constructor: Car c = new Car(); What constructors are called, in what order?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Heres a breakdown of the scenarios mentioned with the given classes 1 Code Car c new Car Systemoutpr...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started