Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Instrument Class Assignment Help

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Instrument Class Assignment Help

    I know this problem has been posted on the forums before, but the information on that thread wasn't too much help to me. I think my problem is that I overlook the simpler details when writing my code for programs.

    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
    Violin violin1 = new Violin();

    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:

    public class JamesKinneyp3 
    {
            public static void main(String[] args) throws Exception 
            {
     
     
                java.io.File file = new java.io.File("JamesKinneyp3tst.txt");
     
                //check to see if file already exists
                if (file.exists())
                {
                    System.out.println("File already exists");
                    System.exit(0);
                }
     
                //create a file
                java.io.PrintWriter output = new java.io.PrintWriter(file);
     
                //declare violin object
                Violin violin1 = new Violin();
     
                //write test results to file
                output.println(violin1.playViolin());
                output.println(violin1.stopPlaying());
                output.println(violin1.tuneViolin());
            } 
     
     
     
    class Violin 
     {
         boolean isTuned; //Violin starts off not tuned
         boolean isPlaying; //Violin is not playing at start
     
         //array for Violin strings
         char [] violinStrings = {'E','A','D','G'};
     
         //default Violin object
         public Violin()
         {
             isTuned = false;
             isPlaying = false;
             System.out.println("The violin is not playing, and it is not tuned.");
         }
     
         public Violin(boolean T, boolean P)
         {
             isTuned = T;
             isPlaying = P;
         }
     
         public boolean playViolin()
         {
            System.out.println("The violin is playing!");
            return isPlaying = true;
         }
     
         public boolean stopPlaying()
         {
             System.out.println("The violin has stopped playing.");
             return isPlaying = false;
         }
     
         public boolean tuneViolin()
         {
             System.out.println("The violin is being tuned!");
             return isTuned = true;
         }
     
     }
     
    }
    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.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Instrument Class Assignment Help

    The main() method is a static method, which means it does not belong to any particular instance of the class it's in. And you can't access non-static stuff from a static method, because Java has no way to know which instance that stuff should belong to. That's what your error message is telling you- you can't create an instance of Violin from the static main method, because Violin is a non-static inner class.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  2. Replies: 7
    Last Post: July 21st, 2011, 02:29 PM
  3. 1st Java class assignment, and having some difficulties. Little help please.
    By flpanthers1 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 27th, 2011, 04:09 AM
  4. Applying a MIDI instrument change to a Track
    By AnkleSpankle in forum Java Theory & Questions
    Replies: 3
    Last Post: January 23rd, 2011, 03:34 PM
  5. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM