write a JAVA Programming - Classes Pick one theme for a new class. You can pick
ID: 3798564 • Letter: W
Question
write a JAVA Programming - Classes
Pick one theme for a new class. You can pick from one of the following:
Bicycle
Lawn Mower
TV Remote Control
Cell Phone
Once you pick your theme, you must use the “HAS A” question to determine the fields that you are going to include in your class.
You will WRITE JAVA code to create the class from above. In the class you are creating you will include at least for fields or attributes
include at least two constructor methods in your class. Your constructors will differ by parameter list.
You will write all required Setter, Getter, and ToString methods.
Finally, you will write a test class (this is where you place your main method). The test class will create two objects of type (Your New Class). The test class (main) will call the setters, getters and toString and any other methods you placed in the new Class.
include a UML for the new class
Explanation / Answer
class Battery {
private int battery; // used for battery power %
public int getBattery() {
return battery;
}
public Battery setBattery(int battery) {
this.battery = battery;
return this;
}
}
class CellPhone {
private Battery battery; // Has-A Relationship Means CellPhone has a battery
private String gps;
private boolean powerOn;
public Battery getBattery() {
return battery;
}
public void setBattery(Battery battery) {
this.battery = battery;
}
public String getGps() {
return gps;
}
public void setGps(String gps) {
this.gps = gps;
}
public boolean isPowerOn() {
return powerOn;
}
public void setPowerOn(boolean powerOn) {
this.powerOn = powerOn;
}
public CellPhone(Battery battery, boolean powerOn) {
super();
this.battery = battery;
this.powerOn = powerOn;
}
public CellPhone(Battery battery, String gps, boolean powerOn) {
super();
this.battery = battery;
this.gps = gps;
this.powerOn = powerOn;
}
}
class Main {
public static void main(String[] args) {
CellPhone cellPhone1 = new CellPhone(new Battery(), true);
CellPhone cellPhone2 = new CellPhone(new Battery(), "yes", false);
cellPhone1.getBattery();
cellPhone1.getGps();
cellPhone2.setBattery(new Battery().setBattery(34));
System.out.println(cellPhone2.getBattery().getBattery());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.