2014-10-07 178 views
-4

以下是我必须完成的Java程序的指令和代码。我卡住了,不知道如何继续。我试图弄清楚这一点。我觉得我不知道我在做什么。所有的帮助,方向和解释将非常感激。构造函数和无参数构造函数?

写了一个名为Car类具有以下字段:

yearModel:该yearModel场是保持汽车的年模型的int。

make:该make字段引用一个字符串对象,其中包含汽车的品牌。

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

此外,类应该有下面的构造和其他 方法:

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

访问者:相应的访问者方法应将 中的值存储在对象的yearModel,makespeed字段中。

变体:适当的变体方法应将值存储在 对象的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; 
} 
+1

'Accessor'对于'getter'方法来说只是一个奇特的词,而对'setter'来说''Mutator''。 – 2014-10-07 04:24:53

+0

“_I卡住了”什么?哪部分不工作? – csmckelvey 2014-10-07 04:24:57

+1

你的构造函数应该看起来像'public Car(String whatever){}'not“acceptor”或者“zeroer” – tom 2014-10-07 04:26:43

回答

1

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

构造函数。一个构造者应该接受汽车的年模型,制造和加速作为参数。应将这些值分配给对象的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

...和'zeroer()'在同一时间... – 2014-10-07 04:29:27

+0

+1编程疯狂:D和雅btw兄弟我认为他是新鲜的Java可能没有这么多的想法:) – Krishna 2014-10-07 04:57:32

+1

@Krishna是啊,我回答这个问题的唯一原因是因为OP做了一个尝试,所有这一点点的标志,希望他们会阅读链接的教程... – MadProgrammer 2014-10-07 04:58:25

0

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

class车不应该输入的,你的Car对象应该是POJO,只有getters和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 
} 

现在你从一个实现类访问你的车类,说车库

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

这里是您reference.And删除这些代码zeror和接受者方法。

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

} 

}

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和基本的东西:)

+0

如果你可以解释“为什么”(OP应该做你说的)并且可能提供一些参考资料,那么你会提出这个高于其他“复制粘贴”答案;) – MadProgrammer 2014-10-07 05:21:27

+0

@MadProgrammer okey m编辑它:) – Krishna 2014-10-07 05:22:27