2016-10-24 54 views
-3

我有一個針對我的java編程課程的項目。實施演示/測試程序

指令是我們必須創建一個簡單的類和測試器類,並且該類必須包含一個Default構造函數;具有三個參數(品牌,型號和價格)的參數化構造函數;稱爲getMake()的Accessor方法返回make;稱爲getModel()的訪問器方法返回模型;訪問器方法getPrice()返回價格; Mutator方法setMake(String newMake)設置make; Mutator方法setModel(String newModel)設置模型;和一個Mutator方法setPrice(double newPrice)來設置價格。

我創建了我的課程和測試程序,並且我的課程完美編譯。 當我嘗試運行它時,雖然得到的錯誤是沒有主要方法。現在,我遵循我的教授的示例程序示例,並且發現了一些錯誤。如果有人能給我一個正確的方向指針,我將不勝感激。

我的問題是:我如何實現我的測試程序?我是否需要創建一個zip文件?我已經試過這樣做,似乎不幫助多...

以下是我的類代碼:

public class Automobile 
{ 
    private String make;  
    private String model; 
    private double price; 

    public Automobile() 
    { 
     make = "Lexus2017"; 
     model = "RX"; 
    } 

    public Automobile(String initMake, String initModel, double initPrice) 
    { 
     make = initMake; 
     model = initModel; 
     price = initPrice; 
    } 

    public String getMake() 
    { 
     return make; 
    } 

    public String getModel() 
    { 
     return model; 
    } 

    public double getPrice() 
    { 
     return price; 
    } 

    public void setMake(String newMake) 
    { 
     make = newMake; 
    } 

    public void setModel(String newModel) 
    { 
     model = newModel; 
    } 

此外,以下是我的測試類(具有一個很多錯誤的):

public class AutomobileTester 
{ 
    public static void main(String[] args) 
    { 
     Automobile make = new Automobile("Lexus 2017"); 
     System.out.println("The car is " + make.getMake()); 

     Automobile model = new Automobile("RX"); 
     System.out.println("The car is " + Automobile.getModel()); 

     Automobile price = new Automobile("43020"); 
     System.out.println("The car is " + Automobile.getPrice()); 

     // Use the mutator to change the make variable 
     Automobile.setMake("Lexus 2017"); 
     System.out.println("The car is " + backDoor.getState()); 

     // Use the mutator to change the model variable 
     Automobile.setModel("RX"); 
     System.out.println("The car is called " + backDoor.getName()); 

     Automobile.setPrice("43020"); 
     System.out.println("The car is " + price.getPrice()); 
    } 
} 

這與建設者的工作我的第一次,我很新到Java,所以我現在沒有任何明顯的錯誤抱歉。提前感謝您的時間和幫助。

+1

'參數化的構造函數與t hree參數(品牌,型號和價格);'不匹配'新汽車(「雷克薩斯2017」);' –

+0

不要使用類名來訪問方法...這裏有很多錯誤。你是否期待我們解決所有問題併爲你完成任務? –

+0

@ cricket_007不,那太荒謬了,我不會學到任何東西。我只想了解如何實施一堂課,因爲我已經三次閱讀了我的章節,聽了課堂筆記,還有一些沒有點擊我。我只是想把我的代碼放在上下文中。 –

回答

1

第一個問題之一是,您不需要使用適當數量的參數來調用構造函數,在Java(和大多數編程語言)中,您必須將所有必需的參數提供給方法/函數/構造函數在一次調用。爲您的代碼的解決將是使用:

Automobile car = new Automobile("Lexus 2017", "RX", 43020.0D); 

此外,當你打印出你第一次使用instance通話則使用static電話的汽車信息,我不會去多進之間的差異兩個,但基本上instance調用要求您實例化一個對象,而不是static。這個問題的解決將是做:

System.out.println("The car is a " + car.getMake() + ", the brand is " + car.getModel() + ", the price is $" + car.getPrice()); 

至於改變的變量,你應該使用:的

car.setMake("My New Car Make"); 

代替:

Automobile.setMake("My New Car Make"); 

static之間的差異您可以看看hereherehere

+0

如果您有'汽車=新汽車(「雷克薩斯2017」,「RX」,43020.0D);',43020後的D是否將價格視爲雙重變量?另外,我的任務說使用實例變量,而不是靜態的。這直到下一章纔會出現。 –

+0

關於類名稱呢?那些好嗎?汽車和汽車測試儀?通過這些名字,該計劃是否知道實施這個課程? –

+0

「d」或「D」字符表示該數字的類型爲'double',您可以在這裏找到更多關於這些類型標識符的信息(https://docs.oracle.com/javase/tutorial/java/nutsandbolts /datatypes.html)。至於你使用靜態和實例變量,我指出你正在使用靜態調用,你應該使用實例調用。至於名稱,只要鍵入的內容完全相同,就可以真正使用它,它應該能夠正確實例化該類,[IDE](https://en.wikipedia.org/wiki/Integrated_Development_environment)可以幫助。 –

0

你做得對。您通過使用Automobile類的make實例變量來訪問該方法。

(邊注:請爲汽車例如一個不好的名字,而稱之爲CAR 1,或東西)

Automobile make = new Automobile("Lexus 2017"); 
System.out.println("The car is " + make.getMake()); 

現在,在其他地方你使用Automobile.someMethod(),這是不對的,因爲你需要設置或獲取類的一個實例上的數據,而不是整個類。

然後,最後,您需要使用該類中的三個參數測試構造函數。

+0

謝謝。 「make」是我的教授希望我們使用的實例變量,但是我必須創建兩個汽車物體,car1,car2會如何工作?我會用'setMake(String newMake)'嗎?併爲每個實例變量做到這一點? –

+0

我不是在談論'String make',我正在談論'Automatible make = new Automobile(...)',因爲那是一輛有製造的汽車,是的,但不是製造自己 –

0

您在構造函數調用中有錯誤。 您的構造函數需要三個參數(品牌,型號和價格),但是當您調用該方法時只會發送一個參數。這是一個錯誤。 默認情況下,Java類構造函數不接受參數(在你的情況下,這將是「new Automobile()」)。 要實現測試儀,您有兩種選擇。 首先,創建使用構造的車無參數,然後設置參數:

Automobile auto = new Automobile(); 
auto.setMake("Lexus 2017"); 
auto.setModel("RX"); 
auto.setPrice(43020); 

汽車汽車製造=新汽車();

另一種選擇是使用自己的構建和傳遞參數:

Automobile auto2 = new Automobile("Lexus 2017", "RX", 43020); 

Automobile.java:

public class Automobile { 
private String make; 
private String model; 
private double price; 

public Automobile() { 
} 

public Automobile(String make, String model, double price) { 
    this.make = make; 
    this.model = model; 
    this.price = price; 
} 

public String getMake() { 
    return make; 
} 

public void setMake(String make) { 
    this.make = make; 
} 

public String getModel() { 
    return model; 
} 

public void setModel(String model) { 
    this.model = model; 
} 

public double getPrice() { 
    return price; 
} 

public void setPrice(double price) { 
    this.price = price; 
} 
} 

AutomobileTester.java:

public class AutomobileTester { 

public static void main(String[] args) { 

    Automobile auto = new Automobile(); 
    auto.setMake("Lexus 2017"); 
    auto.setModel("RX"); 
    auto.setPrice(43020); 
    System.out.println("The car1 is " + auto.getMake() + " " + auto.getModel() + " " + auto.getPrice()); 

    Automobile auto2 = new Automobile("Lexus 2017", "RX", 43020); 
    System.out.println("The car2 is " + auto2.getMake() + " " + auto2.getModel() + " " + auto2.getPrice()); 

} 

}

+0

不會汽車汽車make = new();是多餘的?另外,我知道當你想讓方法中的參數等於實例變量時,你使用這個參數。你什麼時候不想使用它們? –

+0

是的,這是一個錯誤。這將是汽車汽車=新汽車(); 如果你只想使用一些參數,你可以實現多個構造函數。例如,如果您只想使用價格:公共汽車(雙倍價格){this.price = price; } 電話將是汽車汽車=新汽車(1000);或者如果你想使用的價格和型號:公共汽車(字符串模型,雙重價格){ this.model = model; this.price = price;電話將是:汽車汽車=新汽車(「Rx」,1000); – Juan