Create the public class Module2 with the following data fields and methods:
- Data fields:
- an object of type Scanner named sc (you can import this from Java.util.Scanner)
- four Strings: named question, answer, input1, and input2
- Methods:
- an object method named formatCheck that accepts as parameters a String text and an int code:
- if code is one:
- returns true if the parameter String begins with a capital letter and ends with “?”
- returns false otherwise
- if code is two:
- returns true if the parameter String begins with a capital letter and ends with a period
- returns false otherwise
- if code is any other value, returns false
- if code is one:
- a constructor method that:
- creates Module2’s Scanner object (sc) using the System.in argument to read console input
- creates question, answer, input1, and input2 as empty (zero length) Strings
- A main method (i.e., with the header public static void main(String[] args)) that, in order:
- Creates a new Module2 object named m by calling Module2’s constructor method
- Prints the message “beginning application” to the console
- Prints a message asking the user to enter question text, reads the input using [m‘s] Scanner’s nextLine() method, and sends it and the code value 1 to formatCheck.
- If formatCheck returns true assigns it to input1
- If formatCheck returns false, print “bad question format!” to the console and exits the main method with a return statement
- Prints another message asking the user to enter question text, reads the input using [m‘s] Scanner’s nextLine() method, and sends it and the code value 2 to formatCheck
- If formatCheck returns true assigns it to input2
- If formatCheck returns false, print “bad answer format!” to the console and exits the main method with a return statement
- Prints a message indicating that the question & answer pair was accepted and displays with appropriate labels the question & answer text, e.g.:
- Q&A validated!
- Question: [question text]
- Answer: [answer text]
- Prints the message “ending application” to the console
- an object method named formatCheck that accepts as parameters a String text and an int code:
ADDITIONAL CONSTRAINTS
- Other than Module2’s main() method, all data fields & methods should be for individual objects, not the entire class.
- Use System.out.println() for print operations
- Comments should be provided at the start of the file (i.e., above the class definition) giving the class author’s name and the date the program was finished.
Post a Comment