Write a cell method that takes a single double as an argument and returns an int
ID: 3936637 • Letter: W
Question
Explanation / Answer
import java.math.*; public class TestRound11 { public static double round(double d, int decimalPlace){ BigDecimal bd = new BigDecimal(Double.toString(d)); bd = bd.setScale(decimalPlace,BigDecimal.ROUND_HALF_UP); return bd.doubleValue(); } } private static String getInput(Scanner in) { // Ask the user to enter string here do { System.out.println("Enter a string here: "); String input = in.nextLine(); // Create an if/else statement to find out if the user // entered input if (input.trim().length() > 0) { return input.trim(); } else { System.out.println("Error -- " + "You must enter a string!"); } } while (true); } private static int getWordCount(String input) { String[] result = input.split(" "); return result.length; } public static void main(String args[]){ double d = 3.1537; BigDecimal bd = new BigDecimal(d); bd = bd.setScale(2,BigDecimal.ROUND_HALF_UP); // output is 3.15 System.out.println(d + " : " + round(d, 2)); // output is 3.154 System.out.println(d + " : " + round(d, 3)); //Print out the number of words within the users string here int counter = getWordCount(getInput(new Scanner(System.in))); System.out.println("The number of words in the string are: " + counter); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.