Create your own Java class that represents your favorite musical instrument. You
ID: 3662030 • Letter: C
Question
Create your own Java class that represents your favorite musical instrument. Your musical instrument class should have at least 3 constants, 5 private data fields, getters and setters for each private data field, a toString() method, and three additional methods of your choice. Create a test class that constructs at least 5 of your musical instrument objects. For each of the objects constructed demonstrate the use of each of the methods. Be sure to use your IDE to accomplish this assignment. You can pick any instrument you want. When designing your class, think about what would make sense to describe and use the instrument. For example, if you selected a trumpet, you might need to provide the number of valves, the manufacturer, if the instrument is using a mute, the volume or even notes the trumpet is playing
Explanation / Answer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package instrumentproject;
public class favoriateMusicalInstrument {
// 5 private data fields
private String instrument1;
private String instrument2;
private String instrument3;
private String instrument4;
private String instrument5;
// 3 constant variables
final int const1=0;
final int const2 =0;
final int const3=0;
//getter and setter methods
public String getInstrument1() {
return instrument1;
}
public void setInstrument1(String instrument1) {
this.instrument1 = instrument1;
}
public String getInstrument2() {
return instrument2;
}
public void setInstrument2(String instrument2) {
this.instrument2 = instrument2;
}
public String getInstrument3() {
return instrument3;
}
public void setInstrument3(String instrument3) {
this.instrument3 = instrument3;
}
public String getInstrument4() {
return instrument4;
}
public void setInstrument4(String instrument4) {
this.instrument4 = instrument4;
}
public String getInstrument5() {
return instrument5;
}
public void setInstrument5(String instrument5) {
this.instrument5 = instrument5;
}
//tostring() method
@Override
public String toString(){
return "your in instrumentation class";
}
// own 3 methods
public void display(){
System.out.println("In display");
}
public void show(){
System.out.println("In show");
}
public void visble(){
System.out.println("In visble");
}
}
//-----------------------------------------------------------------------------------------------------------------------------
//Test Class here
//---------------------------------------------------------------------------------------------------------------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package instrumentproject;
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//Creating 5 objects
favoriateMusicalInstrument obj1 = new favoriateMusicalInstrument();
favoriateMusicalInstrument obj2 = new favoriateMusicalInstrument();
favoriateMusicalInstrument obj3 = new favoriateMusicalInstrument();
favoriateMusicalInstrument obj4 = new favoriateMusicalInstrument();
favoriateMusicalInstrument obj5 = new favoriateMusicalInstrument();
//calling and assigning values to objects.
obj1.setInstrument1("trumpet");
obj2.setInstrument2("paino");
obj3.setInstrument3("tabla");
obj4.setInstrument4("viaolin");
obj5.setInstrument5("guitar");
//getting the values
String trumpet = obj1.getInstrument1();
String paino = obj2.getInstrument2();
String tabla = obj3.getInstrument3();
String vialoin = obj4.getInstrument4();
String guitar = obj5.getInstrument5();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.