Creating a car class in java
Emily Wong
Okay, I need to write code that makes this file
public class HW1tester
{ public static void main(String[] args) { Car car1 = new Car(); Car car2 = new Car("Ford", 2013, 20000); Car car3 = new Car("Audi", 2012, 25000); Car car4 = new Car(); car2.setPrice(22000); car2.setYear(2011); car4.setBrand("Cadillac"); System.out.println("This car is " + car1.getBrand() + ", year " + car1.getYear() + ", price " + car1.getPrice()); System.out.println("This car is " + car2.getBrand() + ", year " + car2.getYear() + ", price " + car2.getPrice()); System.out.println("This car is " + car3.getBrand() + ", year " + car3.getYear() + ", price " + car3.getPrice()); System.out.println("This car is " + car4.getBrand() + ", year " + car4.getYear() + ", price " + car4.getPrice()); System.out.println("The total car number is: " + car1.getNumber()); System.out.println("The total car number is: " + car2.getNumber()); System.out.println("The total car number is: " + car3.getNumber()); System.out.println("The total car number is: " + car4.getNumber()); }
}So far I have this, but I'm not sure what the hell I'm doing wrong.
public class Car
{ private int yearModel; private String brand; private int priceModel; private int numberModel; public Car(String b, int year, int price, int number) { yearModel = year; brand = b; priceModel = price; numberModel = number; } public int getYear() { return yearModel; } public String getBrand() { return brand; } public int getPrice() { return priceModel; } public int getNumber() { return numberModel; } public void setYear(int year) { yearModel = year; } public void setBrand(String carBrand) { brand = carBrand; } public void setPrice(int price) { priceModel = price; public void setNumber(int number) { numberModel = number; }
}Everytime I run the first code right now it just gives me errors on car1, car2, etc I just can't seem to see what I'm doing wrong at all, I hope somebody can help me out. By the way, I can't make ANY changes to HW1tester.
5 Answers
When creating a new object (car1, car2, etc.), you're not passing in enough variables. Your constructor requires 4 and you're giving, at most, 3 variables when trying to construct a new car object.
8You have created parameterized Constructor i.e. public Car(String b, int year, int price, int number)
So when you are trying to create object for the same like, Car car1 = new Car(); then it won't be possible. Because in this you are trying to call default constructor. Which is not present in the class.
While creating object you need to pass 4 arguments.
Moreover, in Car car2 = new Car("Ford", 2013, 20000); You are passing 3 arguments which doesn't match with the constructor.
To create object of class Car, you need to do something like,
Car c = new Car('Volvo', 2014, 25000, 1234);
you need to write overloaded constructors with different parameter sets. when you call new Car(), java is looking for a ctor with no params, new Car("audi", 2013, 25000) one with 3 params etc.
in your Car.java file:
public Car() {}then you can set the instance variables with their getters and setters (until you do, their values will be null).
if you want, you can define more, but their signatures have to be different. eg:public Car(String b, int year) { ... } and public Car(String b, int price) {...} wont work, because they have the same signature.
In class Car, you have a constructor that has 4 parameters. However in the main class, you create a Car with 0 or 3 parameters. In other to run the code, you have to add 2 more constructor, one with 0 parameter, and one with 3 parameters.
public Car() {
}
public Car(String b, int year, int price) { yearModel = year; brand = b; priceModel = price;
} public Car() {
}
public Car(String b, int year, int price,) { yearModel = year; brand = b; priceModel = price;
}In that case you have a constructor that has 4 parameters.