aspose file tools*
The moose likes Beginning Java and the fly likes Design and implement a stringed musical instrument class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Design and implement a stringed musical instrument class" Watch "Design and implement a stringed musical instrument class" New topic
Author

Design and implement a stringed musical instrument class

andrew firth
Greenhorn

Joined: Mar 08, 2012
Posts: 4
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!
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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

Joined: Mar 08, 2012
Posts: 4
"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?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
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

Joined: Mar 08, 2012
Posts: 4
thanks
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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

Joined: Mar 08, 2012
Posts: 4
thanks for the help
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8223
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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

Joined: Mar 17, 2011
Posts: 8223
    
  23

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
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
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

Joined: Mar 12, 2011
Posts: 808
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
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Design and implement a stringed musical instrument class