Question

In: Computer Science

In java Create a class named CellPhoneCase that extends your Case class – it inherits all...

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());
    }

Solutions

Expert Solution

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;
    }
}

Related Solutions

In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class....
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class. The vehicle class should contain at least 2 variables that could pertain to ANY vehicle and two methods. The truck class should contain 2 variables that only apply to trucks and one method. Create a console program that will instantiate a truck with provided member information then call one method from the truck and one method contained from the inherited vehicle class. Have these...
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
In java: -Create a class named Animal
In java: -Create a class named Animal
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED...
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED BELOW THIS QUESTION). Include the following: A variable Roof that holds the type of roof (Ex: Convertible, Hard-Top, Soft-Top) A variable Doors that holds the car’s number of doors (Ex: 2, 4) Implement the ChangeSpeed method to add 20 to the speed each time it is called. Add exception handling to the ChangeSpeed method to keep the speed under 65. Implement the sound method...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement the compareTo method to compare the triangles on the basis of similarity. Draw the UML diagram for your classes. Write a Driver class (class which instantiates the comparableTriangle objects) to determine if two instances of ComparableTriangle objects are similar (sample output below). It should prompt the user to enter the 3 sides of each triangle and then display whether or not the are similar...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
JAVA (Tree height) Define a new class named BSTWithHeight that extends BST with the following method:...
JAVA (Tree height) Define a new class named BSTWithHeight that extends BST with the following method: /** Return the height of this binary tree */ public int height() Class Name: Exercise25_01
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT