【问题标题】:Constructor and no-argument constructor? [closed]构造函数和无参数构造函数? [关闭]
【发布时间】:2014-10-07 04:20:39
【问题描述】:

以下是我必须完成的 Java 程序的说明和代码。我被卡住了,不知道如何继续。我试图弄清楚这一点。我觉得我不知道我在做什么。非常感谢所有帮助、指导和解释。

编写一个名为Car 的类,该类具有以下字段:

yearModelyearModel 字段是一个包含汽车年份模型的 int。

makemake 字段引用了一个保存汽车品牌的字符串对象。

speedspeed 字段是保存汽车当前速度的 int。

此外,该类应具有以下构造函数和其他 方法:

构造函数:一个构造函数应该接受汽车的年份模型, 制作和速度作为论据。这些值应分配给 对象的 yearModelmakespeed 字段。另一个构造函数将 没有参数,并将分配 0 作为汽车的年份型号和速度 和一个空字符串(“”)作为品牌。

访问器:适当的访问器方法应该获取存储的值 在对象的 yearModelmakespeed 字段中。

Mutators:适当的 mutator 方法应该将值存储在一个 对象的 yearModelmakespeed 字段。

accelerate: 加速方法应该在speed 字段中添加 5 每次调用。

brake:刹车方法应该从speed字段中减去5 调用它的时间。

在要求用户输入数据的程序中演示该类 然后创建一个Car 对象。然后它调用accelerate 方法 五次。每次调用accelerate 方法后,获取当前 汽车的speed 并显示它。然后调用brake方法五 次。每次调用brake方法后,获取当前speed的 汽车并展示它。

运行此程序的输出将类似于:

Enter the car's year model: 1965
Enter the car's make: Mustang
Enter the car's speed: 30

Current status of the car:
Year model: 1965
Make: Mustang
Speed: 30

Accelerating...
Now the speed is 35

Accelerating...
Now the speed is 40

Accelerating...
Now the speed is 45

Accelerating...
Now the speed is 50

Accelerating...
Now the speed is 55

Braking...
Now the speed is 50

Braking...
Now the speed is 45

Braking...
Now the speed is 40

Braking...
Now the speed is 35

Braking...
Now the speed is 30

这是我目前所拥有的:

public class Car {

// Declaration of variables.
private int yearModel;
private String make;
private int speed;

// Constructor that accepts arguements.
public static void acceptor(int yearModelIn, String makeIn, int speedIn){
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter the car's year model: ");
    yearModelIn = keyboard.nextInt();  
    System.out.println("Enter the car's make: ");
    makeIn = keyboard.next();
    System.out.println("Enter the car's speed: ");
    speedIn = keyboard.nextInt();
}

// Constructor that zeroes fields.
public void zeroer()
{
    yearModel = 0;
    speed = 0;
    make = ("");
}

// Accessor Methods
public int getYearModel()
{
    return yearModel;
}
public String getMake()
{
    return make;
}
public int getSpeed()
{
    return speed;
}    

// Accelerate method for adding 5 to speed.
public void Accelerate()
{
    speed += 5;        
}

// Brake method for reducing speed.
public void Brake()
{
    speed-=5;
}

【问题讨论】:

  • Accessor 只是 getter 方法的一个花哨词,而 Mutator - 对于 setter
  • "我卡住了" 用什么?哪个部分不工作?
  • 你的构造函数应该看起来像 public Car(String whatever){} 而不是“接受者”或“归零者”
  • 如果您不知道 Constractor 是什么以及它的用途,您需要回到您的书籍/课堂笔记。

标签: java constructor accessor mutators


【解决方案1】:

首先,摆脱acceptor 方法,它没有做你认为应该做的事情......我可能也会放弃zeroer 方法,因为它不提供任何有用的功能,除了把你搞砸了

构造函数。一个构造函数应该接受汽车的年份型号、品牌和速度作为参数。这些值应该分配给对象的 yearModel、make 和 speed 字段。另一个构造函数将没有参数,并将分配 0 作为汽车的年份和速度,并将空字符串 (“”) 作为品牌。

首先,你错过了这个......

public Car(int yearModel, String make, int speed) {
    this.yearModel = yearModel;
    this.make = make;
    this.speed = speed;
}

由此,您可以创建汽车的实例...

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter the car's year model: ");
    int year = keyboard.nextInt();
    keyboard.nextLine();
    System.out.println("Enter the car's make: ");
    String make = keyboard.nextLine();
    System.out.println("Enter the car's speed: ");
    int speedIn = keyboard.nextInt();

    Car car = new Car(year, make, speedIn);        
}

然后,你需要做的就是调用相应的方法来改变和报告状态,例如......

car.Accelerate();
System.out.println(car.getSpeed());

遇到困难时请查阅笔记和教程,例如Providing Constructors for Your ClassesPassing Information to a Method or a Constructor

您可能还想通读Code Conventions for the Java TM Programming Language,它将使人们更容易阅读您的代码并让您更轻松地阅读其他人

【讨论】:

  • ... 和zeroer() 同时...
  • +1 for Programming mad :D 和你顺便说一句,我认为他是 java 新手可能没有那么多想法 :)
  • @Krishna 是的,我回答这个问题的唯一原因是因为 OP 进行了尝试,但有点成功,希望他们能阅读链接的教程......
  • @MadProgrammer 我完全同意你的看法 :) 一如既往:)
  • @Krishna 您可以添加另一个答案,但已经有两个基本上为 OP 提供了可粘贴的解决方案
【解决方案2】:

你的编码结构有一点点错误。

类 Car 不应该接受输入,你的 Car 对象应该是 POJO,只有 getter 和 setter。

所以你的车应该是这样的

public class Car {
    String model;
    int make;
    double speed = 0.0;
    Car(int make, String model, double speed) {
        this.make = make;
        this.model = model;
        this.speed = speed;
   }

   // then the getters and setters for speed, accelerate, decelerate
}

现在你从一个实现类访问你的 Car 类,比如 Garage

public class Garage {
    public static void main(String ar[]) {
       // Now take your inputs here say model = Mustang, make = 1995 speed = 50
       Car c = new Car(make, model, speed);
       // and then a simple looping construct
       for(int i = 0; i < 5; i ++) {
           c.accelerate(); // please don't use capitals for method names, because they look like class names
           System.out.println(c.getSpeed());
        }
    }
}

【讨论】:

    【解决方案3】:

    这是供您参考的代码。并删除那些 zeror 和 acceptor 方法。

    导入 java.util.Scanner;

    公共类汽车{

    private int speed;
    private String make;
    private int yearModel;
    
    public int getSpeed() {
        return speed;
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public String getMake() {
        return make;
    }
    public void setMake(String make) {
        this.make = make;
    }
    public int getYearModel() {
        return yearModel;
    }
    public void setYearModel(int yearModel) {
        this.yearModel = yearModel;
    }
    
    void accelarate(){
      this.setSpeed(this.getSpeed()+5);
    }
    
    void brake(){
        this.setSpeed(this.getSpeed() -5);
    }
    
    public Car(int yearModel,String make,int speed){
        this.speed = speed;
        this.make =make;
        this.yearModel = yearModel;
    }
    
    
    public static void main(String[] args) {
         Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter the car's year model: ");
            int yearModel = keyboard.nextInt();
            keyboard.nextLine();
            System.out.println("Enter the car's make: ");
            String make = keyboard.nextLine();
            System.out.println("Enter the car's speed: ");
            int speed = keyboard.nextInt();
    
        Car car = new Car(yearModel,make, speed);
    
        //Accelerate
        for(int i=0;i<5;i++){
            car.accelarate();
            System.out.println("speed after accelaration::"+car.getSpeed());
        }   
    
        //Brake
        for(int i=0;i<5;i++){
            car.brake();;
            System.out.println("speed after applying brake::"+car.getSpeed());
        }   
    
    }
    

    }

    【讨论】:

      【解决方案4】:
      public class Car {
      
          private int yearmake; // Variable of your yearmake of car
          private String model; // Variable of your car company
          private int speed;  // Variable of your car speed
      
          // getter and setter method as the pojo class defines that will give getter and setter property for the every variable inside class as according to OP it wont let u change Variable directly
      
          public String getModel() {
              return model;
          }
      
          public void setModel(String model) {
              this.model = model;
          }
      
          public int getSpeed() {
              return speed;
          }
      
          public void setSpeed(int speed) {
              this.speed = speed;
          }
      
          public int getYearmake() {
              return yearmake;
          }
      
          public void setYearmake(int yearmake) {
              this.yearmake = yearmake;
          }
      
          public Car(int speed) {
              this.speed = speed;
          }
         // constructor which will accepts and initialize your car with data i mean class
          public Car(int yearmake, String model, int speed) {
              this.yearmake = yearmake;
              this.model = model;
              this.speed = speed;
              System.out.println("Year Make " + yearmake);
              System.out.println("Model " + model);
              System.out.println("Speed " + speed);
      
          }
          // method of the making accelerate car by speed of 5 
          public int acclarate(int speed) {
              speed = speed + 5;
              System.out.println("Speed Acclarated " + speed);
              return speed;
          }
         // method for reducing speed of 5
          public int Breaking(int speed) {
              speed = speed - 5;
              System.out.println("Speed Breaking " + speed);
              return speed;
          }
         // main method
          public static void main(String[] args) {
              // accept from user input
              Scanner keyboard = new Scanner(System.in);
              System.out.println("Enter the car's made year model: ");
              int year = keyboard.nextInt();
              keyboard.nextLine();
              System.out.println("Enter the car's make Company: ");
              String make = keyboard.nextLine();
              System.out.println("Enter the car's speed: ");
              int speedIn = keyboard.nextInt();
              // initialize car model with constructor
              Car c = new Car(year, make, speedIn);
      
              //increasing speed with use of method and loop
              for (int i = 0; i < 5; i++) {
                  int speedchange = c.acclarate(c.getSpeed());
                  c.setSpeed(speedchange);
      
              }
              //decreasing speed according to your requriement with use of method and loop
              for (int i = 0; i < 5; i++) {
                  int decreasedpeed = c.Breaking(c.getSpeed());
                  c.setSpeed(decreasedpeed);
              }
      
          }
      }
      

      你也可以用其他方式做:) 但我也会像 madprogrammer 所说的那样建议。学习一些OP和基本的东西:)

      【讨论】:

      • 如果你能解释“为什么”(OP 应该按照你说的做)并且可能提供一些参考,你会在其他“copy-n-paste”答案之上提出这个;)跨度>
      • @MadProgrammer okey m 编辑它:)
      猜你喜欢
      • 2013-08-26
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 2011-02-08
      • 2011-10-08
      • 1970-01-01
      相关资源
      最近更新 更多