Should the 12-String be in its own class and why?

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
}
</code>
<code>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 } </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Guitar {
Body bodyStyle;
Neck neckStyle;
Tuning tuning;
...
};
</code>
<code>public class Guitar { Body bodyStyle; Neck neckStyle; Tuning tuning; ... }; </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Guitar g = new Guitar(new Body (/* body parameters */),
new Neck (/* neck parameters */),
new Tuning (/* tuning parameters */),
...);
</code>
<code>Guitar g = new Guitar(new Body (/* body parameters */), new Neck (/* neck parameters */), new Tuning (/* tuning parameters */), ...); </code>
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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật