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 3 of 3

Thread: Help with Stringed Instrument Project

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Stringed Instrument Project

    Hello friends, I have been given the following assignment:


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

    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.
    A constructor method that set the tuned and currently playing fields to false.
    Other methods 1) to tune the instrument, 2) to start the instrument playing, and 3) to stop the instrument from playing.
    Other methods as you see fit (Add at least one unique method).
    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.

    Create a Java class for your instrument. 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.");
    }

    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.

    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!)


    In my test file, named linwoodaraujop3.java I currently have:

    package linwoodaraujop3;
     
     
    public class LinwoodAraujop3 {  
     
        public static void main(String[] args) throws Exception  
        {  
     
            java.io.File file = new java.io.File("LinwoodAraujop3test.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 guitar object    
                Guitar guitar1 = new Guitar();    
     
                //write test results to file    
                output.println(guitar1.playGuitar());    
                output.println(guitar1.stopPlaying());    
                output.println(guitar1.tuneGuitar());    
            }  
    }
     
    class Guitar     
     {    
         boolean isTuned; //Guitar isn't tuned at the start   
         boolean isPlaying; //Guitar isn't playing at start    
     
         //array for strings on Guitar  
         char [] guitarStrings = {'E','A','D','G'};    
     
         //default Guitar object    
         public Guitar()    
         {    
             isTuned = false;    
             isPlaying = false;    
             System.out.println("The guitar isn't playing, and isn't tuned.");    
         }    
     
         public Guitar(boolean T, boolean P)    
         {    
             isTuned = T;    
             isPlaying = P;    
         }    
     
         public boolean playGuitar()    
         {    
            System.out.println("The guitar is playing!");    
            return isPlaying = true;    
         }    
     
         public boolean stopPlaying()    
         {    
             System.out.println("The guitar has stopped playing.");    
             return isPlaying = false;    
         }    
     
         public boolean tuneGuitar()    
         {    
             System.out.println("The guitar is being tuned!");    
             return isTuned = true;    
         }    
     
     }
    __________________________________________
    And for my instrument file I currently have:
    public class Guitar {
     
     
     public void main(String[] args){
     
            Guitar[] guitars;
     
           guitars = new Guitar[10]; 
     
                for(int i = 0; i < guitars.length; i++){
                   guitars[i] = new Guitar();
     
                }
     
     }
    }
    __________________________________________________ ___

    I have already cleaned up all of my code and currently am not receiving any error messages. However, when i run my programs it only prints to the console one iteration. And the file the it creates turns up with nothing inside of it. Any suggestions or help will be greatly appreciated.

    -Linwood


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with Stringed Instrument Project

    Why do you have 2 Guitar classes?

    Make sure you close the PrintWriter.
    Improving the world one idiot at a time!

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with Stringed Instrument Project

    Quote Originally Posted by Junky View Post
    Make sure you close the PrintWriter.
    Better yet, use try with resource constructs available in Java 7.

Similar Threads

  1. Instrument Class Assignment Help
    By kinney.j in forum Object Oriented Programming
    Replies: 1
    Last Post: November 2nd, 2011, 07:27 AM
  2. 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
  3. Project
    By Mac in forum What's Wrong With My Code?
    Replies: 15
    Last Post: June 4th, 2010, 04:39 AM
  4. help with project
    By pairenoid in forum Object Oriented Programming
    Replies: 1
    Last Post: May 7th, 2010, 08:55 AM
  5. Project - Please Help
    By toxikbuni in forum Java Theory & Questions
    Replies: 0
    Last Post: April 20th, 2010, 09:58 AM

Tags for this Thread