Question
To highlight the fact that static variables are stored in a single memory space that is accessed by each instance of a class we are
To highlight the fact that static variables are stored in a single memory space that is accessed by each instance of a class we are going to amend the code created in question 1.
a. In the main method, above the line that displays the total number of cars manufactured add the following line of code:
vehicle2.setMake("Seer");
We are using vehicle2 to amend the value but we could use any instance name.
b. Add two output statements under this line that will use the toString method to display the value of vehicle1 and vehicle2.
The output of the program should look like this:
..
..
..
The vehicle is manufactured by: Seer
The model type is Vision
The chassis number is ch1
The vehicle is manufactured by: Seer
The model type is Edict
The chassis number is ch2
Number of vehicles manufactured: 2
Code from question 1:
package vehicles;
class Vehicle{ public static String MAKE ="Augur"; public static int numVehicles = 0; private String chassisNo, model; public Vehicle(){}; public Vehicle(String model){ this.model = model; numVehicles = numVehicles+1; chassisNo = "Ch"+Integer.toString(numVehicles); System.out.println("Vehicle manufactured"); } public void setChassisNo(String cNo){ this.chassisNo = cNo; } public void setModel(String model){ this.model = model; } public String getChassisNo(){ return chassisNo; } public String getModel(){ return model; } public void ToString(){ System.out.println("The vehicle is Manufactured by : "+ MAKE); System.out.println("The model type is : "+this.getModel()); System.out.println("Number of vehicles manufactured: "+this.getChassisNo()); System.out.println("Number of vehicles manufactured: "+numVehicles); } }
public class TestVehicle{
public static void main(String args[]) {
System.out.println("Manufacturer : "+ Vehicle.MAKE); System.out.println("Number of vehicles manufactured: "+Vehicle.numVehicles); Vehicle vehicle1 = new Vehicle("Vision"); // System.out.println("Number of vehicles manufactured: "+vehicle1.getChassisNo()); vehicle1.ToString(); Vehicle vehicle2 = new Vehicle("Edict"); vehicle2.ToString(); } }
//output
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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