Please write the Java code for a Phone class that represents a mobile phone

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Please write the Java code for a Phone class that represents a mobile phone, as detailed below:

  • It should have instance variables (attributes) for make, model, color (all stored as Strings) and capacity (stored as int: the storage capacity in GB)
  • Please define one constructor with parameters for all those attributes.
  • The Phone class will be immutable, except its storage capacity can be changed.  So please write only one mutator method, called .setCapacity.
  • Please write accessor methods for each instance variable.
  • Please write a .toString() method to display all the Phone's information.
  • Please write a .equals method to compare two Phone objects, returning true if all their attributes are the same.

 

Finish the Jave program to show the outputs bellow:

Code:

class Main 
{
  public static void main(String[] args) 
  {
    Phone craigPhone, bettoPhone, tobyPhone;

    craigPhone = new Phone("Samsung", "Galaxy S21 5G", "Phantom Violet", 256);
    bettoPhone = new Phone("Apple", "iPhone 13 Pro", "Graphite", 512);
    tobyPhone = new Phone("Apple", "iPhone 13 Pro", "Graphite", 512);

    System.out.println("The brightest color here is: " + craigPhone.getColor());

    if(craigPhone.getCapacity() < bettoPhone.getCapacity())
    {
      System.out.println("This phone:");
      System.out.println(craigPhone);
      System.out.println("has lower capacity than the:");
      System.out.println(bettoPhone);
    }
    else
    {
      System.out.println("Error! This phone:");
      System.out.println(craigPhone);
      System.out.println("should have lower capacity than the:");
      System.out.println(bettoPhone);
    }

    if(bettoPhone.equals(tobyPhone))
    {
      System.out.println("We have two of these:");
      System.out.println(bettoPhone);
    }
    else
    {
      System.out.println("Error! This phone:");
      System.out.println(tobyPhone);
      System.out.println("should be equal to:");
      System.out.println(bettoPhone);
    }

    tobyPhone.setCapacity(1000);   // upgrade to 1TB
    if(bettoPhone.equals(tobyPhone))
    {
      System.out.println("Error! After upgrade, this phone:");
      System.out.println(tobyPhone);
      System.out.println("should have more capacity than:");
      System.out.println(bettoPhone);
    }
    else
    {
      System.out.println("After upgrade they aren't equal, because this phone:");
      System.out.println(tobyPhone);
      System.out.println("has more capacity than:");
      System.out.println(bettoPhone); 
    }      
  }
}

 

Output:

The brightest color here is: Phantom Violet
This phone:
Samsung Galaxy S21 5G Phantom Violet with 256 GB
has lower capacity than the:
Apple iPhone 13 Pro Graphite with 512 GB
We have two of these:
Apple iPhone 13 Pro Graphite with 512 GB
After upgrade they aren't equal, because this phone:
Apple iPhone 13 Pro Graphite with 1000 GB
has more capacity than:
Apple iPhone 13 Pro Graphite with 512 GB
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY