Create a new project called Trunc.java . Trunc.java should loop around getting floating point (i.e. type double) numbers and displaying both the number and the running total to the nearest whole number. The program will finish when a number is entered outside of the range -100 to 100. The program should use methods to check whether the number is in this range, and then rounded to the nearest whole number. (NOTE: Not to use the JAVA Round method, write own method by modifying the calcTwoDPs method in TwoDPs.java). //TwoDPs.java //Displays running total of numbers in lines of standard input correct to two decimal places. //Uses an out of range number (<-100 or >100) to quit. import java.util.Scanner; public class TwoDPs { public static void main(String[] args) { Scanner input = new Scanner(System.in); double total = 0; boolean flag = true; System.out.println("Use an out of range entry (< -100 or > 100) to quit."); while (flag) { System.out.println("Enter a floating point number on a line:"); double d = input.nextDouble(); if (outOfRange(d)) { flag = false; } else { dispTwoDPs("The number value is", d); total = total + d; dispTwoDPs("The total is", total); System.out.println(); System.out.println("Next."); }//end of else }//end of while System.out.println("You quit."); }//end of main static boolean outOfRange(double d) { if (d < -100) { return true; } if (d > 100) { return true; } return false; } static void dispTwoDPs(String msg, double num) { // Display on screen the message msg followed by num // correct to two decimal places with both decimal // values showing even if they are zero //record whether the number is negative boolean neg = (num < 0); //make a positive version of the number double posNum = num; if (neg) { posNum = -num; } //add 0.005 to the posNum, so that //truncating nPlus is equivalent to //rounding posNum double nPlus = posNum + 0.005; //extract whole number part and the rest int whole = (int) nPlus; double rest = nPlus - whole; //multiply the rest by 100 //truncate, cast and make sure there //are some zeros in front of small numbers int temp = (int) (100.0 * rest + 100.0); //make a string version of temp String ss = "" + temp; int len = ss.length(); String sign = ""; if (neg) { sign = "-"; } //display the message, sign, whole part //and last two digits of ss System.out.println(msg + " " + sign + whole + "." + ss.substring(len - 2, len)); }//end of DispTwoDPs }//end of class
Create a new project called Trunc.java . Trunc.java should
loop around getting floating point (i.e. type double) numbers and displaying
both the number and the running total to the nearest whole number. The
100. The program should use methods to check whether the number is in this
range, and then rounded to the nearest whole number. (NOTE: Not to
use the JAVA Round method, write own method by modifying the
calcTwoDPs method in TwoDPs.java).
//TwoDPs.java
//Displays running total of numbers in lines of standard input correct to two decimal places.
//Uses an out of range number (<-100 or >100) to quit.
import java.util.Scanner;
public class TwoDPs {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double total = 0;
boolean flag = true;
System.out.println("Use an out of range entry (< -100 or > 100) to quit.");
while (flag) {
System.out.println("Enter a floating point number on a line:");
double d = input.nextDouble();
if (outOfRange(d)) {
flag = false;
} else {
dispTwoDPs("The number value is", d);
total = total + d;
dispTwoDPs("The total is", total);
System.out.println();
System.out.println("Next.");
}//end of else
}//end of while
System.out.println("You quit.");
}//end of main
static boolean outOfRange(double d) {
if (d < -100) {
return true;
}
if (d > 100) {
return true;
}
return false;
}
static void dispTwoDPs(String msg, double num) {
// Display on screen the message msg followed by num
// correct to two decimal places with both decimal
// values showing even if they are zero
//record whether the number is negative
boolean neg = (num < 0);
//make a positive version of the number
double posNum = num;
if (neg) {
posNum = -num;
}
//add 0.005 to the posNum, so that //truncating nPlus is equivalent to //rounding posNum
double nPlus = posNum + 0.005;
//extract whole number part and the rest
int whole = (int) nPlus;
double rest = nPlus - whole;
//multiply the rest by 100
//truncate, cast and make sure there
//are some zeros in front of small numbers
int temp = (int) (100.0 * rest + 100.0);
//make a string version of temp
String ss = "" + temp;
int len = ss.length();
String sign = "";
if (neg) {
sign = "-";
}
//display the message, sign, whole part //and last two digits of ss
System.out.println(msg + " " + sign + whole + "." + ss.substring(len - 2, len));
}//end of DispTwoDPs
}//end of class
Step by step
Solved in 3 steps with 2 images