0

Good evening.

I have an ArrayList and I am trying to print the corresponding data for each index. However, the output is incorrect. For example, when I write... System.out.println("The third model in the cars list is: " + (cars.get(2))); ....I would expect to see "The third model in the cars list is: Ford Focus" ...However, instead, it prints "The third model in the cars list is: week3src.Car@5e265ba4". My apologies, but as a novice, I am unsure how to correct this and would apreciate some feedback as to the nature of this error and how to correct it. Thanks in advance.

I am using 4 classes in total. Here is my source code in full...

// instance variables
    private String model;
    private int tankSize;
    private double manfMPG;
    private String ownerName;


    final double GPL = 0.22; // there are 0.22 gallons per litre


    // parameterised constructor
    public Car(String m, int t, double mpg, String owner)
    {
        model = m;
        tankSize = t;
        manfMPG = mpg;
        ownerName = owner;
    }
    
    public String getOwnerName() {
        return ownerName;
        
    }
    //
    public void setOwnerName(String ownerName) {
        this.ownerName = ownerName;
    }

    // default constructor
    public Car()
    {
        model = "";
        tankSize = 0;
        manfMPG = 0.0;
        ownerName = "";
    }

    // setters and getters
    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getTankSize() {

        return tankSize;
    }

    public void setTankSize(int tankSize) {
        this.tankSize = tankSize;
        
    }

    public double getManfMPG() {
        return manfMPG;
    }

    public void setManfMPG(double manfMPG) {
        this.manfMPG = manfMPG;
    }

    public double getEstimateDistance() {
        // there are 0.22 gallons per litre

        if (tankSize >= 4) {
            System.out.println("inefficient fuel use");
        }
        else if (tankSize <= 2) {
            System.out.println("good fuel consumption");
        }
        else {
            System.out.println("Average consumer");
        }
        return tankSize * manfMPG * GPL;

    }

}

ArrayList<Car> cars = new ArrayList<>();

        
        cars.add(new Car("Ferrari", 4, 4.0, "Kieran David Nock"));
        cars.add(new Car("VW Golf", 2, 9.0, "Alan Mathison Turing"));
        cars.add(new Car("Ford Focus", 3, 7.0, "Jurgen Norbert Klopp"));
        cars.add(new Car("Nissan Juke", 3, 9.0, "Martin Luther King"));
        cars.add(new Car("Porsche 911", 4, 3.5, "Barack Hussain Obama"));
            
System.out.println("The third model in the cars list is: " + (cars.get(2)));
 
Kieran Nock
  • 15
  • 1
  • 5
  • Add a `toString()` method to your `Car` class which returns whatever string representation for a car which you want. See the duplicate link for more information. – Tim Biegeleisen Nov 03 '20 at 02:14
  • You are trying to print the object itself. You should specifies the data members related to the object like this: System.out.println (cars.get(2).model + " "+cars.get(2).ownerName); – husin alhaj ahmade Nov 03 '20 at 02:20

0 Answers0