汽车class程序

Car class program

下面是驱动class我是给的,不让edit/change这个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());
   }
}

这是我创建的 Car.java class 文件

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;
}

}   

我遇到的问题: 尝试合并计数以显示汽车总数 (4) 由于驱动程序 class 为空,第一辆汽车对象 car1 和最后一辆汽车对象 car4 未显示,我无法更改测试仪 class,只能更改我的汽车 class。

应该是什么样的输出

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

编辑 我进行了向我推荐的更改,但由于某种原因,我收到有关 String 到 Int 转换的错误消息。以下是我所做的更改和收到的错误消息。

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);
                     ^

问题出在你的default constructor:

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

一切都设置为 0null,这意味着您的实际输出(尽管在撰写本文时您尚未发布)可能类似于:

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

或类似的东西。那是因为你把所有的成员变量都设置为 0 或者 null.

尝试将默认构造函数更改为如下内容:

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

这样,当您实例化(即创建)一个没有参数的对象时(如汽车 1 和汽车 4 所做的那样),它会自动为您填充这些值。

编辑: 正如 Mohd Akbar 指出的那样,您的代码还有另一个问题,那就是 number 是一个 instance variable, not a static one.

您可能希望将数字更改为更像 this example,因此:

 static int number = 0;

并更改您的构造函数以匹配它:

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

而不是我原来的建议number = 4;

编辑 2: 您可能遇到的另一个问题是您的参数在您的其他构造函数中乱序:

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

你的另一个 class(给你的那个)需要这样的参数:

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

这应该可以解决您遇到的其他问题。

你需要做的第一件事是让变量'number'成为一个静态变量,这样它就被class的所有对象共享,而不是为每个对象创建一个副本. 并且您需要在两个构造函数中递增 'number' 而不仅仅是参数化构造函数。

private static int number;

并且在两个构造函数中你都需要做

number++;

您创建的参数化构造函数是正确的,'Chipster'的默认构造函数是正确的。

希望,我已经说清楚了。