Question

In: Computer Science

I'm having difficulty trying to make my code work. Create a subclass of Phone called SmartPhone....

I'm having difficulty trying to make my code work.

Create a subclass of Phone called SmartPhone. Make this subclass have a no-arg constructor and a constructor that takes a name, email, and phone. Override the toString method to return the required output.

Given Files:

public class Demo1 {

    public static void test(Phone p) {
        System.out.println("Getter test:");
        System.out.println(p.getName());
        System.out.println(p.getNumber());
    }

    public static void main(String[] args) {
        Phone test1 = new SmartPhone();
        Phone test2 = new SmartPhone("Alice", "8059226966", "[email protected]");
        System.out.println(test1);
        System.out.println(test2);
        System.out.println(test1);

        test(test1);
        test(test2);
    }

}

And

public class Phone
{
    protected String name;
    protected long number;

    public Phone() {
        this("None", -1);
    }

    public Phone(String name) {
        this(name, -1);
    }

    public Phone(String name, long number) {
        this.name = name;
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public long getNumber() {
        return number;
    }
}

This is my code and please give me any tips or suggestions. Thanks!

public class SmartPhone extends Phone
{
    private String email;
    private String format;


    public SmartPhone()
    {

    }

    public SmartPhone(String name, String phoneNumber, String email)
    {

        super(name, Long.parseLong(phoneNumber));


        format = "Name: " + " " + "\n"
                + "Phone: " + " " + "\n"
                + "Email: " + " " + "\n"
                + "\n" + "Name: " + name + "\n"
                + "Phone: " + phoneNumber + "\n"
                + "Email: " + email + "\n";


    }

    @Override
    public String toString()
    {
        return format;
    }

}

Solutions

Expert Solution

You can follow this programming style.

/***********************************Phone.java*****************************/

public class Phone {

   /*
   * private data field, always keep data field to private and give access through
   * getter and setter
   */
   private String name;
   private long number;

   // no argument constructor
   public Phone() {
       this("None", -1);
   }

   /**
   *
   * @param name
   */
   public Phone(String name) {
       this(name, -1);
   }

   /**
   *
   * @param name
   * @param number
   */
   public Phone(String name, long number) {
       this.name = name;
       this.number = number;
   }

   // return name
   public String getName() {
       return name;
   }

   // return number
   public long getNumber() {
       return number;
   }

   @Override
   public String toString() {
       return "Name: " + name + "\n" + "Phone: " + number;
   }

}

/**********************************SmartPhone.java*****************************/

public class SmartPhone extends Phone {

   // private data field
   private String email;

   // no argument constructor
   public SmartPhone() {
       this.email = "None";
   }

   /**
   *
   * @param name
   * @param phoneNumber
   * @param email
   */
   public SmartPhone(String name, String phoneNumber, String email) {

       super(name, Long.parseLong(phoneNumber));
       this.email = email;

   }

   // return email
   public String getEmail() {
       return email;
   }

   // no need to declare extra global variable for set the format
   @Override
   public String toString() {

       return super.toString() + "\n" + "Email: " + email + "\n";
   }

}

/*********************************Demo1.java****************************/

public class Demo1 {

   //method test
   public static void test(Phone p) {
       System.out.println("Getter test:");
       System.out.println(p.getName());
       System.out.println(p.getNumber());
   }

   public static void main(String[] args) {
       //creating object
       Phone test1 = new SmartPhone();
       Phone test2 = new SmartPhone("Alice", "8059226966", "[email protected]");
       //display toString
       System.out.println(test1.toString());
       System.out.println(test2.toString());
       //test getters
       test(test1);
       test(test2);
   }

}

/***********************output********************************/

Name: None
Phone: -1
Email: None

Name: Alice
Phone: 8059226966
Email: [email protected]

Getter test:
None
-1
Getter test:
Alice
8059226966

Please let me know if you have any doubt or modify the answer. Thanks :)


Related Solutions

I'm having a very hard time getting this c++ code to work. I have attached my...
I'm having a very hard time getting this c++ code to work. I have attached my code and the instructions. CODE: #include using namespace std; void printWelcome(string movieName, string rating, int startHour, int startMinute, char ampm); //menu function //constants const double TICKET_PRICE = 9.95; const double TAX_RATE = 0.095; const bool AVAILABLE = true; const bool UNAVAILABLE = false; int subTotal,taxAdded, totalCost; // bool checkAvailability( int tickets, int& seatingLeft){ //checks ticket availability while(tickets > 0 || tickets <= 200) //valid...
XML and XSL I'm trying to style my XML code but it's not implementing the XSL...
XML and XSL I'm trying to style my XML code but it's not implementing the XSL file when I run it even though the file is referenced. Any help? XML code: <?xml version="1.0"?> <?xml-stylesheet href="textbooks.xsl" type="text/xsl" ?> <textbooks> <textbook> <title> Introduction to Design and Analysis of Algorithms</title> <authors> <author> <firstName>Anany</firstName> <lastName>Levitin</lastName> </author> </authors> <publisher> <name>Ed, Pearson</name> <website>https://www.pearson.com</website> </publisher> <Year-of-Publication>2011</Year-of-Publication> <ISBN>978-0132316811</ISBN> <book-specific-website></book-specific-website> <edition>3rd edition</edition> <cover-type>Paperback</cover-type> </textbook> <textbook> <title>Software Engineering: A Practitioner’s Approach</title> <authors> <author> <firstName>Roger</firstName> <lastName>Pressman</lastName> </author> </authors> <publisher> <name>Ed, McGraw-Hill</name>...
I am trying to make a new code that uses functions to make it. My functions...
I am trying to make a new code that uses functions to make it. My functions are below the code. <?php */ $input; $TenBills = 1000; $FiveBills = 500; $OneBills = 100; $Quarters = 25; $Dimes = 10; $Nickels = 5; $Pennies = 1; $YourChange = 0; $input = readline("Hello, please enter your amount of cents:\n"); if(ctype_digit($input)) { $dollars =(int)($input/100); $cents = $input%100;    $input >= $TenBills; $YourChange = (int)($input/$TenBills); $input -= $TenBills * $YourChange; print "Change for $dollars dollars...
I'm having problems with my java code not correctly spellchecking certain words. 1) Determine if a...
I'm having problems with my java code not correctly spellchecking certain words. 1) Determine if a word entered by the user is spelled correctly. A word is considered correct if it's found in dictionary.txt (see required output to get file). these are the input and output that are suppose to come out i already have the dictionary.txt i just don't know hoe to set it up someone please help me Standard Input                 Files in the same directory glimmer hello...
I'm trying to study for my General Chemistry II Exam, but I want to make sure...
I'm trying to study for my General Chemistry II Exam, but I want to make sure that I don't miss anything important! If you could provide your input on the following concepts, that would be greatly appreciated! Thank you so much!(: Chapter 17 – Chemical Equilibrium I. Chemical Equilibria             A. Equilibrium constant             B. Homogeneous equilibria             C. Heterogeneous equilibria             D. Molar concentrations             E. Contribution of a pure liquid or a pure solid to the equilibrium constant...
I'm trying to study for my General Chemistry II Exam, but I want to make sure...
I'm trying to study for my General Chemistry II Exam, but I want to make sure that I don't miss anything important! If you could provide your input on the following concepts, that would be greatly appreciated! Thank you so much!(: Chapter 16 - Chemical Kinetics I. Reaction Rates             A. Definition of reaction rate             B. Mathematical expression for Reaction rate             C. Instantaneous rate             D. Initial rate             E. Rate constant             F. Rate law             G....
I'm having trouble with validating this html code. Whenever I used my validator, it says I...
I'm having trouble with validating this html code. Whenever I used my validator, it says I have a stray end tag: (tbody) from line 122 to 123. It's the last few lines. Thanks and Ill thumbs up whoever can help solve my problem. Here's my code: <!DOCTYPE html> <html lang="en"> <head> <title>L7 Temperatures Fields</title> <!--    Name:    BlackBoard Username:    Filename: (items left blank for bb reasons    Class Section: (blank)    Purpose: Making a table to demonstrate my...
Working with Python. I am trying to make my code go through each subject in my...
Working with Python. I am trying to make my code go through each subject in my sample size and request something about it. For example, I have my code request from the user to input a sample size N. If I said sample size was 5 for example, I want the code to ask the user the following: "Enter age of subject 1" "Enter age of subject 2" "Enter age of subject 3" "Enter age of subject 4" "Enter age...
I'm having a problem getting my code to function correct, *num_stu keeps losing its value when...
I'm having a problem getting my code to function correct, *num_stu keeps losing its value when ever another function is called from the class. Can someone proof read this for me and let me know what'd up? Header.h file #include <iostream> using namespace std; class report {    private:        int* num_stu;         int xx;        int* id;        double* gpa;    public:                report(int x) {             num_stu = &x;             xx = x;            ...
A surgeon is having difficulty trying to get a Steinmann pin to fit in a treatment...
A surgeon is having difficulty trying to get a Steinmann pin to fit in a treatment for a tibial fracture. In a last ditch effort to finish the operation, he bends the pin to make it go in. What corrosion problems might this cause?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT