Ok, I'll try to keep it simple
This is the Car class and a class to test its methods.
Code:
/**
* Class for testing the Car class.
* This class doesn't need a constructor, but I've supplied
* a default (empty) constructor here
*/
class CarClassTester {
public CarClassTester() {} // empty constructor
public void runTest() {
// make some cars
Car aCar=new Car("Ford","Focus",2003);
Car anotherCar=new Car("Volvo","V70",2004);
// get the make, model and year of one of the cars
// and print it out.
String mk=aCar.getMake();
String md=aCar.getModel();
int yr=aCar.getYear();
System.out.println("Car data, Make: "+mk+", Model: "+md+", Year: "+yr);
// use the toString method of both car objects.
System.out.println(anotherCar.toString());
System.out.println(aCar.toString());
}
// the main method, this is where the program execution starts.
// the definition of this method requires the String array parameter
// but that array is not used here.
public static void main(String[] args) {
// create a CarClassTester object
CarClassTester cct = new CarClassTester();
// invoke the CarClassTester objetcs only method.
cct.runTest();
// and we are finished ....
}
}
/**
* Class for storing basic car information
*/
class Car {
private String make = null;
private String model = null;
private int year = -1;
// constructor
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
// the toString method, delivers a string explainig in
// short what the object contains
public String toString() {
return "Make: " + make + ", Model: " + model + ", Year: " + year;
}
// get methods
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
}