In: Computer Science
In java
Create a class named CellPhoneCase that extends your Case class
– it
inherits all the fields and methods from the Case class. It should
also contain:
One field for a CellPhone object. Be sure to put in the
appropriate access modifier.
(private, public)
A constructor with 3 parameters – owner’s name, Case color, the
phone number of the
cell phone that will be in the Case. This constructor MUST call the
super class’s
constructor. Then set the cell phone property to a newly created
CellPhone object.
The cell phone owner’s name and phone number should be set to
the values passed to
this constructor.
One accessor named getCellPhone that returns the
CellPhone that is in this Case.
An instance method named quietPhone that sets the do not disturb
property of the
phone in this Case to true.
A public static method called swapPhones that accepts two
CellPhoneCases. This
method swaps the CellPhones that are in the CellPhoneCases.
Override the toString method to return the toString value of the
super class followed
by ‘ , ‘ followed by the toString value of the CellPhone that is in
the Case.
A main method that creates two instances of the CellPhoneCase
class. One with the
owner named ‘Tim’, color set to ‘black’ and phone number set to
‘111-222-3333’. The
second with the owner named ‘Neena’, color set to ‘blue’ and phone
number set to
‘444-555-6666’. Print both cell phone Case objects. Then call
CellPhoneCase.swapPhones method to swap the phones in Tim and
Neena’s cell
phone Cases. Print both cell phone Case objects again.
public static class Case { String ownerName; String color; Case(String name, String color) { this.ownerName = name; this.color = color; } public String getOwnerName(){ return ownerName; } public String getColor(){ return color; } public String toString(){ return "Case Owner: " + ownerName + ", " + "Color: "+ color; }
Needs to pass these tests
public class CellPhoneCaseTest{ @Test public void testingInheritance() { System.out.println("Testing CellPhoneCase class"); // Testing whether CellPhoneCase is derived from Case class // instances of CellPhoneCase ARE also instances of Case CellPhoneCase myCase = new CellPhoneCase("Me", "gold", "123-456-7890"); assertTrue(myCase instanceof Case); // Testing that CellPhoneCase class did not override the getOwnerName and getColor methods try { Method meth = CellPhoneCase.class.getDeclaredMethod("getOwnerName"); System.out.println("getOwnerName should not be defined in CellPhoneCase class. It is" + "inherited from the Case class."); } catch (NoSuchMethodException e) { } try { Method meth = CellPhoneCase.class.getDeclaredMethod("getColor"); System.out.println("getColor should not be defined in CellPhoneCase class. It is " + "inherited from the Case class."); } catch (NoSuchMethodException e) { } } @Test public void testing_constructor() { System.out.println("Testing CellPhoneCase constructor"); CellPhoneCase myCase = new CellPhoneCase("Me", "gold", "123-456-7890"); assertEquals("Me", myCase.getOwnerName()); assertEquals("gold", myCase.getColor()); assertEquals("Me", myCase.getCellPhone().getOwnerName()); assertEquals("123-456-7890", myCase.getCellPhone().getPhoneNumber()); // Initially phone's ring tone should be set to "beep" assertEquals("beep", myCase.getCellPhone().getRingTone()); // Initally phone should have do not disturb set to false assertFalse(myCase.getCellPhone().isDoNotDisturb()); }
CellPhoneCase class:
public class CellPhoneCase extends Case {
private CellPhone cellPhone;
CellPhoneCase(String name, String color, String phoneNumber) {
super(name, color);
cellPhone = new CellPhone(phoneNumber, name);
super.setCellPhone(cellPhone);
}
public static void swapPhones(CellPhoneCase cellPhoneCase1, CellPhoneCase cellPhoneCase2) {
String t1 = cellPhoneCase1.ownerName;
String t2 = cellPhoneCase1.color;
String t3 = cellPhoneCase1.getCellPhone().cellPhoneNumber;
cellPhoneCase1.ownerName = cellPhoneCase2.ownerName;
cellPhoneCase1.color = cellPhoneCase2.color;
cellPhoneCase1.cellPhone = new CellPhone(cellPhoneCase2.cellPhone.cellPhoneNumber, cellPhoneCase2.ownerName);
cellPhoneCase2.ownerName = t1;
cellPhoneCase2.color = t2;
cellPhoneCase2.cellPhone = new CellPhone(t3, t1);
}
public String toString(){
return super.toString() + "," + super.cellPhone.toString();
}
public static void main(String[] args) {
CellPhoneCase c1 = new CellPhoneCase("Tim", "Black", "111-222-3333");
CellPhoneCase c2 = new CellPhoneCase("Neena", "Blue", "444-555-6666’");
System.out.println(c1.toString() + "\n" + c2.toString());
CellPhoneCase.swapPhones(c1, c2);
System.out.println(c1.toString() + "\n" + c2.toString());
}
}
CellPhone class:
public class CellPhone {
String cellPhoneNumber;
String ownerName;
boolean doNotDisturb;
CellPhone(String cellPhoneNumber, String ownerName) {
this.cellPhoneNumber = cellPhoneNumber;
this.ownerName = ownerName;
}
public String getCellPhoneNumber() {
return cellPhoneNumber;
}
public void quietPhone() {
this.doNotDisturb = true;
}
public boolean isDoNotDisturb() {
return doNotDisturb;
}
public String toString(){
return "Case Owner: " + ownerName + ", " + "Number: "+ cellPhoneNumber;
}
}
Case Class:
public class Case {
String ownerName;
String color;
CellPhone cellPhone;
Case(String name, String color) {
this.ownerName = name;
this.color = color;
this.cellPhone = cellPhone;
}
public String getOwnerName(){
return ownerName;
}
public String getColor(){
return color;
}
public void setCellPhone(CellPhone cellPhone) {
this.cellPhone = cellPhone;
}
public CellPhone getCellPhone() {
return cellPhone;
}
public String toString(){
return "Case Owner: " + ownerName + ", " + "Color: "+ color;
}
}