Question: I'm new at coding and I'm confused in how to finish this code. Utility.Java must contain the following methods: A method named inputDouble that will
I'm new at coding and I'm confused in how to finish this code.
Utility.Java must contain the following methods:
A method named inputDouble that will accept a single String parameter representing the prompt to display to the user and return a double number read from the keyboard. This method should:
display the prompt, followed by a colon and single space (no newline),
read a double value from the keyboard using the appropriate Scanner method (assume that a valid double value will be entered),
consume the newline character following the double (also using Scanner object), and
return the double value.
A method named inputLine that will accept a single String parameter representing the prompt to display to the user and return a String containing the whole line that the user types in.
This method should:
display the prompt, followed by a colon and a single space (no newline),
read a line of input, and
return the line of input.
A method named formatTwoDecimals that will accept a double value of up to 15 digits and return a String containing that value rounded to exactly two decimal places.
For example, formatTwoDecimals(34.2589) would return 34.26.
A method named formatNoDecimals that will accept a double value of up to 15 digits and return a String containing that value rounded to an integer, with no decimal point. For example, formatNoDecimals(34.6589) would return 35 .
My code so far:
import java.until.Scanner; import java.text.DecimalFormat; public class Utility { //use this Scanner object to take input in inputDouble and inputLine methods private static Scanner sc = new Scanner(System.in); // Start writing the 4 method codes after this point public static double inputDouble(String prompt){ double input; scanner keyboard; keyboard = new Scanner(System.in); system.out.print(prompt + " : "); input = keyboard.nextDouble(); keyboard.nextLine(); return input; } public static double inputLine(String prompt){ double input; scanner keyboard; keyboard = new Scanner(System.in); system.out.print(prompt + " : "); input = keyboard.nextLine(); keyboard.nextLine(); } public static double formatTwoDecimals()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
