【发布时间】:2017-03-05 16:28:48
【问题描述】:
我的 Java 编程课程有一个项目。
说明是我们要创建一个简单的类和一个测试器类,并且该类必须包含一个Default构造函数;具有三个参数(品牌、型号和价格)的参数化构造函数;访问器方法调用 getMake() 来返回 make;访问器方法调用getModel()返回模型;访问器方法调用 getPrice() 返回价格; Mutator方法setMake(String newMake)设置make; Mutator方法setModel(String newModel)设置模型;和一个 Mutator 方法 setPrice( double newPrice ) 来设置价格..
我已经创建了我的类和测试程序,我的类可以完美编译。 当我尝试运行它时,虽然得到了没有主方法的错误。现在,我按照教授的示例进行了测试程序,但我遇到了几个错误。如果有人能给我一个正确方向的指针,我将不胜感激。
我的问题是:如何实现我的测试程序?我需要创建一个 zip 文件吗?我试过这样做,但似乎没有多大帮助......
以下是我的课程代码:
public class Automobile
{
private String make;
private String model;
private double price;
public Automobile()
{
make = "Lexus2017";
model = "RX";
}
public Automobile(String initMake, String initModel, double initPrice)
{
make = initMake;
model = initModel;
price = initPrice;
}
public String getMake()
{
return make;
}
public String getModel()
{
return model;
}
public double getPrice()
{
return price;
}
public void setMake(String newMake)
{
make = newMake;
}
public void setModel(String newModel)
{
model = newModel;
}
另外,下面是我的测试类(有很多错误的):
public class AutomobileTester
{
public static void main(String[] args)
{
Automobile make = new Automobile("Lexus 2017");
System.out.println("The car is " + make.getMake());
Automobile model = new Automobile("RX");
System.out.println("The car is " + Automobile.getModel());
Automobile price = new Automobile("43020");
System.out.println("The car is " + Automobile.getPrice());
// Use the mutator to change the make variable
Automobile.setMake("Lexus 2017");
System.out.println("The car is " + backDoor.getState());
// Use the mutator to change the model variable
Automobile.setModel("RX");
System.out.println("The car is called " + backDoor.getName());
Automobile.setPrice("43020");
System.out.println("The car is " + price.getPrice());
}
}
这是我第一次使用构造函数,而且我对 Java 还很陌生,所以对于任何明显的错误,我深表歉意。提前感谢您的时间和帮助。
【问题讨论】:
-
Parameterized constructor with three parameters (make, model and price);不匹配new Automobile("Lexus 2017"); -
不要使用类名来访问方法...这里有很多错误。您是否希望我们将它们全部修复并为您完成任务?
-
@cricket_007 不。那太荒谬了,我不会那样学任何东西。我只是想了解如何实现一个类,因为我已经将我的章节通读了三遍,听了讲义,但有些东西不适合我。我只想将我的代码放入上下文中。
-
好的。我已经回答了我看到的直接问题。汽车类看起来不错
-
@ScaryWombat 那么我会通过摆脱汽车年来解决它吗?
标签: java methods constructor implementation mutators