Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Create a directory in which to do your work. Download the program SmallIO.java from the unit LMS website and save the file into your

1. Create a directory in which to do your work. Download the program SmallIO.java from the unit LMS website and save the file into your working directory. Note that this application uses the Scanner class from the Java.util package, which you would need to import into your program (refer lecture notes examples).

2. Using the NetBeans IDE development environment, create and build a project using the source file that you just downloaded (SmallIO.java). Execute the program.

3. Using the IDE make a new project to modify the previous program so that it now repeats the loop five times and then stops.

4. Using the IDE make a new project to modify the previous program so that integers instead of Strings are entered by the user.

5. Using the IDE make a new project to modify the previous program so that it only loops until the number -999 is entered by the user.

6. Using the IDE make a new project to modify the previous program so that it: a) Keeps and displays a running total and the average so far of the entered numbers. b) Displays the largest and the smallest numbers so far of the entered numbers.

7. Download the program TwoDPs.java from the unit LMS website and save the file in your working directory. Use the NetBeans IDE development environment, create and build a project using the source file that you just downloaded. Execute the program.

8. Create a new project called Trunc.java either by copying and modifying TwoDPs.java or by typing that source code from scratch. 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: You are not to use the JAVA Round method, write your own method by modifying the calcTwoDPs method in TwoDPs.java).

SmallIO.java:

import java.util.Scanner; public class SmallIO{ public static void main(String[] args){ Scanner keyboard = new Scanner (System.in); String a = ""; // initialise to empty string while (true){ //an infinite loop, use Ctrl-C (from command prompt) to quit System.out.println("Enter a line:"); a = keyboard.nextLine(); System.out.println("Your line: " + a); System.out.println(); }//end of while }//end of main }//end of class 

TwoDPs.java:

import java.util.Scanner; //To change body of generated methods, choose Tools | Templates. //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. 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 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; //To change body of generated methods, choose Tools | Templates. } 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 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 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 temp = (int) (100.0*rest +100.0); //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(1-2,1)); } } //end of class 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions

Question

What is the most important part of any HCM Project Map and why?

Answered: 1 week ago