
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 stepSolved in 3 steps with 2 images

- Instructor note: This lab is part of the assignment for this chapter. This lab uses two Java files, LabProgram.java and SimpleCar.java. The SimpleCar class has been developed and provided to you already. You don't need to change anything in that class. Your job is to use the SimpleCar class to complete the specified tasks in the main() method of LabProgram.java Given two integers that represent the miles to drive forward and the miles to drive in reverse as user inputs, create a SimpleCar object that performs the following operations: Drives input number of miles forward Drives input number of miles in reverse Honks the horn Reports car status The SimpleCar class is found in the file SimpleCar.java. Ex: If the input is: 100 4 the output is: beep beep Car has driven: 96 milesarrow_forwardPlease answer 6 and 7 as they are associated altogether. 6. The FixProgram.java below has some errors.a. Fix the errors so that the program successfully compiles and runs. class FixProgram{ public static void main(String[] args) { System.out.println('Hello World!') }} 7. Change the SampleProgram.java program below.a. So that it displays Programming is fun! Instead of Hello World! class SampleProgram{ public static void main(String[] args) { System.out.println("Hello World!"); }}arrow_forwardFor the code in java below it shows a deck of 52 cards and asks the name of the two players and makes both players draw five cards from the deck. What I want to be added onto the code is the possibility for Player A too chose whatever card they want from his/her 5 cards and Player B has to chose two cards from his/her 5 cards that equal in value of Player A's card. I also want the cards that have been used to be replaced by different cards in the deck of 52 cards and that game to go on until the deck runs out of cards. Main class code: import java.util.ArrayList; import java.util.Scanner; import java.util.List; import java.util.Random; class Main { publicstaticvoid main(String[] args) { // card game, two players, take turns. String[] suits = {"Hearts", "Clubs", "Spades", "Diamonds"}; String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; for(String oneSuit : suits){ for(String num : numbers){ System.out.println(oneSuit + " " + num); } }…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





