Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Design and implement a stringed musical instrument class

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!)


Here is what I have so far, I seem to have an issue at the Violin violin1 = new Violin(); portion of the code and I am having difficulty figuring it out, please help! thanks!
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you intentionally nest the Violin class inside the AndrewFirthP3 class? Since Violin is an inner class, it can't be instantiated unless (a) there is an enclosing instance of AndrewFirthP3 or (b) the Violin class is changed to be static. But I think this is not what you want. It's simpler just to define the Violin class outside the other class.
 
andrew firth
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"It's simpler just to define the Violin class outside the other class."

I am trying to do this and getting frustrated with the process. What is the best way to do this without changing many lines of code around?
 
Marshal
Posts: 79637
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You will probably have to move code, because you would want the Violin class to be public, so it needs to be in its own file. You can move the Violin class out of the other class simply by moving a few {s and }s.
 
andrew firth
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to keep the Violin class definition in the same file, then all you have to do is change the placement of a single brace.

Your file structure is currently this:


The structure you want is this:



Edit: as Campbell says, it's better for Violin to be public.
 
andrew firth
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the help
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

andrew firth wrote:I am trying to do this and getting frustrated with the process. What is the best way to do this without changing many lines of code around?


My suggestion (and the first one is the same as I give to ALL newbies here) is this:

1. STOP CODING, and turn your computer OFF.
2. Back up and look at the problem (and also whether you are looking at this as simply a programming exercise or a real music program). If it's the first, then simply write out, in detail, what it is you need to do. If you need to actually provide implementations for the methods, then do that too, ON PAPER, before you write another line of code.
3. Scrap what you've done so far, and start again.

Sorry if it sounds brutal, but in order to write code for a solution you must first understand it. Most of the best programmers I know spend 20% of their life coding (at the most). The rest is spent with bits of paper, whiteboards, or just down the pub thinking (at least, so they tell me).

If the problem really is to write a musical program, then "tune the instrument" strikes me as a major challenge right off the bat. The bloke who came around to tune my mum's upright had been doing it for 25 years when I knew him, and said there were still things that he didn't fully understand about the process.

Winston
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

andrew firth wrote:I am trying to do this and getting frustrated with the process. What is the best way to do this without changing many lines of code around?


My suggestion (and the first one is the same as I give to ALL newbies here) is this:

1. STOP CODING, and turn your computer OFF.
2. Back up and look at the problem (and also whether you are looking at this as simply a programming exercise or a real music program). If it's the first, then simply write out, in detail, what it is you need to do. If you need to actually provide implementations for the methods, then do that too, ON PAPER, before you write another line of code.
3. Scrap what you've done so far, and start again.

Sorry if it sounds brutal, but in order to write code for a solution you must first understand it. Most of the best programmers I know spend 20% of their life coding (at the most). The rest is spent with bits of paper, whiteboards, or just down the pub thinking (at least, so they tell me).

If the problem really is to write a musical program, then "tune the instrument" strikes me as a major challenge right off the bat. The bloke who came around to tune my mum's upright had been doing it for 25 years when I knew him, and said there were still things that he didn't fully understand about the process.

Winston



This is excellent advice in general. However, in this case it strikes me as a bit drastic. I see no problem in Andrew's Violin class, and it is very close to meeting the requirements of the assignment. You simply can't scrap everything you've done and start over every time you hit a rough spot.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Deems wrote:You simply can't scrap everything you've done and start over every time you hit a rough spot.


Oh g'won. 73 lines? Heck, I've trashed more than that in a lunch hour.

But I take your point (and perhaps you should too andrew (providing you heed the rest); all us old fart programmers are nihilists - probably something to do with growing up in the 60's).

Winston
 
Campbell Ritchie
Marshal
Posts: 79637
380
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Deems wrote: . . . I see no problem in Andrew's Violin class, . . .

You might not, but I do. I have gone down with a nasty attack of déjà vu. You will get even more déjà vu, Winston, when you see what I said last time I saw that code:

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.

The code shown here is virtually identical to the other code I saw.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Dennis Deems wrote: . . . I see no problem in Andrew's Violin class, . . .

You might not, but I do. I have gone down with a nasty attack of déjà vu. You will get even more déjà vu, Winston, when you see what I said last time I saw that code:

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.

The code shown here is virtually identical to the other code I saw.


I remembered seeing the exercise before, but I hadn't recognized the code. You have a sharp eye, it is indeed virtually identical.
 
Campbell Ritchie
Marshal
Posts: 79637
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only differences are the misplaced {} and the different student’s name. I hope he hasn’t handed in what he posted, because it will earn him a 0 for plagiarism
reply
    Bookmark Topic Watch Topic
  • New Topic