reate a new project called Trunc.java either by modifying TwoDPs. 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 without using Round method.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Create a new project called Trunc.java either by modifying TwoDPs. 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 without using Round method.

 

 

 

double posNum
num;
if (neg)posNum
//add 0.005 to the posNum, so that truncating nPlus
//is equivalent to rounding posNum
double nPlus =
-
num;
posNum+0.005;
//extract the whole number part and the rest
int whole =
(int)nPlus;
double rest= nPlus
whole;
//multiply the rest by 100
//truncate, csat and make sure there
//are some
zeros in front of small numbers
(int) (100.0*rest +100.0);
int temp
//make a string version of temp
String s
+temp;
int I
s.length();
String sign="";
if (neg) sign="-";
//display the message, sign,whole part and last two digits of s
System.out.println (msg +"" +sign +whole +"." +s.substring(I-
2,1));
}
}
//end of class
Transcribed Image Text:double posNum num; if (neg)posNum //add 0.005 to the posNum, so that truncating nPlus //is equivalent to rounding posNum double nPlus = - num; posNum+0.005; //extract the whole number part and the rest int whole = (int)nPlus; double rest= nPlus whole; //multiply the rest by 100 //truncate, csat and make sure there //are some zeros in front of small numbers (int) (100.0*rest +100.0); int temp //make a string version of temp String s +temp; int I s.length(); String sign=""; if (neg) sign="-"; //display the message, sign,whole part and last two digits of s System.out.println (msg +"" +sign +whole +"." +s.substring(I- 2,1)); } } //end of class
import java.util.Scanner;
//Two Dps.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.
public class TwoDPs{
public static void main (String [] args) {
new Scanner(System.in);
Scanner input
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 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 decima places
//with both decimal values showing even if they are zero
//record whetherthe number is negative
boolean neg = (num < 0);
//make a positive version of the number
Transcribed Image Text:import java.util.Scanner; //Two Dps.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. public class TwoDPs{ public static void main (String [] args) { new Scanner(System.in); Scanner input 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 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 decima places //with both decimal values showing even if they are zero //record whetherthe number is negative boolean neg = (num < 0); //make a positive version of the number
Expert Solution
steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Random Class and its operations
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education