Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Java program EOF to quit

bruier6
bruier6 


This project deals with EOF-based loops, using the while construct where we do not know the exact number of times a loop will iterate (perform the associated block of code). We will continue to build on our knowledge of if/else constructs. Use the variables given below to solve the problem. You should need no other variables to complete this project. // number entered and the running total int inputNum, sum = 0; // count of valid and invalid numbers seen int numValid = 0, numInvalid = 0; double average; Using your knowledge of Java and using inputNum as your input variable, write a while loop to read positive integers from the user until the user enters the EOF character. This is the sentinel value indicating that the user has finished entering data. You can use hasNext() to determine if the EOF has occurred. The user must use CTRL-D (DrJava) to terminate the program since these are the appropriate EOF characters. Numbers will be separated into two categories. Valid numbers are considered to be the range of positive integers including zero. Invalid numbers are the range of negative integers. As you are reading in the values you should be keeping track of how many positive values (numValid) have been read while simultaneously keeping a running total (sum). You should not include negative values in the running total. Instead you should increment the count of invalid numbers (numInvalid) entered by the user. You will report to the user the number of valid values they entered, the number of invalid values, the sum and average of the valid values. The average should be displayed as a floating point value to two decimal places using the printf() method. A sample run should look something like the following: Enter a positive value (EOF to quit): 4 Enter a positive value (EOF to quit): 7 Enter a positive value (EOF to quit): 8 Enter a positive value (EOF to quit): 2 Enter a positive value (EOF to quit): -1 The number "-1" is invalid. Enter a positive value (EOF to quit): 8 Enter a positive value (EOF to quit): 0 Enter a positive value (EOF to quit): -4 The number "-4" is invalid. Enter a positive value (EOF to quit): CTRL-D There were 6 valid numbers entered. The sum of the valid numbers was 29 and the average was 4.83. There were 2 invalid numbers. Attachments
Tags Clarifications

Answers

Java - program EOF to quit - Solution

dickersonka
dickersonka 



Attached is the solution for the Java - program EOF to quit assignment including source and comments. If you are windows, it is Ctrl+Z, if you are on linux it is Ctrl+D. Attachments
EOF.java (1 K) Download Preview
sum += inputNum; } else { //increment num invalid numInvalid++; //display invalid message System.out.println("The number \"" + inputNum + "\" is invalid"); } } else { //eof was inputted eof = true; } }while(!eof); //calculate average

Screenshots
Purchase Answer