• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with Instrument Class assignment  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will start by listing the requirements of my program, and then I will go from there.

Requirements (Sorry for the wall of text, just want to be thorough in my explanation so my problem is understood):

Design and implement a stringed musical instrument class using the following guidelines:

a. Data fields for your instrument should include number of strings, an array of string names representing string names (e.g. E,A,D,G), and boolean fields to determine if the instrument is tuned, and if the instrument is currently playing. You are welcome to add additional data fields if you like.

b. A constructor method that set the tuned and currently playing fields to false.

c. Other methods 1) to tune the instrument, 2) to start the instrument playing, and 3) to stop the instrument from playing.

d. Other methods as you see fit (Add at least one unique method).

2. Create a UML class diagram using a diagram tool (e.g. PPT, Visio) of your choice. Prepare the diagrams and place them in a word document along with a brief description of each of your classes.

3. Create Java classes for your instruments. Be sure that your code matches your design specifications and some minimal functionality is included. For example, if you called the violin.play() method, you should at least print that the violin is playing. Similar functionality should be supplied when you stop playing, tune or call any of your methods. For example:

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

4. Write the output from your Instrument class methods to a text file that a user entered from the command line arguments (e.g. java Mynamep3tst myfilename.txt). This allows your program to accept filenames from the user via a command line argument.

5. Finally, create a Java test class that simulates using your instrument class. In your test class be you should at a minimum: a) Construct 10 instances of your instrument, b) tune your instruments, c) Start playing your instrument, d) Call your unique method, and e) Stop playing your instruments. (Hint: Arrays and Loops will make your job easier and result in more efficient code!)

6. Your programs should compile and run without errors.

7. Be sure to test your program carefully. Provide a list of comprehensive test cases used to validate your application and include these test cases in your word document containing your UML diagrams and descriptions. Similar to Project 1, your test data can be shown in a table that includes input data, expected output, actual output and pass/fail results from the test.

The error I receive is in the statement



The error I receive is: No enclosing instance of type JamesKinneyp3 is accessible. Must qualify the allocation with an enclosing instance of type JamesKinneyp3 (e.g. x.new A() where x is an instance of JamesKinneyp3).

I know something needs to go in the parenthesis in Violin(); However, I am not sure what.

Here is the code I have thus far:



Another question I have is about the test Class that is mentioned in the requirements. I have read through our textbook chapters and our online sources for the class, however it does not explain how to correlate the Instrument class with the Test class. If any light could be shed on this, I would be grateful.

I apologize for all of the text.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Did your instructor really say constructor method? Constructors aren’t methods and methods aren’t constructors.
You need to start small. Comment out everything, write (or un-comment) 5 lines, compile and run. Repeat the process until the class is complete.
Give the Violin class a main method, which you remove/deprecate/comment out later, which tests all its methods. Something rather like this:Obviously those method calls need to match the methods in the Violin class. Similarly the constructor call after new must match the constructor. If your method or constructor needs certain parameters, then you need to provide matching arguments every time you call that constructor. If your method or constructor doesn’t take any parameters, you call it with an empty pair of ().

Note the println() calls will give you something incomprehensible until you override the toString() method.

I am not at all happy about a return and an assignment in the same statement. Bad style. Separate the two into separate statements, and one statement per line.

The notion that your class would have a method called playViolin() seems strange to me. You can have several object-oriented ways of doing it.
  • strad.play() where strad is a Violin object
  • lso.play(strad) where lso is an Orchestra object
  • menuhin.play(strad) where menuhin is a Musician object
  • I think they get better as you go down; a musician playing a violin better models real life than a violin playing itself.
     
    Ranch Hand
    Posts: 808
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:I am not at all happy about a return and an assignment in the same statement. Bad style. Separate the two into separate statements, and one statement per line.


    Better still, separate into two distinct methods, one that allows us to inspect the violin's tuned state, and another that allows us to perform the tuning.

    Another question I have is about the test Class that is mentioned in the requirements. I have read through our textbook chapters and our online sources for the class, however it does not explain how to correlate the Instrument class with the Test class. If any light could be shed on this, I would be grateful.


    A test class is simply a Java class with a main method that instantiates class you are testing (in this case it will instantiate Violin) and then exercises all its functionality.
    Give the Violin class a main method, which you remove/deprecate/comment out later, which tests all its methods.
    This is a pretty good model for a ViolinTest class. But per the assignment, none of this should be in the Violin class itself.
     
    Rancher
    Posts: 600
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    James:

    You probably also want your Violin class to be its own, public class.

    John.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!