Question

In: Computer Science

PhoneNumber You will want to go to PhoneNumber.java and implement the following methods getType setType(String) Once...

PhoneNumber

You will want to go to PhoneNumber.java and implement the following methods

  • getType
  • setType(String)
    Once again no need to check type, any String is allowed.
  • getNumber()
    returns the raw number
  • setNumber(String number)
    sets the number (10 character string, digits only). You do not need to confirm the string format that is done in another class.
  • getAreaCode()
    Uses substring to get the first 3 digits of the number.
  • getPrefix()
    Gets the next three digits (so 4-6) as a string, uses substring
  • getLine()
    Gets the last four digits. Remember with substring, if you only put in one parameter, it gives you the remainder of the string from that point.

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

}

Solutions

Expert Solution

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.


Related Solutions

Hi, I want to implement the following methods with a driver class In the comment block...
Hi, I want to implement the following methods with a driver class In the comment block for add, give the best possible big-O of the worst-case running time for executing a single add operations and give the best possible big-O of the total worst-case running time of executing a sequence of N add operations. here is the Implement class: import java.util.Iterator; // Do not modify the given code. @SuppressWarnings("unchecked") // Given public class MyArrayList { private T[] data; // Given...
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score...
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score (int). A constructor expects a name as a parameter. A method getLevel to get the level(char) of the student. score level table: A: score >= 90 B: score >= 80 and < 90 C: score >= 60 and < 80 D: score < 60 Example:          Student student = new Student("Zack"); student.score = 10; student.getLevel(); // should be 'D'. student.score = 60; student.getLevel(); //...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
In the following Java class, what would the following createFromString(String string) and saveToString() methods return? /**...
In the following Java class, what would the following createFromString(String string) and saveToString() methods return? /** * This class represents a DVD player to be rented. * * @author Franklin University * @version 2.0 */ public class DVDPlayer extends AbstractItem { /** * Constructor for objects of class DVDPlayer. */ public DVDPlayer() { // No code needed } /** * Creates a DVDPlayer from a string in the format * id:desc:weeklyRate:rented * @param string The string * @return the new...
Assume that the following variables have been declared: String a = “Ready, Set, Go!”; String b...
Assume that the following variables have been declared: String a = “Ready, Set, Go!”; String b = a.substring(5, 10); char b1 = b.charAt(2); Evaluate the following expression: 1. Character.isLowerCase(b1) 2. b1 + 5 3. b + 5 4. Character.toLowerCase(b1) 5. a.charAt(2 + a.indexOf(“e”))
For this activity, I want you to GO OUTSIDE. You don't have to go far. Find...
For this activity, I want you to GO OUTSIDE. You don't have to go far. Find a city park. Find a patch of grass. Find your backyard. Find your local playground. But find somewhere where maybe there is some vegetation and some nonhuman animals. Step 1. Look around you. Make careful observations. What do you see? What kinds of phenomena define the landscape that you see before you? What kinds of organisms travel along with it? How does water move...
This is a tricky problem. If you want a challenge give this a go. If on...
This is a tricky problem. If you want a challenge give this a go. If on the other hand the thought of being a little confused scares you then feel free to sit this problem out (it doesn't count towards participation points). It's a lot harder than anything that will be on your exams and you will not be tested on it. I just want to show you how probability comes up outside of the classroom and I'll explain what...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will perform some analysis of data considered to be DNA. DNA shall be represented arrays of chars containing only the characters A, C, G and T. In addition to the six methods you will implement, six other methods exist in the class, which use Strings instead of char arrays to represent DNA. These other methods simply invoke the methods you are to implement, so all...
Implement the following methods. Assume that these will be methods within your myArray class. operator[] Should...
Implement the following methods. Assume that these will be methods within your myArray class. operator[] Should take in an int and return arr at that index if it is a valid index notEqual The notEqual should take in another myArray object and return true if the objects are not equal to one another and false if they are equal. operator-(float) Will subtract the float value that is passed in from each of the values in arr Copy constructor Will take...
In this project you will implement the DataFrame class along with the all the methods that...
In this project you will implement the DataFrame class along with the all the methods that are represented in the class definition. DataFrame is a table with rows and columns – columns and rows have names associated with them also. For this project we will assume that all the values that are stored in the columns and rows are integers. After you have tested your class, you have to execute the main program that is also given below. DataFrame Class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT