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(); //...
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”))
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...
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...
Implement the following methods in Java: a. A method named MergeFileswith the following signature that gets...
Implement the following methods in Java: a. A method named MergeFileswith the following signature that gets two file names and write the content of the first file (sourceFile) into the beginning of the second file (destinationFile. For example, if sourceFile contains “Hello ” and destinationFile contains “World!”, this method must keep sourceFile as it is, but replace the content of the second file by “Hello World!”.public static voidMergeFiles(String sourceFile, String destinationFile) b. A recursive method with the following signature that...
Using STL stack class, implement in C++ the following pseudocodefunctioncheckBraces(aString: string) that checks for...
Using STL stack class, implement in C++ the following pseudocode functioncheckBraces(aString: string) that checks for balanced braces { } in a given string /arithmetic expressions. Write a main() program to test the function.// Checks the string aString to verify that braces match.// Returns true if aString contains matching braces, false otherwise.checkBraces(aString: string): boolean{aStack = a new empty stackbalancedSoFar = truei =0// Tracks character position in stringwhile (balancedSoFar and i < length of aString) {ch = character at position i in...
Where do you want to go? Mountains? Ocean?... What do you want to see? Landscapes? Wildlife?...
Where do you want to go? Mountains? Ocean?... What do you want to see? Landscapes? Wildlife? What do you want to do? Hiking? Biking Kayaking?.. Where do you want to sleep? In a hotel in a tent?... How long do you want to stay? A day? A week?
C++, Write a function, noPunct, that would take a string, go through the string character by...
C++, Write a function, noPunct, that would take a string, go through the string character by character, and push any character that is NOT a punctuation mark onto a stack but count the punctuation dropped. After going through the entire string, print the contents of the stack. Then print the number of punctuation dropped. Hint: use ispunct() library function (returns true if character is punctuation)
Please implement Sample string toString()method for each class and return itself a string, not the output....
Please implement Sample string toString()method for each class and return itself a string, not the output. import java.util.ArrayList; public class Customer extends User{ private ArrayList orders; public Customer(String display_name, String password, String email) { super(display_name, password, email); } @Override public String getPermissionLevel() { return "CUSTOMER"; } public void addOrder(Order order){ this.orders.add(order); } public ArrayList listOrders(){ return this.orders; }; } ---------------- public class ElectronicProduct extends Product{ private long SNo; private String warranty_period; public ElectronicProduct(long SNo, String warranty_period, String productId, String productName,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT