Java 汽车类课程

Java 汽车类课程,java,object,methods,accessor,mutators,Java,Object,Methods,Accessor,Mutators,下面是给我的驱动程序类,我不允许编辑/更改此类 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(); c

下面是给我的驱动程序类,我不允许编辑/更改此类

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类文件

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) 第一辆车对象car1和最后一辆车4未显示,因为驾驶员类别为空,我无法更改检测仪类别,只能更改我的汽车类别

输出应该是什么样子的

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
编辑 我做了推荐给我的更改,但由于某种原因,我收到了一条关于字符串到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);
                     ^

问题在于您的:

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

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;
}
public Car(String b, int y, int p)
这样,当您实例化(即创建)一个没有参数的对象时(如CAR1和CAR4),它会自动为您填充这些值

编辑:正如Mohd Akbar指出的那样,您的代码还有另一个问题,那就是
number
是一个,而不是一个

您可能希望将数字更改为更像,如下所示:

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

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)
您的另一个类(提供给您的类)需要如下参数:

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

这将解决您遇到的另一个问题。

您需要做的第一件事是将变量“number”设置为静态变量,以便该类的所有对象都共享它,而不是为每个对象创建副本。 您需要在构造函数中增加'number',而不仅仅是参数化构造函数

private static int number;
在构造函数和

number++;
您创建的参数化构造函数是正确的,“Chipster”的默认构造函数是正确的


希望,我已经讲清楚了。

您的默认构造函数正在将所有内容设置为空。这可能就是为什么什么都没有出现。每个变量中都没有数据。预期和实际结果是什么?预期输出是什么?数字是一个实例变量。对于每个实例,它都初始化为0。看看如何使其成为静态:。Aa Car 1和Car 4不使用构造函数参数您需要在类本身设置默认值,即雪佛兰,2005,2000刚刚更新了预期输出。您确定要使用无参数构造函数吗?没有品牌和年份的汽车对我来说没有意义。如果我有一个猜测,那是因为你的另一个构造函数:
公共汽车(int y,String b,int p)
的参数顺序不正确。尝试使用
公共汽车(字符串b,int y,int p)