2013-09-22 22 views
-1

我正在NetBeans中爲java類編寫此代碼,但我遇到了一些錯誤,非常感謝您的幫助。分配是:使用下列準則實施絃樂器類在java代碼中非法表達

設計:

您的儀器數據字段應包含字符串的數字,代表字符串名稱字符串名稱的陣列(例如E, A,D,G)和布爾字段來確定樂器是否已調諧,以及樂器是否正在播放。如果您願意,歡迎您添加其他數據字段。

一個構造函數方法,將已調整和當前正在播放的字段設置爲false。 其他方法

  1. 給樂器調音
  2. 開始樂器演奏,並
  3. 以停止播放儀器。 您認爲合適的其他方法(添加至少一種獨特方法)。

使用您選擇的圖表工具(例如PPT,Visio)創建UML類圖。準備圖表並將它們放在一個word文檔中,並附上每個類的簡要說明。

爲您的儀器創建Java類。確保你的代碼符合你的設計規範,並且包含一些最小的功能。例如,如果您調用了violin.play()方法,則至少應打印出小提琴正在播放。當您停止播放,調整或調用您的任何方法時,應該提供類似的功能。例如:

public void playviolin() { 
    System.out.println("The violin is now playing."); 
} 

寫從您的儀器類的方法,以使用戶在命令行參數(例如,Java Mynamep3tst myfilename.txt)進入了一個文本文件的輸出。這允許您的程序通過命令行參數接受來自用戶的文件名。

最後,創建一個模擬使用您的儀器類的Java測試類。在你的測試課上,你應該至少:a)構建你的樂器的10個實例,b)調整你的樂器,c)開始演奏樂器,d)調用你獨特的方法,e)停止演奏你的樂器。 (提示:數組和循環將使您的工作更輕鬆,並導致更有效的代碼!)

因此,這裏是目前我的代碼:

package andrewrubinfinalproject; 

/** 
* 
* @author Andy 
*/ 
public class AndrewRubinFinalProject { 


    public static void main(String[] args) { 
//fields to determine if the instrument is isTuned, 
    private boolean isTuned; 

    //and if the instrument is currently isPlaying. 
    private boolean isPlaying; 


    private String name; 

    private int numberOfStrings = 4; // number of strings 
    private String nameofStringsInInstrument[] = {"E", "C", "D", "A"}; //an array of string names 


    //A constructor method that set the isTuned and currently isPlaying fields to false. 

    public AndrewRubinFinalProject() { 
     this.isTuned = false; 
     this.isPlaying = false; 
    } 



    public String getNameOfInstrument() { 
     return name; 
    } 


    public void setNameOfInstrument(String nameOfInstrument) { 
     this.name = nameOfInstrument; 
    } 


    // Other methods 

    public boolean isPlaying() { 
     return isPlaying; 
    } 

    public void setPlaying(boolean playing) { 
     this.isPlaying = playing; 
    } 

    public boolean isTuned() { 
     return isTuned; 
    } 

    public void setTuned(boolean isTuned) { 
     this.isTuned = isTuned; 
    } 

    public void startPlayInstrument() { 
     System.out.println("The Instrument is now Playing."); 
     isPlaying = true; 
    } 

    public void stopPlayInstrument() { 
     System.out.println("The Instrument is not Playing anymore."); 
     isPlaying = false; 
    } 

    public void startTuneInstrument() { 
     System.out.println("The Instrument is Tuned."); 
     isTuned = true; 
    } 

    public void stopTuneInstrument() { 
     System.out.println("The Instrument is not Tuned."); 
     isTuned = false; 
    } 

    public int getNumberOfStrings() { 
     return this.numberOfStrings ; 
    } 

    public String[] getStringNames() { 
     return nameofStringsInInstrument; 
    } 

} 
+1

這就像你把你的整個任務放在這裏:)你的問題是怎麼回事? –

+3

+1爲我讀過的最無意義的標題! – tilpner

+0

*「一些錯誤」*總是複製/粘貼錯誤和異常輸出。 –

回答

1

看來你的任務是給你檢查你的OOP概念。 看到下面的代碼,我已經給你的代碼一點觸摸。

package andrewrubinfinalproject; 

/** 
* 
* @author Andy 
*/ 
public class AndrewRubinFinalProject { 


    //fields to determine if the instrument is isTuned, 
    private boolean isTuned; 

    //and if the instrument is currently isPlaying. 
    private boolean isPlaying; 

    private String name; 

    private int numberOfStrings = 4; // number of strings 
    private String nameofStringsInInstrument[] = {"E", "C", "D", "A"}; //an array of string names 


    //A constructor method that set the isTuned and currently isPlaying fields to false. 

    public AndrewRubinFinalProject() { 
     this.isTuned = false; 
     this.isPlaying = false; 
    } 



    public String getNameOfInstrument() { 
     return this.name; 
    } 


    public void setNameOfInstrument(String nameOfInstrument) { 
     this.name = nameOfInstrument; 
    } 


    // Other methods 

    public boolean isPlaying() { 
     return this.isPlaying; 
    } 

    public void setPlaying(boolean playing) { 
     this.isPlaying = playing; 
    } 

    public boolean isTuned() { 
     return this.isTuned; 
    } 

    public void setTuned(boolean isTuned) { 
     this.isTuned = isTuned; 
    } 

    public void startPlayInstrument() { 
     System.out.println("The Instrument is now Playing."); 
     this.isPlaying = true; 
    } 

    public void stopPlayInstrument() { 
     System.out.println("The Instrument is not Playing anymore."); 
     this.isPlaying = false; 
    } 

    public void startTuneInstrument() { 
     System.out.println("The Instrument is Tuned."); 
     this.isTuned = true; 
    } 

    public void stopTuneInstrument() { 
     System.out.println("The Instrument is not Tuned."); 
     this.isTuned = false; 
    } 

    public int getNumberOfStrings() { 
     return this.numberOfStrings ; 
    } 

    public String[] getStringNames() { 
     return this.nameofStringsInInstrument; 
    } 

} 

問題是與你的主要方法的定位。 首先按照上面的代碼編寫一個類。 然後在你的主要方法中,通過調用構造函數來創建一個AndrewRubinFinalProject類的實例。

public static void main(String[] args){ 

    AndrewRubinFinalProject andrewsObject= new AndrewRubinFinalProject(); 
    // you can call any method in your class with respect to andrewsObject 
    // e.g. 
    // andrewsObject.setNameOfInstrument("Violin"); 
    // String x= andrewsObject.getNameOfInstrument() 


} 

你必須知道的是,主要的方法不一定在你正在寫的類中。它可以在你的程序中的其他地方。

2

你沒有關閉main方法。在開始編寫其他方法之前,應該插入}

在您的代碼中使用幻數是個壞習慣,例如private int numberOfStrings = 4;,如果更改數組,該怎麼辦?你也必須改變這個數字。

相反,最好使用返回數組大小的.length

0

首先你應該至少做2個班。一個是Instrument及其所有字段和方法。另一個是你的主要項目類,其中包含main()方法並創建並使用工具。 您張貼的代碼將不會在您打開時編譯,也不會關閉main方法。

0

當首次開始面向對象編程時,寫出一個小問題描述,或者至少用足夠詳細的方式來思考問題。

最初,名詞應該成爲你的課堂。摘要羣名詞可能是接口的良好候選者,並且動詞應成爲方法屬於到與動詞「最接近」的類。最接近的意思是,執行動詞將需要更多地訪問類中的屬性。