Search code examples
javaobjectmethodsaccessormutators

Car class program


Below is the driver class I was given and I am not allowed to edit/change this class

public class HW2tester
{
   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());
   }
}

This is the Car.java class file I created

public class Car
{
   private int year;
   private String brand;
   private int price;
   private int number;

public Car()
{
   year = 0;
   brand = null;
   price = 0;
   number = 0;
}

public Car( int y, String b, int p)
{
   number++;
   year = y;
   brand = b;
   price = p;
}

public void setYear( int y)
{
   year = y;
}

public void setBrand( String b)
{
   brand = b; 
}

public void setPrice( int p)
{
   price = p;
}

public int getYear()
{
   return year;
}

public String getBrand()
{
   return brand;
}

public int getPrice()
{
   return price;
}

public int getNumber()
{
   return number;
}

}   

Problems I am having: Trying to incorporate count to display total numbers of cars (4) First car object car1 and last car4 are not displaying due to driver class being blank and I cannot change the tester class, only my car class.

what output is supposed to look like

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

EDIT Ive made the changes recommended to me but I am getting an error message about String to Int conversion for some reason. Here are the changes I have made and the error message I am getting.

public class Car
{
   int year;
   String brand;
   int price;
   static int number;

public Car()
{
   year = 2005;
   brand = "Chevy";
   price = 3000;
   number++;
}

HW2tester.java:6: error: incompatible types: String cannot be converted to int
      Car car2 = new Car("Ford", 2013, 20000);
                         ^
HW2tester.java:7: error: incompatible types: String cannot be converted to int
      Car car3 = new Car("Audi", 2012, 25000);
                     ^

Solution

  • The problem lies with your default constructor:

    public Car()
    {
       year = 0;
       brand = null;
       price = 0;
       number = 0;
    }
    

    Everything is set to 0 or null, meaning your actual output (although at the time of this writing you haven't posted it yet) is probably something like:

    This car is , year 0, price 0
    This car is Ford, year 2011,price 22000
    This car is Audi, year 2012, price 25000
    This car is Cadillac, year 0, price 0
    The total car number is: 0
    The total car number is: 4
    The total car number is: 4
    The total car number is: 0
    

    Or something to that effect. That's because you have set all the member variables to 0 or null.

    Try changing your default constructor to something like this:

    public Car()
    {
       // This car is Chevy, year 2005, price 3000
       year = 2005;
       brand = "Chevy";
       price = 3000;
       number = 4;
    }
    

    That way, when you instantiate (i. e. create) an object with no parameters (as done with car 1 and car 4), it will fill in those values for you automatically.

    EDIT: As Mohd Akbar pointed out, you also have another problem with your code, and that is number is an instance variable, not a static one.

    You will probably want to change number to be more like this example, as so:

     static int number = 0;
    

    And change your constructor to match it:

    public Car()
    {
       // This car is Chevy, year 2005, price 3000
       year = 2005;
       brand = "Chevy";
       price = 3000;
       number++;
    }
    

    Instead of my original suggestion of number = 4;

    Edit 2: Another problem you might have is your arguments are out of order on your other constructor:

    public Car( int y, String b, int p)
    

    Your other class (the one you were given) expects the arguments like this:

    public Car(String b, int y, int p)
    

    And that should fix the other problem you are having.