#Arrays?

1 messages · Page 1 of 1 (latest)

inland swallow
#

Why do I get an error in my array:

    public static void main(String[] args) {
        // Create an array of 4 vehicles
        Vehicle[] myVehicles = new Vehicle[4];

        // Populate each entry of the array with a vehicle
        myVehicles[0] = new Vehicle("Honda", 4, 6f, true);
        myVehicles[1] = new Vehicle("Bicycle", 2, 0f, true);
        myVehicles[2] = new Vehicle("Motorcycle", 3, 2f, false);
        myVehicles[3] = new Vehicle("Toyota", 4, 4f, false);
        // 3 because it starts at 0, so only need 0,1,2,3```
```public class Vehicle {
    // Instance variables
    private String Type; // type of vehicle
    private int Number_of_Wheels; // number of wheels
    private float Engine_Size; // the size of the engine
    private boolean Gas_Electric; // USE BOOLEAN
}```
tidal lagoonBOT
#

This post has been reserved for your question.

Hey @inland swallow! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

half ember
#

what's the error ? Do you have a constructor matching your passed in arguments in your vehicle class ?

inland swallow
#

'Vehicle()' cannot be applied to '(java.lang.String, int, float, boolean)'

#

our teacher said we didnt need a constructor

half ember
#

then he probably said to use getters and setters

#

otherwise there is no way to set the values of the fields in your vehicle class

inland swallow
#
        return Type;
    }
    public int getNumber_of_Wheels() {
        return Number_of_Wheels;
    }
    public float getEngine_Size() {

        return Engine_Size;
    }
    public boolean getGas_Electric() {
        return Gas_Electric;
    }
    public void setType(String newType) {
        Type = newType;
    }
    public void setNumber_of_Wheels(int newNumber_of_wheels) {
        Number_of_Wheels = newNumber_of_wheels;
    }
    public void setEngine_Size(float newEngine_Size) {
        Engine_Size = newEngine_Size;
    }
    public void setGas_Electric(boolean newGas_Electric) {
        Gas_Electric = newGas_Electric;
    }```
half ember
#

it's a little clunky but you will have to create a new vehicle without any arguments new Vehicle() and then set the values of the fields manually using your getters.

ember vapor
#

like this ```java
myVehicles[0] = new Vehicle();
myVehicles[0].setXXX()

inland swallow
#

would my way work with a constructor?

half ember
#

yes it would

pearl light
#

Also note that the record types enable to just give the list of fields without having to define a constructor that sets them. Much more straightforward

inland swallow
#

yea idk what that means but okay!