Question

In: Computer Science

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 neighbor: " + test3.getTeleponeNeighbor());
        System.out.println();

        System.out.print(test4);
        System.out.println("Telephone neighbor: " + test4.getTeleponeNeighbor());
        System.out.println();

        System.out.print(test5);
        System.out.println("Telephone neighbor: " + test5.getTeleponeNeighbor());
        System.out.println();
    }

}

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

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

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

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

Name: Bret\n
Phone: 1234567890\n
Email: None\n
Telephone neighbor: (123) 456-7891\n
\n
Name: Alice\n
Phone: 8059226966\n
Email: [email protected]\n
Telephone neighbor: (805) 922-6967\n
\n
Name: None\n
Phone: Not set\n
Email: None\n
Telephone neighbor: Cannot calculate phone number neighbor\n
\n
Name: Carlos\n
Phone: 8189998999\n
Email: [email protected]\n
Telephone neighbor: (818) 999-9000\n
\n
Name: Dan\n
Phone: 8182293899\n
Email: [email protected]\n
Telephone neighbor: (818) 229-3900\n
\n

Solutions

Expert Solution

code:

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

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

   public String getTeleponeNeighbor() {
       if(phone == "Not set"){
           return "Cannot calculate phone number neighbor";
       }else{
           String res = "(";
           res+=phone.substring(0,3)+")";
           res+=phone.substring(3,6)+"-";
           res+=Integer.parseInt(phone.substring(6,10))+1;
          
           return res;
       }
   }
}


Related Solutions

Modify Example 5.1 to add a ReadAccount method to the BankAccount class that will return a...
Modify Example 5.1 to add a ReadAccount method to the BankAccount class that will return a BankAccountconstructed from data input from the keyboard. Override ReadAccount in SavingsAccount to return an account that refers to a SavingsAccount that you construct, again initializing it with data from the keyboard. Similarly, implement ReadAccount in the CheckingAccount class. Use the following code to test your work. public static void testCode()         {             SavingsAccount savings = new SavingsAccount(100.00, 3.5);             SavingsAccount s = (SavingsAccount)savings.ReadAccount();...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called PushBottom which will add a new item to the bottom of the stack. The push bottom will increase the number of items in the stack by 1
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class...
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class Fibonacci { // Fib(N): N N = 0 or N = 1 // Fib(N-1) + Fib(N-2) N > 1 // For example, // Fib(0) = 0 // Fib(1) = 1 // Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1 // Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1 = (Fib(1) + Fib(0)) + 1 = 1 + 0 + 1...
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...
Please add the following method as a part of the UnorderedList class definition: •print_list: the method...
Please add the following method as a part of the UnorderedList class definition: •print_list: the method prints the elements of the list using the same format as a Python list (square brackets and commas as separators). In the main function of the modified UnorderedList.py, please test the new method to demonstrate that it works as expected. Please leave comments in the code to show what is done UnorderList.py file # Implementation of an Unordered List ADT as a linked list....
What is the issue is in the add method in the SongList Class? It doesn't seem...
What is the issue is in the add method in the SongList Class? It doesn't seem to add the songs when running testing. public class Song { // instance variables private String m_artist; private String m_title; private Song m_link; // constructor public Song(String artist, String title) { m_artist = artist; m_title = title; m_link = null; } // getters and setters public void setArtist(String artist) { m_artist = artist; } public String getArtist() { return m_artist; } public void setTitle(String...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
Add a method to OurQueue class that dequeues the Nth item on a queue and returns...
Add a method to OurQueue class that dequeues the Nth item on a queue and returns it. It must remove the Nth item from the Queue but leave the rest of the queue. Test it with the following code: Console.WriteLine("Testing DequeueNth"); OurQueue<string> ourQ = new OurQueue<string>(12); // Empty Q try { ourQ.DequeueNth(0); Console.WriteLine("\a Error on empty list"); } catch { Console.WriteLine("Empty Queue worked"); } for (int i = 0; i < 9; ++i) ourQ.Enqueue("a" + i); for (int i =...
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...
public class AddValueToArray { // You must define the addValueTo method, which will add // a...
public class AddValueToArray { // You must define the addValueTo method, which will add // a given value to each element of the given array. // // TODO - define your code below this comment // // DO NOT MODIFY main! public static void main(String[] args) { int[] array = new int[]{3, 8, 6, 4}; int valueToAdd = Integer.parseInt(args[0]); addValueTo(valueToAdd, array); for (int index = 0; index < array.length; index++) { System.out.println(array[index]); } } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT