In: Computer Science
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; } }
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; } } }