Skip to main content

a little problem whit java code

1 reply [Last post]
Joined: 2006-02-17

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(new Car("Volvo","red",120));

myTable.addCar(new Car("Ford","black",137));

myTable.addCar(new Car("Saab","white",125));

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);
public void addCar(Car newCar){
myCar[nbrOfCar]= newCar;
public Car getCar(int n){
return myCar[n];

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2004-05-20

Note that this forum is not the most appropiate for this question (this is a forum to discuss java desktop technology like Swing or AWT). But the answer is that your carTable class should have a method 'public String toString()' which should read data about every car in the list and add it to a String or StringBuffer (the latter being the best option).
Then if you call System.out.println(myTable) your method will be called since that's what happens whe you write an objet to System.out. You actually override a method in java.lang.Object that every class in java inherits.