Question

In: Computer Science

Add more methods to SmartPhone. hasPhoneNumber: return true if the phone number has been initialized getAreaCode:...

Add more methods to SmartPhone.

  • hasPhoneNumber: return true if the phone number has been initialized
  • getAreaCode: returns the area code of the phone number
  • getPrefix: returns the prefix of the phone number
  • getLineNumber: returns the line number of the phone number.

Given Files:

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

And:

public class Demo3 {

    public static void test(SmartPhone f)
    {
        System.out.print(f);
        if (f.hasPhoneNumber()) {
            System.out.println("Area code: " + f.getAreaCode());
            System.out.println("Prefix: " + f.getPrefix());
            System.out.println("Line number: " + f.getLineNumber());
        }
        System.out.println();
    }

    public static void main(String[] args) {
        SmartPhone test1 = new SmartPhone("Bret", "1234567890");
        SmartPhone test2 = new SmartPhone("Alice", "8059226966", "[email protected]");
        SmartPhone test3 = new SmartPhone();
        test(test1);
        test(test2);
        test(test3);
    }

}

And:

public class SmartPhone extends Phone
{
    private String email;
    private String phone;
    private String phone2;

    public SmartPhone()
    {
        super("None",-1);
        phone = "Not set";
        email = "None";
        phone2 = "Not set";
    }

    public SmartPhone(String name, String phone)
    {
        super(name, Long.parseLong(phone));
        this.phone = phone;
        this.email = "None";
    }

    public SmartPhone(String name, String phone, String email)
    {
        super(name, Long.parseLong(phone));
        this.email = email;
        this.phone = phone;
    }

    public String toString()
    {
        return "Name: " + name + "\n" +
                "Phone: " + phone + "\n" +
                "Email: " + email + "\n";
    }
}

////////////////////////////////////////// Output //////////////////////////////////////////

Name: Bret\n
Phone: 1234567890\n
Email: None\n
Area code: 123\n
Prefix: 456\n
Line number: 7890\n
\n
Name: Alice\n
Phone: 8059226966\n
Email: [email protected]\n
Area code: 805\n
Prefix: 922\n
Line number: 6966\n
\n
Name: None\n
Phone: Not set\n
Email: None\n
\n

Solutions

Expert Solution

public class SmartPhone extends Phone
{
    private String email;
    private String phone;
    private String phone2;

    public SmartPhone()
    {
        super("None",-1);
        phone = "Not set";
        email = "None";
        phone2 = "Not set";
    }

    public SmartPhone(String name, String phone)
    {
        super(name, Long.parseLong(phone));
        this.phone = phone;
        this.email = "None";
    }

    public SmartPhone(String name, String phone, String email)
    {
        super(name, Long.parseLong(phone));
        this.email = email;
        this.phone = phone;
    }

    public String toString()
    {
        return "Name: " + name + "\n" +
                "Phone: " + phone + "\n" +
                "Email: " + email + "\n";
    }

    public boolean hasPhoneNumber() {
        return !phone.equals("Not set");
    }

    public String getAreaCode() {
        return phone.substring(0, 3);
    }

    public String getPrefix() {
        return phone.substring(3, 6);
    }

    public String getLineNumber() {
        return phone.substring(6);
    }
}


Related Solutions

Add the method getTelephoneNeighbor to the SmartPhone class. Make this method return a version of the...
Add the method getTelephoneNeighbor to the SmartPhone class. Make this method return a version of the phone number that's incremented. Given Files: public class Demo4 { public static void main(String[] args) { SmartPhone test1 = new SmartPhone("Bret", "1234567890"); SmartPhone test2 = new SmartPhone("Alice", "8059226966", "[email protected]"); SmartPhone test3 = new SmartPhone(); SmartPhone test4 = new SmartPhone("Carlos", "8189998999", "[email protected]"); SmartPhone test5 = new SmartPhone("Dan", "8182293899", "[email protected]"); System.out.print(test1); System.out.println("Telephone neighbor: " + test1.getTeleponeNeighbor()); System.out.println(); System.out.print(test2); System.out.println("Telephone neighbor: " + test2.getTeleponeNeighbor()); System.out.println(); System.out.print(test3); System.out.println("Telephone...
JAVA Add static methods largest and smallest to the Measurable interface. The methods should return the...
JAVA Add static methods largest and smallest to the Measurable interface. The methods should return the object with the largest or smallest measure from an array of Measurable objects.
What is the probability that seven-digit phone number has one or more repeated digits ? The...
What is the probability that seven-digit phone number has one or more repeated digits ? The leading number can not be zero (0).
A major smartphone manufacturer is launching the newest version of its mobile phone. The company has...
A major smartphone manufacturer is launching the newest version of its mobile phone. The company has media advertising planned already, but it is considering complementing it with a public relations (PR) campaign. Discuss the target audience, the objectives, and the advantages of a PR campaign? 600 words A major smartphone manufacturer is launching the newest version of its mobile phone. The company has media advertising planned already, but it is considering complementing it with a public relations (PR) campaign. Discuss...
The smartphone market has been dominated by Apple, but recently the Droid has been able to...
The smartphone market has been dominated by Apple, but recently the Droid has been able to leverage Google’s information services into market gains while Blackberry, known for its se- cure business-oriented network, has attempted to become more attractive with a “friendlier” interface. At the same time, a number of less ca- pable fringe firms are emerging. How do these features fit into an industrial organization (IO) view of the market versus a resource-based view (RBV)?
Add your own method (or use one or more of the existing methods) to insert the...
Add your own method (or use one or more of the existing methods) to insert the following set of numbers (1, 5, 19, 7, 23, 17, 2) in a linked list (use one function-call per number, and preserve the order of insertion). Once inserted print the linked list such that the output displays the numbers in reverse order. (2, 17, 23, 7, 19, 5, 1) JAVA CODE BELOW class aNode { char data; aNode next; aNode(char mydata) { // Constructor...
•          It has been said that a smartphone is a microcomputer in your hand. Discuss the security...
•          It has been said that a smartphone is a microcomputer in your hand. Discuss the security implications of this statement. •          What management, organizational, and technology issues must be addressed by smartphone security? •          What problems do smartphone security weaknesses cause for businesses?
Given a String variable named sentence that has been initialized, write an expression whose value is...
Given a String variable named sentence that has been initialized, write an expression whose value is the the very last character in the String referred to by sentence. write it in python
Add the following methods to the singly list implementation below. int size(); // Returns the number...
Add the following methods to the singly list implementation below. int size(); // Returns the number of nodes in the linked list bool search(string query); // Returns if the query is present in the list void add(List& l); // // Adds elements of input list to front of "this" list (the list that calls the add method) ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // slist.cpp #include <string> #include "slist.h" using namespace std; Node::Node(string element) : data{element}, next{nullptr} {} List::List() : first{nullptr} {} // Adds to...
Add the following methods to the singly list implementation below. int size(); // Returns the number...
Add the following methods to the singly list implementation below. int size(); // Returns the number of nodes in the linked list bool search(string query); // Returns if the query is present in the list void add(List& l); // // Adds elements of input list to front of "this" list (the list that calls the add method) #include <string> #include "slist.h" using namespace std; Node::Node(string element) : data{element}, next{nullptr} {} List::List() : first{nullptr} {} // Adds to the front of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT