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).
I mean How can I write toString()methode.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
I mean this toString() in class car must print out
every thing both in class car and class carTable.
but toString methode is just in class Car not in Class carTable.
---------------
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)
public String toString()//?????????????
------------------
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];

Dani AI

Generated

: System.out.println(myTable) calls myTable.toString() under the hood. As hinted, you need two pieces: a toString() in Car that returns a single-line representation of one car, and a toString() in CarTable that builds a multi-line string by concatenating each car’s toString().

A simple Car.toString() (override and format the fields in the order you want):

@Override
public String toString() {
    return String.format("%s %s %.0f", model, color, power);
}

CarTable.toString() should loop over the stored cars (use your getCar(i) and nbrOfCar or an internal List) and append each car’s string plus a newline. Using StringBuilder is efficient:

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < nbrOfCar; i++) {
        Car c = getCar(i);
        if (c != null) sb.append(c.toString()).append(System.lineSeparator());
    }
    return sb.toString();
}

Notes and tips: include @Override so the compiler checks your signature; guard against null entries if the array isn’t full; prefer ArrayList<Car> to avoid manual counters; and keep toString() side-effect free. If you still see something like ClassName@1a2b3c, it means toString() hasn’t been overridden on the object you printed. For background see Java’s Object.toString() and the StringBuilder utility (Object docs, StringBuilder docs).

Recommended Answers

All 2 Replies

>but toString methode is just in class Car not in Class carTable.
So define it for carTable.

Hi again!
Sorry!
I wrote wrong about toString. I mean How can I write toSting methode.
It make no difference where we write the toString methode, but important is how can I print out the class Test.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.