• Post Reply Bookmark Topic Watch Topic
  • New Topic

please help me  RSS feed

 
peterx peter
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
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{
private Car[]myCar;
private int nbrOfCar;
public CarTable() {
myCar=new Car[100];
nbrOfCar=0;}
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];
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On way is to add a toString() method to the CarTable class. This method must return a String object. If you create this string so that it formats the output correctly, the rest of your code should work fine. You probably also need to add a toString() method to the Car class to format the output for each Car object.

HTH

Layne
[ November 02, 2004: Message edited by: Layne Lund ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't read? You have a problem with the english language?

You asked the exact same question 2 days ago

http://www.javaworld.com/javaforums/showflat.php?Cat=&Board=javabeginner&Number=13010&page=0&view=collapsed&sb=5&o=&fpart=1
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!