In: Computer Science
PhoneNumber
You will want to go to PhoneNumber.java and implement the following methods
Additional Methods
getPrettyNumber()
Combines String.format with getAreaCode(), getPrefix() and
getLine() to return a pretty formatted phone number.
The format would be as follows for 9708675309:
(970) 867-5309
render()
Uses getPrettyNumber() and getType() to build a formatted String.
Like Email, the type should have 7 padding. For example:
Home: (970) 111-5309 Office: (970) 111-1101
public class PhoneNumber {
private String type;
private String number;
/**
* The type of phone number - mobile, home, office, other
* @return the type associated with the number
*/
public String getType() {
return type;
}
/**
* sets the type of phone number
* @param type string to set it to
*/
public void setType(String type) {
this.type = type;
}
/** Gets the phone number as *rawnumbers* only **/
public String getNumber() {
return number;
}
/** Sets the phone number. I thousl be set as raw numbers
5554443333 for example, NOT (555)444-3333 **/
public void setNumber(String number) {
this.number = number;
}
/** Gets the first 3 digits, you can assume US Standard, so 10
digits **/
public String getAreaCode() {
return ""; //TODO STUDENT
}
/** Gets the second three digits. In the number (555) 444-3333,
4444 would be the prefis. Remember, phone
* numbers are stored as number strings only, so 5554443333.
* @return the prefix
*/
public String getPrefix() {
return ""; //TODO STUDENT
}
/** get the last four digits in a number (so location 6 onward)
**/
public String getLine() {
return ""; //TODO STUDENT
}
/**
* Reformats a 10 digit only number, into US Standard display
format, so 5554443333 becomes (555) 444-3333
* @return a US Standard formatted number
*/
public String getPrettyNumber() {
return ""; //TODO STUDENT
}
/**
* Returns a string of format type: prettyNumber - the type is 7
padded (%-7s)
* @return
*/
public String render() {
return ""; //TODO STUDENT
}
/**
* Basic constructor
* @param type the type of number
* @param number the phone number as a ten digit string
*/
public PhoneNumber(String type, String number) {
setType(type);
setNumber(number);
}
}
code:
public class PhoneNumber {
private String type;
private String number;
/**
* The type of phone number - mobile, home, office, other
* @return the type associated with the number
*/
public String getType() {
return type;
}
/**
* sets the type of phone number
* @param type string to set it to
*/
public void setType(String type) {
this.type = type;
}
/** Gets the phone number as *rawnumbers* only **/
public String getNumber() {
return number;
}
/** Sets the phone number. I thousl be set as raw numbers
5554443333 for example, NOT (555)444-3333 **/
public void setNumber(String number) {
this.number = number;
}
/** Gets the first 3 digits, you can assume US Standard, so 10
digits **/
public String getAreaCode() {
return this.number.substring(0,3);
}
/** Gets the second three digits. In the number (555) 444-3333,
4444 would be the prefis. Remember, phone
* numbers are stored as number strings only, so 5554443333.
* @return the prefix
*/
public String getPrefix() {
return this.number.substring(3,6); //TODO STUDENT
}
/** get the last four digits in a number (so location 6 onward)
**/
public String getLine() {
return this.number.substring(6); //TODO STUDENT
}
/**
* Reformats a 10 digit only number, into US Standard display
format, so 5554443333 becomes (555) 444-3333
* @return a US Standard formatted number
*/
public String getPrettyNumber() {
return String.format("(%s)
%s-%s",getAreaCode(),getPrefix(),getLine()); //TODO STUDENT
}
/**
* Returns a string of format type: prettyNumber - the type is 7
padded (%-7s)
* @return
*/
public String render() {
return String.format("%s: %s",getType(),getPrettyNumber()); //TODO
STUDENT
}
/**
* Basic constructor
* @param type the type of number
* @param number the phone number as a ten digit string
*/
public PhoneNumber(String type, String number) {
setType(type);
setNumber(number);
}
}
Sample code used to test the above class:
class Main
{
public static void main(String[] args) {
PhoneNumber p=new
PhoneNumber("Office","9444148408");
System.out.println(p.render());
}
}
Sample i/o:
Explanation:
The sample i/o and code is presented for your understanding, kindly upvote if you like the answer.