2017-04-11 34 views
-1

你好親愛的Stackoverflow人!關於Java OOP及其問題

我目前正在研究我的項目,我認爲我被卡住了。

基本上我正在做的是通過使用之前製作的以前的Car Project來製作一個名爲Car的類。因此,當這個類被實例化時,用戶將選擇汽車#1-10,並且基於該號碼,具有該號碼的汽車將被加載,然後用戶可以選擇打開/關閉點火,水平地,垂直地移動汽車,或退出。此外,每當一個行動完成後,程序就會顯示汽車的當前位置。

  import java.util.Random; 
      public class Car 
      { 
       int value = 0; 
       boolean ignitionStatus = false; 

       public static boolean ignitionSwitch(boolean ignitionStatus){ 
        return !ignitionStatus; 
       } 

       public static int randomizePosition() { 
        Random rand = new Random(); 
        int randPos = rand.nextInt(20)+1; 
        return randPos; 
       } 

       public static char assignColor() { 
        //Assign a random number between 1 and 5 to each of the possible colors 
        Random rand = new Random(); 
        int colorNumber = rand.nextInt(5) + 1; 
        switch(colorNumber) 
         { 
         case 1: return 'R'; 
         case 2: return 'B'; 
         case 3: return 'W'; 
         case 4: return 'S'; 
         case 5: return 'G'; 
         } 
        return 'X'; 
       } 

       public static int moveHorizontally(int x, int distance, boolean ignition){ 
        if(ignition && x + distance <= 20) //checking if the movement is in bounds 
         return x + distance; 
        else if(!ignition){ //checking if the ignition is on 
         System.out.println("Please turn the ignition on.\n"); 
         return x; 
        } 
        else if(x + distance > 20){ 
         System.out.println("Error: movement out of bounds\n"); 
         return x; 
        } 
        else 
         return x; 
       } 

       public static int moveVertically(int y, int distance, boolean ignitionStatus){ //same thing as moveHorizontally 
        if(ignitionStatus && y + distance <= 20) 
         return y + distance; 
        else if(!ignitionStatus){ 
         System.out.println("Please turn the ignition on.\n"); 
         return y; 
        } 
        else if(y + distance > 20){ 
         System.out.println("Error: movement out of bounds\n"); 
         return y; 
        } 
        else 
         return y; 
       } 

       public String getColor(){ 
        return ""+ assignColor(); 
       } 

      } 

然而,問題是,我需要創建一個方法的getX(返回INT),方法的getY(返回INT),和getIgnition方法(它返回布爾值)。不過,我試着寫像

public int getX(){ 
    int value = moveHorizontally(int x, int distance, boolean ignition); 
    return value; 
    } 


public int getY(){ 
    int value = moveVertically(int y, int distance, boolean ignition); 
    return value; 
    } 

public boolean getIgnition(){ 
    return (ignitionSwitch(boolean ignitionStatus)); 

這樣的代碼,但我嘗試編譯,他們將無法編譯,我是一個Java的啞巴,所以我想不出真正弄明白我應該如何實現這些方法的getX,的getY和getIgnition,以便我可以在其他程序中實例化Car!

因此,我想就如何處理這個汽車類創建問題尋求建議......謝謝!

+0

當你調用一個方法時,你不應該爲每個參數指定數據類型。所以你可以寫一些東西,比如'int value = moveHorizo​​ntally(x,distance,ignition);'不要在參數前加上int或boolean。 –

+0

謝謝@DavidWallace。我會嘗試編輯它們,看看我能否在TestCar.java程序中實例化這個對象,我將很快創建它! – Harry

+0

可能還有其他錯誤。這只是我注意到的第一件事。 –

回答

-1

問題是,這些方法需要一個與它們相關的對象。此時您需要創建一個構造函數,以您想要的方式設置x和y值。之後,您可以編寫get方法。

+0

好的!謝謝 。我有另一個問題。我想我的TestCarClass.java稍後會詢問用戶他/她想要使用哪輛車(1-10),並且我將實例化我仍然在右邊掙扎的汽車類。那麼我是否需要在汽車類中創建數組還是沒有必要? – Harry

+0

對不起,但它不這樣工作。我們不做問題答案乒乓在這裏。 SO是關於提出明確答案的單個明確問題。我們不是免費的導師服務,我們通過他們所有的基本問題指導新手。 – GhostCat

+0

@GhostCat好嗎!我很抱歉造成麻煩。 – Harry