|
Hi!
I have a little problem
The program is OK, but I can not print out:
QUESTION is how can I print out (class Test).
how can I print out "myTable" and the out put must be
like this:
Audi blue 180
Volvo red 120
Ford black 137
Saab white 125
---------------
calss Test{
Car c1 = new Car("Audi", "blue", 180);
Car c2,c3,
CarTable myTable= new CarTable();
myTable.addCar(c1);
myTable.addCar(new Car("Volvo","red",120));
myTable.addCar(new Car("Ford","black",137));
myTable.addCar(new Car("Saab","white",125));
System.out.println(myTable);//???????
-------------
class Car{
public Car(String model, String color, double power){
public String getModel()
public String getColor()
public double getPower()
public void setModel(String model)
public void setColor(String color)
public void setPower(double power)
------------------
class CarTable{
public void addCar(String model, String color, int power){
myCar[nbrOfCar] = new Car(model, color, power);
nbrOfCar++;
}
public void addCar(Car newCar){
myCar[nbrOfCar]= newCar;
nbrOfCar++;
}
public Car getCar(int n){
return myCar[n];
|
|
|
Hi
My advice would be to add some print methods to your table class. However if you only want to amend the test class try this:
class Test
{
Car c1 = new Car("Audi", "blue", 180);
Car c2,c3,
CarTable myTable= new CarTable();
myTable.addCar(c1);
myTable.addCar(new Car("Volvo","red",120));
myTable.addCar(new Car("Ford","black",137));
myTable.addCar(new Car("Saab","white",125));
for(int i=0; i<numCars; i=i+1)
{
System.out.println(myTable.getCar().getModel()+" "+
myTable.getCar().getColor"+
myTable.getCar().getPower
}
}
Note the change is the for-loop at the end the variable numCars has to be the number of entries that are in the CarTable (in your example 3).
|
|
|
|
|
|
|
|