汽车类计划

问题描述 投票:-1回答:2

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

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

编辑我已经向我推荐了更改,但由于某种原因,我收到有关String to 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);
                     ^
java object methods accessor mutators
2个回答
0
投票

问题在于你的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

或者那种效果。那是因为你已经将所有成员变量设置为0null

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

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,而不是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)

你的另一个类(你给的那个)期望像这样的参数:

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

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


1
投票

您需要做的第一件事是使变量'number'成为一个静态变量,以便它由类的所有对象共享,而不是为每个对象创建一个副本。并且您需要在两个构造函数中增加'number',而不仅仅是参数化构造函数。

private static int number;

在你需要的构造函数中

number++;

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

希望,我已经说清楚了。

© www.soinside.com 2019 - 2024. All rights reserved.