In: Computer Science
Using the given file, ask the user for a name, phone number, and email. Display the required information.
These are the Files that I made:
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()); } }
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"; } public String getTeleponeNeighbor() { if(phone == "Not set") { return "Cannot calculate phone number neighbor"; } else { String roundUp = "("; roundUp += phone.substring(0,3) + ") "; roundUp += phone.substring(3,6) + "-"; roundUp += Integer.parseInt(phone.substring(6,10)) + 1; return roundUp; } } }
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; } }
//////////////// 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
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; } } }