In: Computer Science
Create a class called Car (Car.java). It should have the following private data members:
• String make
• String model
• int year
Provide the following methods:
• default constructor (set make and model to an empty string, and set year to 0)
• non-default constructor Car(String make, String model, int year)
• getters and setters for the three data members
• method print() prints the Car object’s information, formatted as follows:
Make: Toyota
Model: 4Runner
Year: 2010
public class Car {
}
Complete the following unit test for the class Car described above. Make sure to test each public constructor and each public method, print the expected and actual results.
// Start of the unit test of class Car
public class CarTester {
public static void main() {
// Your source codes are placed here
return;
}
}
import java.util.*;
import java.lang.*;
class Car{
//Declare a private variables.
private String make;
private String model;
private int year;
//Default constructor.
public Car(){
this.make = "";
this.model = "";
this.year = 0;
}
//Constructor with user-defined values.
public Car(String make , String model , int year) {
this.make = make;
this.model = model;
this.year = year;
}
//Getter and Setter method for all of them.
public void setMake(String make) {
this.make = make;
}
public void setModel(String model) {
this.model = model;
}
public void setYear(int year) {
this.year = year;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
//Print function for print object in appropriate order.
public void print(){
System.out.println("Make: " + make);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}
}
class CarTester {
public static void main(String[] args) {
//Testing code.
Car test1 = new Car();
test1.setMake("toyota");
test1.setModel("4tuner");
test1.setYear(2010);
System.out.println(test1.getMake());
System.out.println(test1.getModel());
System.out.println(test1.getYear());
Car test2 = new Car("Audi" , "a4" , 2012);
test2.print();
}
}
OUTPUT :-