-1

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);
                     ^
  • Your default constructor is setting everything to blank. That could be why nothing shows up. There is no data in each of those vars. What is your expected and actual result? –  Feb 26 '19 at 17:57
  • What is your expected output? – Mohd Akbar Feb 26 '19 at 18:01
  • Number is an instance variable. It is initialised to 0 for every instance. Look at making it static: https://en.m.wikibooks.org/wiki/Java_Programming/Keywords/static. Aa Car 1 and Car 4 take no constructor args you need to set defaults in the class itself i.e. Chevy, 2005, 2000 – Alan Hay Feb 26 '19 at 18:01
  • just updated expected output. – Anthony Berrios Feb 26 '19 at 18:08
  • You're sure you want that no-argument constructor? A car without brand and year doesn't make sense to me. – MC Emperor Feb 26 '19 at 18:12
  • Well as he says he can't change HW2tester then there's not really an option. – Alan Hay Feb 26 '19 at 18:21

2 Answers2

1

The first thing you need to do is to make the variable 'number' a static variable so that it is shared by all the objects of the class and not a copy is created for each object. And you need to increment 'number' in both the constructors and not only the parameterized constructor.

private static int number;

And in both the constructor you need to do

number++;

The parameterized constructor created by you is correct and the default constructor by 'Chipster' is correct.

Hope , I have made myself clear.

Mohd Akbar
  • 111
  • 5
0

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.

  • If I had a guess, it's because your other constructor: `public Car( int y, String b, int p)` has the arguments out of order. Try `public Car( String b, int y, int p)` instead. –  Feb 26 '19 at 21:11