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 trouble trying to create the italian, polish, and netherlands flag in C Here's my...
I'm having trouble trying to create the italian, polish, and netherlands flag in C Here's my code so far: #include<stdio.h> int main(){    int country_choice;    fprintf(stderr, "Enter the country_code of the chosen flag.(1 for Poland, 2 for Netherlands, 3 for Italy)");    fscanf(stdin, "%d", &country_choice);    switch (country_choice) { case 1: printf("Poland Flag\n"); printf("%c%c%c", 255,255,255); printf("%c%c%c", 220,20,60);    break;       case 2: printf("Italian Flag\n"); printf("%c%c%c", 0,146,70); printf("%c%c%c", 225,255,255); printf("%c%c%c", 206,43,55); break;    case 3: printf("Netherlands Flag\n"); printf("%c%c%c", 174,28,40);...
I'm having trouble with my do while loop. I'm trying to get it where if the...
I'm having trouble with my do while loop. I'm trying to get it where if the user enter's 3 after whatever amount of caffeinated beverages they've entered before then the loop will close and the rest of my code would proceed to execute and calculate the average price of all the caffeinated beverages entered. Can someone please help me with this? Here's my Code: import java.util.Scanner; public class Main { public static void main(String[] args) { CaffeinatedBeverage[] inventory = new...
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...
Invalid entry code in python my code is pasted below. The last elif statement, I'm trying...
Invalid entry code in python my code is pasted below. The last elif statement, I'm trying to get the program to print "invalid entry" if the entry for user_input is invalid. The first user prompt should only allow for numbers 1-10 and "exit" and "quit" import math user_prompt = """Enter number of operation that you want to execute <type exit or quit to end program>: 1 sin(x) 2 cos(x) 3 tan(x) 4 asin(x) 5 acos(x) 6 atan(x) 7 ln(x) 8...
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>...
Hello! I'm trying to work on a python lab in my class, and we basically have...
Hello! I'm trying to work on a python lab in my class, and we basically have to make this method play_interactive Now for the fun bit. The function play_interactive will take just one argument --- the length of patterns to use as keys in the dictionary --- and will start an interactive game session, reading either '1' or '2' from the player as guesses, using the functions you wrote above and producing output as shown in the sample game session...
I'm trying to get my code to loop back to let the user input multiple patients....
I'm trying to get my code to loop back to let the user input multiple patients. import java.util.Scanner; public class XYZ {    public static void main(String[] args) {           String response;        do{            //import scanner for input            Scanner input = new Scanner(System.in);               //prompt user input for last name            System.out.print("Enter last name ");            //output for last name            String...
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...
Please finish this code and make it work. This is my homework and my professor wont...
Please finish this code and make it work. This is my homework and my professor wont allow me to change the code in main, it's a set of huge numbers need to sort by radixsort with bit operation. #include <iostream> using namespace std; void radixLSD_help(int *items, int length, int bit) {    // – Count number of items for each bucket.    // – Figure out where each bucket should be stored (positions // of the first and last element...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT