这是我的课
package javaapplication1;
public class Car {
private String model;
private String reg;
private String colour;
private double litreEngine;
//Default Constructor
public Car() {
model = null;
reg = null;
colour = null;
litreEngine = -1;
}
public Car(String startModel, String startReg, String startColour, double startLitreEngine) {
model = startModel;
reg = startReg;
colour = startColour;
litreEngine = startLitreEngine;
}
//Prints out Car attributes
public void print() {
System.out.println("Model:" + model + "Reg:" + reg + "Colour:" + colour + "litreEngine:" + litreEngine);
}
public String toString() {
return ("Model:" + model + " Reg: " + reg + " Colour: " + colour + " Litre Engine: " + litreEngine);
}
//Get and set Methods
public String getModel() {
return model;
}
public String getReg() {
return reg;
}
public String getColour() {
return colour;
}
public double getLitreEngine() {
return litreEngine;
}
public void setModel(String new_model)
{
model = new_model;
}
public void setReg(String new_Reg)
{
reg = new_Reg;
}
public void setnumberOfHoursPerWeek(String new_colour)
{
colour = new_colour;
}
public void setcompulsory(double new_litreEngine)
{
litreEngine = new_litreEngine;
}
}
这是我的测试课
package javaapplication1;
import java.util.Scanner;
public class CarTestApp {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Car[] values = new Car[4];
int i = 0;
String Continue = "Y";
String Stop = "N";
String answer = null;
//Car c = new Car("VW Golf", "01D1234", "Red", 1.6);
//Car d = new Car("Mazda MX-5", "03WW324", "Olive", 1.6);
//Car e = new Car();
// Creates car details from user input.
do {
System.out.println("Enter model details: ");
String model = in.nextLine();
System.out.println("Enter reg details: ");
String reg = in.nextLine();
System.out.println("Enter colour details: ");
String colour = in.nextLine();
System.out.println("Enter engine details:(Number) ");
String line = in.nextLine();
double litreEngine = Double.parseDouble(line);
//double litreEngine = in.nextDouble();
Car one = new Car(model, reg, colour, litreEngine);
values[i] = one;
i++;
if (i < values.length) {
System.out.println("Do you want to add another car?(Y/N)");
answer = in.nextLine();
}
} while (answer.equalsIgnoreCase(Continue) && i < values.length);
System.out.println("Thank you for inputting this information");
System.out.println(values[0]+"\t"+values[1]+"\t"+values[2]+"\t"+values[3]);
}
}