-4

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

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

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

make:该make字段引用了一个包含汽车品牌的 String 对象。

speed:该speed字段是一个 int,用于保存汽车的当前速度。

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

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

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

修改器:适当的修改器方法应该将值存储在对象的yearModelmakespeed字段中。

acceleratespeed:每次调用时,Accelerate 方法应在字段中添加 5 。

brakespeed:每次调用刹车方法时,应从字段中减去 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

4 回答 4

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

当您遇到困难时,请查阅您的笔记和教程,例如为您的类提供构造函数并将信息传递给方法或构造函数

您可能还想通读Java TM 编程语言的代码约定,这将使人们更容易阅读您的代码并让您更轻松地阅读其他人

于 2014-10-07T04:27:52.347 回答
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());
        }
    }
}
于 2014-10-07T04:42:15.660 回答
0

这是供您参考的代码。并删除那些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());
    }   

}

}

于 2014-10-07T04:59:47.770 回答
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);
        }

    }
}

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

于 2014-10-07T05:15:37.837 回答