dungo
dungo

Reputation: 103

Trouble with referencing attributes in Java

I've been trying to figure out how to pass these attributes from a data class to an executable class correctly but I'm not sure how. Here's the data class:

public class Motor {
        private int cylinders;
        private int hp;
        private String type;
        public Motor(int cylinders, int hp, String type) {
            this.cylinders = cylinders;
            this.hp = hp;
            this.type = type;
        }
        public int getCylinders() {
            return cylinders;
        }
        public int getHp() {
            return hp;
        }
        public String getType() {
            return type;
        }
        public String toString() {
            return "Motor: cylinders=" + this.cylinders + ", hp=" + 
                    this.hp + ", type=" + this.type;
        }
}

Now, in ANOTHER data class I'm supposed to reference Motor and make a bunch of other attributes. I don't have any(noticeable) errors with this one but I figured none of this would make any sense if I didn't post it:

public class Vehicle {
        private String make;
        private String model;
        private int year;
        private double price;
        private Motor motor;
        public Vehicle(String make, String model, int year, double price, Motor motor) {
            this.make = make;
            this.model = model;
            this.year = year;
            this.price = price;
            this.motor = motor;

        }
    public double getPrice() {
            return price;
        }
        public void setPrice(double price) {
            this.price = price;
        }
        public String toString() {
            return "Vehicle make=" + this.make + ", model=" +
                    this.model + ", year=" + this.year + 
                    ", price=" + this.price + ", motor=" +
                    this.motor;
        }
    }

This where the real problem is, when I created a Vehicle object in TestVehicle I get an error when I try to add the values for cylinder, hp and type from back in Motor:

public class TestVehicle {

    public static void main(String[] args) {
        Vehicle v1 = new Vehicle("Toyota", "Corolla", 2015, 15999.0, 7, 300, "Gas");
        System.out.println(v1.toString());

    }

}

The error I get when I try to execute it is: Exception in thread "main" java.lang.Error: Unresolved compilation problem: The constructor Vehicle(String, String, int, double, int, int, String) is undefined

Thanks a lot!

Upvotes: 0

Views: 142

Answers (3)

user1841944
user1841944

Reputation: 66

This is your constructor, you expecting String, String, int, double and Motor.

 public Vehicle(String make, String model, int year, double price, Motor motor) {
        this.make = make;
        this.model = model;
        this.year = year;
        this.price = price;
        this.motor = motor;

    }

Your motor has a constructor like:

public Motor(int cylinders, int hp, String type) {
            this.cylinders = cylinders;
            this.hp = hp;
            this.type = type;
        }

So if you want to pass motor parameters direct as int, int and String to Vehicle constructor you need a Vehicle constructor which accept String, String, int, double, int, int, String.

you can add a second consturctor to you Vehicle class like:

public Vehicle(String make, String model, int year, double price, int cylinders, int hp, String type) {
    this.make = make;
    this.model = model;
    this.year = year;
    this.price = price;
    this.motor = new Motor(cylinders, hp, type);
}

But this is not a good style, if something changes you might forget to change both constructors. Better you chaining constructors.

public Vehicle(String make, String model, int year, double price, int cylinders, int hp, String type) {
        this.(make, model, year, price, new Motor(cylinders, hp, type))
    }

Upvotes: 0

ABHISHEK RANA
ABHISHEK RANA

Reputation: 327

There is some problem in Parameter passing Constructor of Vehicle. Please try the below code, it is working correctly

class Motor {
        private int cylinders;
        private int hp;
        private String type;
        public Motor(int cylinders, int hp, String type) {
            this.cylinders = cylinders;
            this.hp = hp;
            this.type = type;
        }
        public int getCylinders() {
            return cylinders;
        }
        public int getHp() {
            return hp;
        }
        public String getType() {
            return type;
        }
        public String toString() {
            return "Motor: cylinders=" + this.cylinders + ", hp=" + 
                    this.hp + ", type=" + this.type;
        }
}


class Vehicle {
        private String make;
        private String model;
        private int year;
        private double price;
        private Motor motor;
        public Vehicle(String make, String model, int year, double price, Motor motor) {
            this.make = make;
            this.model = model;
            this.year = year;
            this.price = price;
            this.motor = motor;

        }
    public double getPrice() {
            return price;
        }
        public void setPrice(double price) {
            this.price = price;
        }
        public String toString() {
            return "Vehicle make=" + this.make + ", model=" +
                    this.model + ", year=" + this.year + 
                    ", price=" + this.price + ", motor=" +
                    this.motor;
        }
    }

class TestVehicle {

    public static void main(String[] args) {
        Motor m1 = new Motor(7,300,"Gas");
        Vehicle v1 = new Vehicle("Toyota", "Corolla", 2015, 15999.0,m1);
        System.out.println(v1.toString());

    }

}

Upvotes: 0

SatyaTNV
SatyaTNV

Reputation: 4135

Exception in thread "main" java.lang.Error: Unresolved compilation problem: The constructor Vehicle(String, String, int, double, int, int, String) is undefined

 Vehicle v1 = new Vehicle("Toyota", "Corolla", 2015, 15999.0, 7, 300, "Gas");

change to

Motor motor = new Motor( 7, 300, "Gas");
Vehicle v1 = new Vehicle("Toyota", "Corolla", 2015, 15999.0, motor);

Check the constructors.

Upvotes: 4

Related Questions