2014-10-07 247 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