Question
Create a class named Module2, for performing various numeric operations as given below. You should submit your source code file (Module2.java). The Module2 class will
Create a class named Module2, for performing various numeric operations as given below. You should submit your source code file (Module2.java). The Module2 class will import the class java.util.Scanner and should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class)
- Data fields:
- A String named value
- An int named decimalPlaces
- Methods:
- A Module2 constructor method that accepts no parameters and initializes the data fields value to an empty String and decimalPlaces to zero
- A method named valueRoundUp that accepts a parameter String s, converts s to a double using the method Double.parseDouble(), and returns, as an int, that double rounded up to the next higher int value. Note that negative and positive values will need to be processed differently typecasting to int will round negative values up but positive values down.
- A method named valueRoundDown that accepts a parameter String s, converts s to a double, and returns, as an int, that double rounded up to the next lower int value. Again, negative and positive values will need to be processed differently.
- A method named valueDecimalPlaces that accepts two parameters a String s and an int places. The parameter s contains the text representation of a number and the method will return another String which represents that number with places as the number of decimal places. If the number contains more decimal places than places, the last decimal place should be rounded off. If places == 0, the number should not have a decimal point and should be rounded to the nearest integer.
- A method named getValue that accepts a parameter String prompt and does the following: creates a new Scanner object, prints prompt to the console, uses Scanner's nextLine method to read the user input, and returns the String obtained from Scanner's nextLine method
- A method named getInput with return type void and accepts no parameters and, in order:
- Calls method e) with the argument "Please enter the value" and stores the result in value. If the user enters the String "exit", the program should end by calling the method System.exit(0).
- Calls method e) with the argument "Please enter the number of decimal places", converts the returned String to an int (use Integer.parseInt()), and stores the result in decimalPlaces
- Prints to the console, with appropriate text labels, the number represented by value to 1) decimalPlaces decimal places; 2) rounded up; and 3) rounded down
- Prints an empty line of text
- A main method that, in order:
- Creates a new Module2 object named m using the constructor from a)
- Starts an endless loop using while(true) and calls getInput() inside it, i.e.: while(true) {m.getInput();}
Additional Constraints
The program should be implemented as a single file holding the public class Module2. Comments should be provided at the start of the class 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started