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