This question is regarding a homework project in my first Java programming class (online program).
The assignment is to create a “stringed instrument” class using (among other things) an array of String names representing instrument string names (“A”, “E”, etc). The idea for the 12-string is beyond the scope of the assignment (it doesn’t have to be included at all) but now that I’ve thought of it, I want to figure out how to make it work.
Part of me feels like the 12-String should have its own class, but another part of me feels that it should be in the guitar class because it’s a guitar. I suppose this will become clear as I progress but I thought I would see what kind of response I get here.
Also, why would they ask for a String[] for the instrument string names? Seems like a char[] makes more sense.
Thank you for any insight.
Here’s my code so far (it’s a work in progress):
public class Guitar {
private int numberOfStrings = 6;
private static int numberOfGuitars = 0;
private String[] stringNotes = {"E", "A", "D", "G", "B", "A"};
private boolean tuned = false;
private boolean playing = false;
public Guitar(){
numberOfGuitars++;
}
public Guitar(boolean twelveString){
if(twelveString){
stringNotes[0] = "E, E";
stringNotes[1] = "A, A";
stringNotes[2] = "D, D";
stringNotes[3] = "G, G";
stringNotes[4] = "B, B";
stringNotes[5] = "E, E";
numberOfStrings = 12;
}
}
public int getNumberOfStrings() {
return numberOfStrings;
}
public void setNumberOfStrings(int strings) {
if(strings == 12 || strings == 6) {
if(strings == 12){
stringNotes[0] = "E, E";
stringNotes[1] = "A, A";
stringNotes[2] = "D, D";
stringNotes[3] = "G, G";
stringNotes[4] = "B, B";
stringNotes[5] = "E, E";
numberOfStrings = strings;
}
if(strings == 6)
numberOfStrings = strings;
}//if
else
System.out.println("***ERROR***Guitar can only have 6 or 12 strings***ERROR***");
}
public void getStringNotes() {
for(int i = 0; i < stringNotes.length; i++){
if(i == stringNotes.length - 1)
System.out.println(stringNotes[i]);
else
System.out.print(stringNotes[i] + ", ");
}//for
}
7
The design decisions such as the one you mention should be driven by the requirements. Since it seems purely a modelling exercise, you shouldn’t worry too much.
As a general rule for less future headaches, favour inmutable classes, so I would get rid of that setter.
I’d also use enums instead of char arrays.
Finally try not repeating chunks of code. That’s just increasing the likelihood of bugs and makes it harder to understand the code.
Ah, and don’t forget to document your code: what does a method do, preconditions, postconditions, exceptions.
8
This goes a bit beyond an intro Java class, but…
Subclassing can send you down some pretty deep rabbit holes. The number of strings is only the beginning. Acoustic, electric, or acoustic-electric hybrid? If electric, solid or hollow body? How many pickups? Single or double coil (humbuckers)? Fixed or floating bridge? If acoustic, classical or steel string (classicals have a wider neck and use nylon strings)? And on and on and on. Creating a separate class or subclass for every type of guitar quickly leads to madness (and we haven’t even gotten into basses or other types of stringed instruments).
Instead of trying to track all that information by creating a bunch of subclasses of Guitar
, a better approach is to delegate tracking that information to other classes. For example, we can create a Tuning
class which tracks the number of strings (or courses, thanks Blrfl) and their tunings. We can create a Body
class that tracks whether it’s a solid or hollow body, a Pickup
class to track the type of pickup, a Neck
class to describe the width, radius, and scale length of the neck, etc. We then compose all of these separate classes into a Guitar
class1:
public class Guitar {
Body bodyStyle;
Neck neckStyle;
Tuning tuning;
...
};
Note that each of these classes may also delegate some information to other classes (for instance, the Tuning
class could delegate tracking information about individual guitar strings (gauge (thickness), wound/unwound, composition, etc.) to a GuitarString
class.
This way, we can use the same Guitar
class to specify many different types of guitars:
Guitar g = new Guitar(new Body (/* body parameters */),
new Neck (/* neck parameters */),
new Tuning (/* tuning parameters */),
...);
In general, you want to keep your class hierarchies pretty shallow, and use delegation as much as possible (when you hear or read someone say “favor composition over inheritance”, this is what they mean). This gives you more flexibility and reduces your maintenance headaches.
Knowing where to draw the line is a matter of experience and the problem at hand. It’s possible to go overboard either way. There are times where you want to subclass because you want to enforce a specific interface or behavior on all instances, and there are times where the effort of delegating to another class isn’t worth it.
1: It’s been over a year since I’ve had to write any Java, so I can’t write any really detailed examples without screwing something up
3