For each of the following problems you should start by writing several JUnit tes
ID: 3672989 • Letter: F
Question
For each of the following problems you should start by writing several JUnit tests that will verify correct behavior. Once you have written at least four distinct tests for each of the problems, begin to implement solutions to each of the problems. Be sure to run your unit tests often. They are not necessarily in order of difficulty
Define a method named accept that accepts a String as input and returns true if the String represents a valid telephone number format, false otherwise. Valid formats are: a. (DDD) DDD-DDDD b. DDD-DDDD c. +1-DDD-DDD-DDDD Valid examples are “(716)688-0315”, “645-3180” and “+1-716-645-3180”. If the String does NOT represent a telephone number in a valid format, return false. USE A FINITE STATE MACHINE APPROACH. START BY DRAWING THE MACHINE.
Explanation / Answer
Below are the code :
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class HelloWorld {
public static void main(String ar[])
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a social security number: ");
String s = input.nextLine();
//Examples: Matches following phone numbers-(123)456-7890//
Pattern pattern1 = Pattern.compile("^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$");
Matcher matcher1 = pattern1.matcher(s);
//example for 456-7890//
Pattern pattern2 = Pattern.compile("\d{3}-\d{4}");
Matcher matcher2 = pattern2.matcher(s);
//example for +1-678-456-7890//
Pattern pattern3 = Pattern.compile("^[-+]?(\d{1})[- ]?(\d{3})[- ]?(\d{3})[- ]?(\d{4})$");
Matcher matcher3 = pattern3.matcher(s);
if (matcher1.matches()) {
System.out.println("Entered Phone Number Valid:");
}
else if (matcher2.matches()) {
System.out.println("Entered Phone Number Valid:");
}
else if (matcher3.matches()) {
System.out.println("Entered Phone Number Valid:");
}
else
{
System.out.println("Phone Number must be in the format (DDD)DDD-DDDD / DDD-DDDD /+1-DDD-DDD-DDDD");
}
}
}
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class HelloWorld {
public static void main(String ar[])
{
try {
Scanner input = new Scanner(System.in);
System.out.print("Enter a social security number: ");
String s = input.nextLine();
//Examples: Matches following phone numbers:
//(123)456-7890, 123-456-7890, 1234567890, (123)-456-7890 //
Pattern pattern1 = Pattern.compile("^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$");
Matcher matcher1 = pattern1.matcher(s);
Pattern pattern2 = Pattern.compile("\d{3}-\d{4}");
Matcher matcher2 = pattern2.matcher(s);
Pattern pattern3 = Pattern.compile("^[-+]?(\d{1})[- ]?(\d{3})[- ]?(\d{3})[- ]?(\d{4})$");
Matcher matcher3 = pattern3.matcher(s);
if (matcher1.matches()) {
System.out.println("Phone Number Valid");
}
else if (matcher2.matches()) {
System.out.println("Phone Number Valid");
}
else if (matcher3.matches()) {
System.out.println("Phone Number Valid");
}
else
{
System.out.println("Phone Number must be in the form (DDD) DDD-DDDD / DDD-DDDD /+1-DDD-DDD-DDDD");
}
}
catch (Exception ex) {
System.out.println("Error: ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.