Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

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

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education