Create two (2) classes named Car and TestCar.In the Car class, initialize two (2) public static variables: MAKE (String, final) and numCars (int). Set their values to "Toyota" and 0, respectively.Declare two (2) empty String instance variables named chassisNo and model.Create a public constructor with a single parameter named model (String). This constructor should:Increment the value of numCars by one.Set the value of chassisNo based on the result of concatenating the String "ch" and the value of numCars. Syntax: chassisNo = ch + numCars;Use the this keyword to set the instance variable model equal to the value of the parameter model.Display a message saying "Car manufactured".Create two (2) pairs of getter and setter methods that will allow you to get and set the values of the two (2) instance variables: getModel() and setModel(), getChassisNo() and setChassisNo()Add a public toString() method that will return and display the car's make, model, and chassis number in separate lines.In the main method of the TestCar class, display the car's make and the initial number of manufactured cars by accessing them from the Cars class. Example: System.out.println("Manufacturer: " + Car.MAKE);Instantiate two (2) Car objects assigning the values "Camry" and "Veloz", respectively. Example: Car car1 = new Car("Camry");Add two (2) statements to display the data of the two (2) Car objects. Example: System.out.println(car1);Display the total number of cars manufactured by accessing the numCars method.
Question
Create two (2) classes named Car and TestCar.In the Car class, initialize two (2) public static variables: MAKE (String, final) and numCars (int). Set their values to "Toyota" and 0, respectively.Declare two (2) empty String instance variables named chassisNo and model.Create a public constructor with a single parameter named model (String). This constructor should:Increment the value of numCars by one.Set the value of chassisNo based on the result of concatenating the String "ch" and the value of numCars. Syntax: chassisNo = ch + numCars;Use the this keyword to set the instance variable model equal to the value of the parameter model.Display a message saying "Car manufactured".Create two (2) pairs of getter and setter methods that will allow you to get and set the values of the two (2) instance variables: getModel() and setModel(), getChassisNo() and setChassisNo()Add a public toString() method that will return and display the car's make, model, and chassis number in separate lines.In the main method of the TestCar class, display the car's make and the initial number of manufactured cars by accessing them from the Cars class. Example: System.out.println("Manufacturer: " + Car.MAKE);Instantiate two (2) Car objects assigning the values "Camry" and "Veloz", respectively. Example: Car car1 = new Car("Camry");Add two (2) statements to display the data of the two (2) Car objects. Example: System.out.println(car1);Display the total number of cars manufactured by accessing the numCars method.
Solution
// Car class
public class Car {
// Public static variables
public static final String MAKE = "Toyota";
public static int numCars = 0;
// Instance variables
private String chassisNo;
private String model;
// Constructor
public Car(String model) {
numCars++;
this.chassisNo = "ch" + numCars;
this.model = model;
System.out.println("Car manufactured");
}
// Getter and Setter for model
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
// Getter and Setter for chassisNo
public String getChassisNo() {
return chassisNo;
}
public void setChassisNo(String chassisNo) {
this.chassisNo = chassisNo;
}
// toString method
@Override
public String toString() {
return "Make: " + MAKE + "\nModel: " + model + "\nChassis No: " + chassisNo;
}
}
// TestCar class
public class TestCar {
public static void main(String[] args) {
// Display the car's make and the initial number of manufactured cars
System.out.println("Manufacturer: " + Car.MAKE);
System.out.println("Initial number of cars: " + Car.numCars);
// Instantiate two Car objects
Car car1 = new Car("Camry");
Car car2 = new Car("Veloz");
// Display the data of the two Car objects
System.out.println(car1);
System.out.println(car2);
// Display the total number of cars manufactured
System.out.println("Total number of cars manufactured: " + Car.numCars);
}
}
Similar Questions
Complete the Car class byDeclaring the String factory instance variableDeclaring the String model instance variableDeclaring int year instance variableDeclaring string colour instance variableWriting the constructor method with input parameters for factory, model, year and colourWriting the method carAge to display the age of the car (2024 - year)Complete the main method byUsing new to create an instance of Car with the constructor: Car("BMW" , "X5" , 2021 , "Black") Printing the car detailsPrinting the return value for the carAge() method on the Car instance
Develop a Java Application with ‘Vehicle’ Class with vehicle type, vehicle number, engine number, chassis number, top speed, number of passengers, number of gears. Generate a report for the vehicles. Implement the concept using Interfaces.Input:CarHRDQ245652WVC12342H2X1234535kph45
Predict the outputabstract class Vehicle{ abstract void calcPremium(Number N);}interface Insurance{ abstract void calcPremium (Object O);}class Car extends Vehicle implements Insurance{ public void calcPremium (Object O) { System.out.println("Object"); } void calcPremium (Number N) { System.out.println("Number"); } }public class Test{ public static void main(String[] args) { Vehicle a = new Car(); a. calcPremium (new Integer(121)); Insurance b = new Car(); b. calcPremium (new Integer(121)); Car c = new Car(); c. calcPremium (new Integer(121)); }}Select one:a.NumberObjectNumberb.NumberNumberObjectc.Run time errord.Compile time error
Develop a Java Application with ‘Vehicle’ Class with vehicle type, vehicle number, as member. Inherit the classes bicycle, car, bike, and lorry from vehicle class. Consider car and bike only as passenger vehicle. Get inputs engine number, chassis number, top speed, number of passengers, number of gears for Passenger vehicle, and if is not a passenger vehicle, get inputs total amount of load to carry for the respective inherited classes. Generate a report for the vehicles. Implement the concept using inheritance and method overriding. Input 1: Car (VEHICLE TYPE) HRDQ2456 (VEHICLE NUMBER) 52WVC1234 (ENGINE NUMBER) 2H2X1234 (CHASSIS NUMBER) 535kph (SPEED) 4 (No. of PASSENGER) 5 (GEARS) Output1: Car HRDQ2456 52WVC1234 2H2X1234 535kph 4 5
Implement two vehicle classes: Car:The constructor for Car must take two arguments. The first of them is its maximum speed, and the second one is a string that denotes the units in which the speed is given: either "km/h" or "mph".The class must be implemented to return a string based on the arguments. For example, if car is an object of class Car with a maximum speed of 120, and the unit is "km/h", then printing car prints the following string: "Car with the maximum speed of 120 km/h", without quotes. If the maximum speed is 94 and the unit is "mph", then printing car prints in the following string: "Car with the maximum speed of 94 mph", without quotes. Boat:The constructor for Boat must take a single argument denoting its maximum speed in knots.The class must be implemented to return a string based on the argument. For example, if boat is an object of class Boat with a maximum speed of 82, then printing boat prints the following string: "Boat with the maximum speed of 82 knots", without quotes. The implementations of the classes will be tested by a provided code stub on several input files. Each input file contains several queries, and each query constructs an object of one of the classes. It then prints the string representation of the object to the standard output. Constraints1 ≤ the number of queries in one test file ≤ 100 Input Format Format for Custom TestingIn the first line, there is a single integer, q, the number of queries.Then, q lines follow. In the ith of them, there are space-separated parameters. The first of them denotes the vehicle type to be constructed, and the remaining parameters denote the values passed for the constructor of the object.Sample Case 0Sample InputSTDIN Function----- -------2 → number of queries, q = 2car 151 km/h → query parameters = ["car 151 km/h", "boat 77"]boat 77Sample OutputCar with the maximum speed of 151 km/hBoat with the maximum speed of 77 knotsExplanationThere are 2 queries. In the first of them, an object of class Car with the maximum speed of 151 in km/h is constructed, and then its string representation is printed to the output. In the second query, an object of class Boat is constructed with the maximum speed of 77 knots, and then its string representation is printed to the output.Sample Case 1Sample InputSTDIN Function----- --------3 → number of queries, q = 2boat 101 → query parameters = ["boat 101", "car 120 mph", "car 251 km/h"]car 120 mphcar 251 km/hSample OutputBoat with the maximum speed of 101 knotsCar with the maximum speed of 120 mphCar with the maximum speed of 251 km/hExplanationThere are 3 queries. In the first of them, an object of class Boat with the maximum speed of 101 knots is constructed, and then its string representation is printed to the output. In the second query, an object of class Car with the maximum speed of 120 in mph is constructed, and then its string representation is printed to the output. In the third query, an object of class Car with the maximum speed of 251 in km/h is constructed, and then its string representation is printed to the output.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.