Question

In: Computer Science

How do I make it that it rounds up? it's required that the numbers be (555)...

How do I make it that it rounds up?

it's required that the numbers be (555) 213-000, instead, I get (555) 212-000.

///// Input /////

Zed
5552129999
[email protected]

////// Required Output //////

New number creation tool\n
Enter name\n
Enter phone number\n
Enter email\n
Name: Zed\n
Phone: 5552129999\n
Email: [email protected]\n
Telephone neighbor: (555) 213-0000\n

Here is my 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 roundUp = "(";
            roundUp += phone.substring(0,3) + ") ";
            String num1 = Integer.parseInt(phone.substring(3,6)) + "-";
            String num2 = String.valueOf(Integer.parseInt(phone.substring(6,10)) + 1);

            if (num2.length() > 4)
            {
                num2 = num2.substring(num2.length() - 4);
            }
            if (num2.length() < 4)
            {
                num2 = String.format("%04d", Integer.parseInt(num2));
            }
                roundUp += num1;
                roundUp += num2;

                return roundUp;
            }
    }
}

And:

import java.util.Scanner;

public class Demo5
{
    public static void main(String args[])
    {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("New number creation tool");

        System.out.println("Enter name");
        String name = keyboard.nextLine();

        System.out.println("Enter phone number");
        String phoneNumber = keyboard.nextLine();

        System.out.println("Enter email");
        String email = keyboard.nextLine();

        Phone test1 = new SmartPhone(name, phoneNumber, email);

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

Given File:

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

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 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 roundUp = "(";
            String nextPhone = String.valueOf(Long.parseLong(phone)+1);
            roundUp += nextPhone.substring(0,3) + ") ";
            roundUp += nextPhone.substring(3,6) + "-";
            roundUp += nextPhone.substring(6);
            return roundUp;
        }
    }
}

Related Solutions

There is a special calculator that when used to add numbers up, it always rounds up...
There is a special calculator that when used to add numbers up, it always rounds up or down the number to the closest integer and ignore the decimals. For example, you want to add 1.1+2+3.6, it will show can calculate as 1+2+4, etc. Assume the error from each adding follows uniform distribution on (-0.5, 0.5). When adding 1500 numbers together, what is the probability of the absolute value of total error is greater than 15? How many numbers can you...
How do I make the series graph
How do I make the series graph
How do I do this: Write a program that can read a text file of numbers...
How do I do this: Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!
HOW DO I ACCESS THE HEAD OF A LINKED LIST FROM INT MAIN () WHEN IT'S...
HOW DO I ACCESS THE HEAD OF A LINKED LIST FROM INT MAIN () WHEN IT'S IN ANOTHER CLASS. HERE IS MY CODE FOR MY MAIN AND HEADER FILES. HEADER FILE: #ifndef HANOISTACK_H #define HANOISTACK_H #include <iostream> using namespace std; class HanoiStack { //class private: struct Disc{ //linked list for towers int num; Disc* next; }; Disc* head; public: HanoiStack(){ //constructor head = nullptr; }; //HanoiStack operator+=(const Disc&); //HanoiStack operator<<(const Disc&); void push(int); //push function int pop(); //pop function void...
How much must the bank put into it's required reserves?
Bank Assets      Bank Liabilities  Total Reserves $500,000,000 Demand Deposits $500,000,000LoansGovernment Bonds                Total $500,000,000 $500,000,000Answer the following in successive order..If the required reserve ratio is set at 20% by the FEDHow much must the bank put into it's required reserves?How much will be left in excess reserves?How much can the bank lend at max?At maximum lending, then the money multiplier will be equal to?At maximum lending, the money supply in the economy will increase by how much?If the bank...
I have the solution.. I just do not know how they came up with some of...
I have the solution.. I just do not know how they came up with some of the answers. e.g. sales return and allowande. Exercise 5-11 Calculating income statement components L01,5 Referring to Exhibit 5.15 calculate the missing amounts (round to two decimal places). Company A Company B 2020 2019 2020 2019 Sales     $263,000.00 $187,000.00 ? 114200.00 $48,500.00 Sales Discount $2,630.00 ? 1350.00 $1,200.00 $570.00 Sales Return and Allowance ? 51570 $16,700.00 $6,200.00 ? 2430.00 Net Sales ? 208800.00 $168,950.00...
In Excel: How do I turn a series of dates into a sequence of numbers (example...
In Excel: How do I turn a series of dates into a sequence of numbers (example 1,2, 3)?
Inoculation and Sterilization Experimental Set Up for Microbiology: I was wondering how I can do an...
Inoculation and Sterilization Experimental Set Up for Microbiology: I was wondering how I can do an experiment inoculating different age classes of a plant species with fungi. For example seeds can be sterilized and then inoculated and put into a sterile growth medium, but what if I wanted to do an older plant? Would that plant need to be grown in a sterilized environment up until inoculation to the age I want? Would it need to be sterilized again for...
how do I make a histogram. I am using the example from the book Essentials of...
how do I make a histogram. I am using the example from the book Essentials of Statistics Chapter 2.2, Problem 9BSC.
How can I make a problem like this simpler? and what equation do I use for...
How can I make a problem like this simpler? and what equation do I use for part D? Thanks! A researcher wonders if mental simulation will help one more successfully achieve one’s goals. To test this idea, the researcher recruited six students from a general psychology class, several weeks before their first midterm. After their first midterm, the researcher got the students’ scores on this test from their professor. Several weeks later, each of the students was brought into the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT