Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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)

  1. Data fields:
    1. A String named value
    2. An int named decimalPlaces
  2. Methods:
    1. A Module2 constructor method that accepts no parameters and initializes the data fields value to an empty String and decimalPlaces to zero
    2. 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.
    3. 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.
    4. 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.
    5. 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
    6. 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
    7. 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Carlos Coronel, Steven Morris

14th Edition

978-0357673034

More Books

Students also viewed these Databases questions

Question

Give three examples of typical types of exceptions handled by CPUs.

Answered: 1 week ago