2012-04-19 12 views
1

私は別の別のドライバクラスを使ってクラスを作成しています。車のクラスは、自動車の会社が自動車の情報をモデル、モデル、登録番号などのように保存するためのものです。新しい車の入力に使用できるドライバークラスを使用して、車両がレンタルされていないかどうか、もし雇われれば雇用者の方法とJavaカークラスの作成

私の車クラス:

public class Car { 

private String Make; 
private String Model; 
private int RegistrationNum; 

public Car(String Make, String Model, String RegN){ 
    //Constructor, 
    //stores the make, model and registration number of the new car 
    //sets its status as available for hire. 
    Make = ""; 
    Model = ""; 
    RegN = ""; 

} 

public String getMake(){ 
    return Make; 

} 

public String getModel(){ 
    return Model; 

} 

public boolean hire(String newHirer){ 

    { 
    //Hire this car to the named hirer and return true. 

     return true; 
    } 
    //Returns false if the car is already out on hire. 



} 

public boolean returnFromHire(){ 

    { 
//Receive this car back from a hire and returns true. 
     return true; 
    } 

//Returns false if the car was not out on hire  


} 

public int getRego(){ 


//Accessor method to return the car’s registration number  

    RegistrationNum++; 
    return RegistrationNum; 
    } 



public boolean hireable(){ 
//Accessor method to return the car’s hire status.  



    { 
//returns true if car available for hire   
    return true;  
    } 
} 

public String toString(){ 
//return car details as formatted string 
//output should be single line containing the make model and reg number 
//followed by either "Available for hire" or "On hire to: <name>" 


    return "Vehicle ID number: "+ getRego()+"-"+"Make is: "+ getMake()+"-"+"Model is: "+getModel(); 


} 


} 

以下は、私のドライバークラスです:

import java.util.*; 
    public class CarDriver { 
    static Car car1; 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    Scanner scan = new Scanner(System.in); 


{ 
    System.out.println("Make?"); 
    String Make=scan.nextLine(); 
    System.out.println("Model"); 
    String Model=scan.nextLine(); 
    System.out.println("Registration number?"); 
    String RegNum=scan.nextLine(); 

    car1 = new Car(Make,Model,RegNum); 


    System.out.println("What you input :"); 

    System.out.println(car1.toString()); 
}} 

} 

私の出力:

Make? 
carmake 
Model 
carmodel 
Registration number? 
12345t 
What you input : 
Vehicle ID number: 1-Make is: null-Model is: null 

問題:

    Javaのコード

  1. 私は入力、モデルのように、 及び登録番号

  2. を作るの情報を格納するためのドライバ クラスを接続することができませんに boolean型のメソッドのための擬似コードを変換する方法を理解することができませんでし210
  3. これと

+1

これは[タグ:宿題]ですか? –

+0

this.Make、this.Modelとthis.redキーワードはコンストラクタでインスタンス変数に格納する –

+0

done =]とにかく実際に宿題ではなくリビジョンに関する質問 – Liquified

答えて

3

第二

変更コンストラクタ:

public Car(String Make, String Model, String RegN){ 
    this.Make = Make; 
    this.Model= Model; 
    this.RegN = RegN; 
} 

以前のコンストラクタには問題がありました。基本的にはコンストラクタの引数を取得し、それらのすべてを ""(空文字列)に設定するだけでした。インスタンスのフィールドに引数値を割り当てたいとします。インスタンスフィールドにアクセスする場合は、キーワードをこのにする必要があります。私はコードで見たよう

public Car(String Make, String Model, String RegN){ 
//Constructor, 
//stores the make, model and registration number of the new car 
//sets its status as available for hire. 
Make = ""; 
Model = ""; 
RegN = ""; 

}

0

、あなたは、コンストラクタで車のインスタンスのいずれかのフィールドを設定しないでください。あなたはこのようになめらかに書く必要があります:あなたの2番目の質問に

public Car(String Make, String Model, String RegN){ 
    //Constructor, 
    //stores the make, model and registration number of the new car 
    //sets its status as available for hire. 
    this.Make = Make; 
    this.Model = Model; 
    this.RegN = RegN; 
} 
0

回答:あなたが呼び出す

瞬間:Carクラスの

car1 = new Car(Make,Model,RegNum); 

コンストラクタは変数をあなたと一緒に呼び出されます供給される。 あなたが作成したコンストラクタを見てとるとき:

public Car(String Make, String Model, String RegN){   
    Make = "";  
    Model = "";  
    RegN = ""; 
}  

をあなたが提供する変数がCarクラスのローカル変数に設定されることはありませんことがわかります。あなたのプログラムに

public Car(String Make, String Model, String RegN){   
    this.Make = Make;  
    this.Model = Model;  
    this.RegN = RegN; 
}  

幸運:あなたは、次のinttoそれを変更する必要があり、この問題を解決するために !

1

第1位:「この車を借りた人」または「この車を借りたドライバー」に関する情報を検索するには、その情報を先に入力する必要があります。 どのデータ型を使用しますか? (これは「宿題」であることを覚えておいてください。私の答えを与えない方が良いと思います)。

P.S:変数と非静的/最終属性に大文字ではない識別子を使用する方が良いです。

0

Carクラスのコンストラクタを目的のパラメータで呼び出す場合、デフォルトでは、これらのパラメータ参照がコンストラクタに送信されます。あなたのパラメータとあなたのCarクラスのメンバ変数は、同じ名前を持つCarクラスのコンストラクタで

public Car(String Make, String Model, String RegN){   
    Make = "";  
    Model = "";  
    RegN = ""; 
} 

。したがって、コンストラクタが呼び出されると、ローカル変数は空の文字列に変更されます。ローカル変数には呼び出し元からの参照が含まれているため、呼び出し元の元の変数も変更されています。 再度、クラスメンバー参照へのインスタンスを作成しないので、それはnullのままです。 あなたのコンストラクタで次のコードセグメントを使用する場合は、

this.Make = "";  
this.Model = "";  
this.RegN = ""; 

Carクラスのメンバ変数は、発信者からの変数の代わりに変更されます。