• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

please help me

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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];
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic