Why doesn\'t this code compile for this assignment??? Guitar Class: public class
ID: 3669347 • Letter: W
Question
Why doesn't this code compile for this assignment???
Guitar Class:
public class Guitar {
public static final int LOW = 1;
public static final int MEDIUM = 2;
public static final int HIGH = 3;
private int volume;
private double price;
private String manufacturer;
private String guitarColor;
Guitar(){
volume = MEDIUM;
price = 0.0;
manufacturer = "DEFAULT";
guitarColor = "DEFAULT";
}
public void setVolume(int v){
if(v < LOW){
volume = LOW;
}
else if(v > HIGH){
volume = HIGH;
}
else{
volume = v;
}
}
public void setPrice(double p){
price = p;
}
public void setManufacturer(String m){
manufacturer = m;
}
public void setColor(String C){
guitarColor = C;
}
public int getVolume(){
return volume;
}
public double getPrice(){
return this.price;
}
public String getManufacturer(){
return this.manufacturer;
}
public String getColor(){
return this.guitarColor;
}
public void changeVolume(int volume){
setVolume(volume);
}
public String toString(){
return String.format("Volume: %d Price: %.2f Manufacturer: %s Color: %s",
volume, price, manufacturer, guitarColor);
}
}
Test Class:
public class TestGuitar {
public static void main(String args[]){
Guitar devices[] = new Guitar[3];
for(int i = 0; i < 3; ++i){
devices[i] = new Guitar();
}
devices[0].setVolume(HeadPhones.HIGH);
devices[0].setManufacturer("Gibson");
devices[0].setPrice(90.5);
devices[0].setColor("Red");
devices[1].setVolume(HeadPhones.LOW);
devices[1].setManufacturer("Fender");
devices[1].setPrice(170);
devices[1].setColor("Blue");
devices[2].setVolume(HeadPhones.MEDIUM);
devices[2].setManufacturer("Ibanez");
devices[2].setPrice(125.5);
devices[2].setColor("Red");
// displaying information using getters
for(int i = 0; i < 3; ++i){
System.out.printf("Volume: %d Price: %.2f Manufacturer: %s Color: %s ",
devices[i].getVolume(), devices[i].getPrice(),
devices[i].getManufacturer(), devices[i].getColor());
}
// displaying information using toString method
for(int i = 0; i < 3; ++i){
System.out.println(devices[i] + " ");
}
}
}
Explanation / Answer
Its compiling now.
The reason it was not compiling is that you were trying to use HeadPhones.HIGH for setting the volume in GuitarTest class.First of all HeadPhones class does not exist in current context and moreover static const LOW,MEDIUM,HIGH are part of Guitar class so use Guitar,HIGH instead of HeadPhones.HIGH
GuitarTest.java
public class TestGuitar {
public static void main(String args[]){
Guitar devices[] = new Guitar[3];
for(int i = 0; i < 3; ++i){
devices[i] = new Guitar();
}
//changed HeadPhones to Guitar as there is HeadPhones class does not exist and moreover HIGH,LOW and MEDIUM are part of
//Guitar class
devices[0].setVolume(Guitar.HIGH);
devices[0].setManufacturer("Gibson");
devices[0].setPrice(90.5);
devices[0].setColor("Red");
devices[1].setVolume(Guitar.LOW);
devices[1].setManufacturer("Fender");
devices[1].setPrice(170);
devices[1].setColor("Blue");
devices[2].setVolume(Guitar.MEDIUM);
devices[2].setManufacturer("Ibanez");
devices[2].setPrice(125.5);
devices[2].setColor("Red");
// displaying information using getters
for(int i = 0; i < 3; ++i){
System.out.printf("Volume: %d Price: %.2f Manufacturer: %s Color: %s ",
devices[i].getVolume(), devices[i].getPrice(),
devices[i].getManufacturer(), devices[i].getColor());
}
// displaying information using toString method
for(int i = 0; i < 3; ++i){
System.out.println(devices[i] + " ");
}
}
}
Output
Volume: 3
Price: 90.50
Manufacturer: Gibson
Color: Red
Volume: 1
Price: 170.00
Manufacturer: Fender
Color: Blue
Volume: 2
Price: 125.50
Manufacturer: Ibanez
Color: Red
Volume: 3
Price: 90.50
Manufacturer: Gibson
Color: Red
Volume: 1
Price: 170.00
Manufacturer: Fender
Color: Blue
Volume: 2
Price: 125.50
Manufacturer: Ibanez
Color: Red
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.