HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Stringed musical instrument class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
stringedinstrumentclassmusical

Problem

I humbly submit the following for critique. This was my final project for an Intro To Java Programming class ("handed-in" already, I can't make changes).

The abundant commenting is required by the professor.


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



  • to tune the instrument



  • to start the instrument playing



  • to stop the instrument from playing.




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



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.

The Guitar class:

```
//data fields include all required by assignment plus numberOfGuitars and numberOfTwelveStringGuitars
private int numberOfStrings = 6;
private static int numberOfGuitars = 0, numberOfTwelveStringGuitars = 0;
StringPackage stringPackage = new StringPackage("guitar", 6);
private boolean tuned = false;
private boolean playing = false;

//simple constructor, built with tuned and playing false
public Guitar(){
numberOfGuitars++;
}

//constructor for different number of strings
//same basic build re: tuning and playing
public Guitar(int numberOfStrings) throws IOException{
this.numberOfStrings = numberOfStrings;
stringPackage = new StringPackage("guitar", numberOfStrings);
if(numberOfStrings == 12) numberOfTwelveStringGuitars++;
}

//displays string notes using methods in

Solution

C++ guy here so maybe I am wrong about this but it looks like you've got a bug. Does java automatically call no-argument constructors from constructors that take arguments? If not then Guitar(int numberOfStrings) doesn't increment "numberOfGuitars".

Also it would make more design sense to have "StringPackage" be ignorant of any particular instrument type and instead just have one constructor that takes the number of strings and the "tuning" String and maybe another String which is the instrument name. This leaves the the class open to being a container for any type of stringed instrument without ever having to modify the class to support new instrument types.

Context

StackExchange Code Review Q#19699, answer score: 5

Revisions (0)

No revisions yet.