So i posted this homework problem awhile back but my professor just decided to let us know that we must use a counter. and i am having trouble writing a constructor that doesnt conflict with another.
the home work problem is as follows:
-In this homework, you will write a Car.java file in order to make HW1tester.java (see the attachment) file work. And the output is required to be exactly
This car is Chevy, year 2005, price 3000
This car is Ford, year 2011, price 22000
This car is Audi, year 2012, price 25000
This car is Cadillac, year 2005, price 3000
The total car number is: 4
The total car number is: 4
The total car number is: 4
The total car number is: 4
-
HW1tester is as follows:
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()); } }
and my class is as follow:
public class Car { private String brand; private int year; private int price; private static int number = 0; public Car() { number += 1; } public static int getNumber() { return number; } public int decrease() { number--; return number; } public Car(String carBrand, int carYear, int carPrice) { brand = carBrand; year = carYear; price = carPrice; } public Car() { brand = "Chevy"; year = 2005; price = 3000; } public void setBrand(String b) { brand = b; } public void setYear(int y) { year = y; } public void setPrice(int p) { price = p; } public String getBrand() { return brand; } public int getYear() { return year; } public int getPrice() { return price; } }
the problem is where i tried creating a constructor for the count, but clearly it conflicts with the constructor for the chevy. not sure how to fix this.
thanks.