Question

Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete...

Homework 3: Input Validation

1   Objectives

  • control structures
  • console-based user input using Scanner class
  • writing complete programs using two classes: client and supplier

2   User Interface Specification

This is a console-based I/O program. Display should go to System.out (print or println) and the program will get user input using the Scanner class. The flow of execution should be as follows:

  • When the program starts, display a one-line introduction to the user
  • Display a menu with 5 options

1. validate zip code

2. validate SSN

3. validate password

4. instructions

0. quit

  • Prompt the user to input an option
  • Prompt the user for the required input and then display the results
  • Repeat the main menu until the user enters the option to quit
  • If the user enters an invalid menu option, print a helpful error message and re-prompt. Note, this could be either a wrong value or wrong data type.

3   Code Specifications

For this assignment, you will write two different classes. The goal is to separate the user interface code from the code that performs validation. The sample programs MyMath.java and TestMyMath.java are an essential reference.

In a supplier class named Validations, implement 3 static methods to perform these different validation tasks:

  • validate that a String represents a valid zip code. A valid zip code is made up of 5 digits.
  • validate that a String represents a 9-digit SSN. A valid SSN follows the pattern XXX-XX-XXXX, including the dashes.
  • validate that a String represents 8 symbols, including letters (upper and lower case), numbers, and special symbols like #$&_.

Though I specify 3 public methods, you may have more if you choose to do some decomposition. Note that this Validations class does not need a 'main' method. However, you may write one for testing purposes if you wish. You must use parameter lists and return statements to move data in and out of these methods. No method in this class should use System.out.println or the Scanner class (except an optional test method). In other words, no method here should interact with the user; that's the job of the other class.

In a class named ValidateApp, create a console-based, menu-driven user interface that behaves like the one described above. This class must use a 'main' method to start things off. However, you are encouraged to decompose your solution to organize your code.

No class variables may be used; class constants are ok.

Your program should not throw any exceptions to the user. If a user enters an invalid menu option, the program should display a helpful error message and ask for new input.

You must implement your solution using loops, if statements, and switch-case.

4   Testing & Documentation

  • As always, I recommend doing this in pieces. For example, you might choose to do the validation code first (testing as you go). Then focus on the user interface.
  • Your documentation in both classes is an integral part to your solution since you are the designer/author of these classes.
    • Each class should have an overall description comment at the top
    • Be sure to include a java-doc for each method that describes what it does, describes the parameters and the return.
    • Make sure to include internal comments to guide the reader through your algorithms.

5   File Submission

There are two different .java files for this assignment: Validations.java and ValidateApp.java. You will need to compress them into one .zip file, or use BlueJ to make a .jar file.

6   Grading

Area

Pct

Design, correctness, implementation

90%

Documentation and style

10%

Total

100%

Achievement

Max Points

Compile/runtime errors

50%

Basic objects

100%

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

import java.util.Scanner;

class Validations{
   //for verifying zip code
   public boolean validateZipCode(String str) {
       if(str.length()==5 && isNumeric(str))
           return true;
       else
           return false;
   }
   //for verifying SSN
   public boolean validateSSN(String str) {
       str = str.replaceAll("-","");
      
       if(str.length()==9 && isNumeric(str))
           return true;
      
       return false;
   }
   //for verifying password
   public boolean validatePassword(String str) {
       if(str.length()==8)
           return true;
       else
           return false;
   }
   //for checking the given string is number or not
   private static boolean isNumeric(String str) {
       try {
           Integer.parseInt(str);
           return true;
       }
       catch(NumberFormatException e) {
           return false;
       }
   }
}

public class ValidateApp {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       Validations obj = new Validations();
       String str, choice;
      
       System.out.println("**************Greeting User welcome to validation app**************");
       System.out.println();
       while(true) {//infinte loop
          
           System.out.println("1. validate zip code");
           System.out.println("2. validate SSN");
           System.out.println("3. validate password");
           System.out.println("4. instructions");
           System.out.println("0. quit");
           System.out.println();
          
           System.out.print("Enter your choice: "); choice = sc.nextLine();
           System.out.println();
          
           //switch case for handling user choice
           switch(choice) {
          
           case "0":
               System.out.println("Thank you for using this app");
               System.exit(0);
              
           case "1":
               System.out.print("Enter pin code: "); str = sc.nextLine();
               System.out.println();
               if(obj.validateZipCode(str))
                   System.out.println("The zip code "+str+" is valid");
               else
                   System.out.println("The zip code "+str+" is invalid");
              
               System.out.println();
               break;
              
           case "2":
               System.out.print("Enter SSN: "); str = sc.nextLine();
               System.out.println();
               if(obj.validateSSN(str))
                   System.out.println("The SSN "+str+" is valid");
               else
                   System.out.println("The SSN "+str+" is invalid");
              
               System.out.println();
               break;
              
           case "3":
               System.out.print("Enter password: "); str = sc.nextLine();
               System.out.println();
               if(obj.validatePassword(str))
                   System.out.println("The password "+str+" is valid");
               else
                   System.out.println("The password "+str+" is invalid");
              
               System.out.println();
               break;
              
           case "4":
               System.out.println("pin code is of 5 digits\n");
               System.out.println("SSN is of 9 digits\n");
               System.out.println("password is 8 symobols which include letters (upper and lower case), "
                       + "numbers, and special symbols like #$&_");          
               System.out.println();
               break;
              
           default://handling invalid user choice
               System.out.println("Invalid Input! Try Again");
               System.out.println();
               break;
           }
       }
   }
}

Output

<terminated> ValidateApp [Java Application] C\Program FilesJava\jre1.8.0_181binjavaw.exe (25-Apr-2019, 1:25:14 PM 1. validate

Add a comment
Know the answer?
Add Answer to:
Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am required to use the try - catch block to validate the input as the...

    I am required to use the try - catch block to validate the input as the test data uses a word "Thirty" and not numbers. The program needs to validate that input, throw an exception and then display the error message. If I don't use the try - catch method I end up with program crashing. My issue is that I can't get the try - catch portion to work. Can you please help? I have attached what I have...

  • Implement a Java program using simple console input & output and logical control structures such that...

    Implement a Java program using simple console input & output and logical control structures such that the program prompts the user to enter 5 integer scores between 0 to 100 as inputs and does the following tasks For each input score, the program determines whether the corresponding score maps to a pass or a fail. If the input score is greater than or equal to 60, then it should print “Pass”, otherwise, it should print “Fail”. After the 5 integer...

  • Write an interface and two classes which will implements the interface. 1. Interface - StringVerification -...

    Write an interface and two classes which will implements the interface. 1. Interface - StringVerification - will have the abstract method defined - boolean verifyInput( String input ); 2. class EmailVerification implements StringVerification - implement the verifyInput() method in EmailVerification class. The method should validate a user input String, character by character and see the input string has a dot (.) and an at the rate (@) character in it. If any of the condition wrong the verifyInput method will...

  • Hello, can you please show me how to do this program with COMPLETE INPUT VALIDATION so...

    Hello, can you please show me how to do this program with COMPLETE INPUT VALIDATION so the computer tells the user to enter ints only if the user enters in floating point numbers or other characters? Write a method called evenNumbers that accepts a Scanner reading input from a file containing a series of integers, and report various statistics about the integers to the console. Report the total number of numbers, the sum of the numbers, the count of even...

  • You are to write a program (BookExceptionsDemo.java) that will create and, using user input, populate an...

    You are to write a program (BookExceptionsDemo.java) that will create and, using user input, populate an array of instances of the class Book. The class Book is loaded in our Canvas files: Book.java The user will enter a number n (n must be > 0, trap the user until they input a valid value for n), Your program will declare and create an array of size n of instances of the class Book. The user will be asked to enter...

  • Using loops with the String and Character classes. You can also use the StringBuilder class to...

    Using loops with the String and Character classes. You can also use the StringBuilder class to concatenate all the error messages. Floating point literals can be expressed as digits with one decimal point or using scientific notation. 1 The only valid characters are digits (0 through 9) At most one decimal point. At most one occurrence of the letter 'E' At most two positive or negative signs. examples of valid expressions: 3.14159 -2.54 2.453E3 I 66.3E-5 Write a class definition...

  • This is a quick little assignment I have to do, but I missed alot of class...

    This is a quick little assignment I have to do, but I missed alot of class due to the Hurricane and no one here knows what theyre doing. please help! this is Java with intelli-J Overview In this project students will build a four-function one-run calculator on the command line. The program will first prompt the user for two numbers, then display a menu with five operations. It will allow the user to select an option by reading input using...

  • Hello, Could you please input validate this code so that the code prints an error message...

    Hello, Could you please input validate this code so that the code prints an error message if the user enters in floating point numbers or characters or ANYTHING but VALID ints? And then could you please post a picture of the output testing it to make sure it works? * Write a method called evenNumbers that accepts a Scanner * reading input from a file with a series of integers, and * report various statistics about the integers to the...

  • I tried to complete a Java application that must include at a minimum: Three classes minimum...

    I tried to complete a Java application that must include at a minimum: Three classes minimum At least one class must use inheritance At least one class must be abstract JavaFX front end – as you will see, JavaFX will allow you to create a GUI user interface. The User Interface must respond to events. If your application requires a data backend, you can choose to use a database or to use text files. Error handling - The application should...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT