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 i
- a double named d
- Methods:
- A Module1 constructor method that accepts no parameters and:
- Assigns the values 0 to i and 0.0 to d
- Creates the Scanner object sc by calling its constructor with the System.in argument,
- A Module1 constructor method that accepts no parameters and:
i.e., sc = new Scanner(System.in);
- a method named square that returns an int, accepts an int parameter named number, and returns (as an int) the square of number, i.e., the value number * number.
- a method named square that returns a double, accepts a double parameter named number, and returns (as a double) the square of number, i.e., the value number * number.
- a method named cube that returns an int, accepts an int parameter named number, and returns (as an int) the cube of number, i.e., the value number * number * number.
- a method named cube that returns an double, accepts an double parameter named number, and returns (as a double) the cube of number, i.e., the value number * number * number.
- 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 to the console (use System.out.println()) asking the user to enter an integer, like "please enter an integer", reads the user input by using Scanner's nextInt() method, then assigns that value to m.i
- Prints a message to the console (use System.out.println()) asking the user to enter a double, like "please enter a floating point number", reads the user input by using Scanner's nextDouble() method, then assigns that value to m.d
- Prints the following outputs to the console:
- 2a: print "int squared: " and the value returned by m.square(m.i)
- 2b: print "int cubed: " and the value returned by m.cube(m.i)
- 2c: print "double squared: " and the value returned by m.square(m.d)
- 2d: print "double cubed: " and the value returned by m.cube(m.d)
- Prints the message ending application to the console
Additional Constraints
- Other than Module1's main method, all data fields & methods should be for individual objects, not the entire class.
- Use simple multiplication, not Math.pow() for exponentiation.
- Comments should be provided at the start of the source code 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
Get step-by-step solutions from verified subject matter experts
