Question: Create the public class Module1 with the following data fields and methods: Data fields: an object of type Scanner named sc (you can import this
Create the public class Module1 with the following data fields and methods:
- Data fields:
- an object of type Scanner named sc (you can import this from Java.util.Scanner)
- an int named index
- two Strings, one named question and one named answer
- two static Strings named input1 and input2
- Methods:
- an object method named add that accepts two Strings as parameters and:
- assigns the first one to question and the second one to answer
- increments index (i.e., adds one to it)
- returns the value of index
- a constructor method that:
- creates Module1's Scanner object (sc) using the System.in argument to read console input
- creates question and answer as empty (zero length) Strings
- assigns index an initial value of -1
- A main method (i.e., with the header public static void main(String[] args)) that, in order:
- Creates a new Module1 object named m by calling Module1'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 assigns it to input1
- Prints another message asking the user to enter question text, reads the input using [m's] Scanner's nextLine() method, and assigns it to input2
- Calls m's add() method with input1 and input2 as the arguments and assigns the returned value to a local variable in main()
- Prints a message displaying the returned value
- Prints the message ending application to the console
- an object method named add that accepts two Strings as parameters and:
-
-
Additional Constraints
- Other than Module1's main() method and input1 and input2, 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 authors name and the date the program was finished.
-
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
