构造函数和无参数构造函数?

时间:2014-10-07 04:20:40

标签: java constructor accessor mutators

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

  

编写一个名为Car的类,其中包含以下字段:

     

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

     

makemake字段引用一个包含汽车品牌的String对象。

     

speedspeed字段是一个保持汽车当前速度的int。

     

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

     

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

     

访问者:适当的访问者方法应该存储值   在对象的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;
}

4 个答案:

答案 0 :(得分:1)

首先,摆脱acceptor方法,它没有按你认为的那样做...我可能也放弃了zeroer方法,因为它没有提供任何有用的方法功能,除了搞砸你

  

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

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

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,这样可以让人们更轻松地阅读您的代码并让您阅读其他代码

答案 1 :(得分:0)

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

类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());
        }
    }
}

答案 2 :(得分:0)

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

import 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());
    }   

}

}

答案 3 :(得分:0)

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

    }
}

你也可以做其他方式:)但我也会建议疯狂程序员说。学习一些OP和基本的东西:)