Java What do you need to know in order to do this lab? Input, Output, Validation
ID: 3707815 • Letter: J
Question
Java
What do you need to know in order to do this lab?
Input, Output, Validation, Compound Conditions, Functions, Loops and Decisions
What you will do:
For your third lab, you will use the files in JavaLab3 and you will upload it in your Dr Java environment.
You will then compile it and run it.
The goal of the program is to find the highest run of positive and even numbers higher than 100.
To do this, you will ask the user for numbers, and add them. As soon as the sum of the numbers reaches 100 or the total number of number entered is 5 or greater, you will stop and tell the user what their total for this run is.
You will then ask the user if they want to play again. You will keep track of the highest run, and display it when the user is done playing.
Note: you will need to use exactly 3 loops to get the maximum credit, and use compound conditions where applicable.
You will also need to have a minimum of two functions, one of these returning a value.
Example:
Do you want to play again ?
ask java code and pseudocode
make up the java code :
import java.io.*;
public class JavaLab3 {
public static void main(String[] args) {
// first we define our input streams.
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
// constant declarations
final Integer MAX = 100;
final Integer MAX_NUMBER = 4;
// variable declarations
String sName;
Integer currentRun;
Integer currentNumber;
Integer maxRunToDate = 0;
// we catch exceptions if some are thrown.
// an exception would be entering a string when a number is expected
try {
System.out.println("what is your name?");
// we read a string from the stream
sName = reader.readLine();
currentRun = 0;
System.out.println("Please enter a number");
currentNumber = Integer.parseInt(reader.readLine());
currentRun = currentRun + currentNumber;
System.out.println("The total for this run is "+ currentRun);
} catch (IOException e){
System.out.println("Error reading from user");
}
}
}
Explanation / Answer
import java.io.*;
public class JavaLab3 {
public static void main(String[] args) {
// first we define our input streams.
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
// constant declarations
final Integer MAX = 100;
final Integer MAX_NUMBER = 5;
// variable declarations
int currentRun;
int currentNumber;
int count = 0;
// we catch exceptions if some are thrown.
// an exception would be entering a string when a number is expected
try {
String choice = "yes";
currentRun = 0;
do {
if (count == MAX_NUMBER ||currentRun>MAX) {
System.out.println("Do You Want to Play Again !!");
choice = reader.readLine();
if (choice.equalsIgnoreCase("yes")) {
// Let the loop running
// Reset the counter
count = 0;
currentRun=0;
} else {
System.exit(0);
}
}
System.out.println("Please enter a number");
currentNumber = Integer.parseInt(reader.readLine());
if (currentNumber < 0) {
do {
System.out.println("Negative Numbers are not allowed . Please Enter a Positive number ");
currentNumber = Integer.parseInt(reader.readLine());
count++;
} while (currentNumber < 0);
} else if (currentNumber % 2 != 0) {
do {
System.out.println("Odd Numbers are not allowed . Please Enter a Positive number ");
currentNumber = Integer.parseInt(reader.readLine());
count++;
} while (currentNumber % 2 != 0);
}
currentRun = currentRun + currentNumber;
count++;
System.out.println("The total for this run is " + currentRun);
} while (currentRun < MAX || count >= MAX_NUMBER);
} catch (IOException e) {
System.out.println("Error reading from user");
}
}
}
Output:
Please enter a number
50
The total for this run is 50
Please enter a number
20
The total for this run is 70
Please enter a number
10
The total for this run is 80
Please enter a number
2
The total for this run is 82
Please enter a number
4
The total for this run is 86
Do You Want to Play Again !!
No
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.