Question: CAN YOU PLEASE DO THIS IN JAVA! WILL LEAVE GOOD RATING THANK YOU Modify your program that reads grades from the user, so that it
CAN YOU PLEASE DO THIS IN JAVA! WILL LEAVE GOOD RATING THANK YOU
Modify your program that reads grades from the user, so that it has a method that checks if a particular input is valid i.e. as long as the user types invalid input, the user should be given another chance to enter input (it would also be good to let the user know that their input is invalid). Moreover, only a valid grade should be used in computing the sum and average. At the appropriate point in your code, put a comment that states that the grade entered by the user has been determined to be valid.
Write/modify your program that draws 2 pictures (e.g. houses) so that it uses a loop (with 2 iterations). What would you need to change if you wanted the program to draw more than 2 pictures? What kind of loop is appropriate for this?
Tracing programs containing loops (by drawing tables and showing output; you need not write any code):
- Trace the following program assuming the user enters 3 for value and 21 for limit. Draw a table showing the values of relevant variables (value, limit, mult, count) during each iteration of the loop: Multiples.java. As you trace, keep track of the output too, and then check that your predicted output is correct by running the program.
- Trace Stars.java by drawing a table that shows the values of relevant variables (row and star) and keeping track of the output. This program contains a nested for loop, so for each iteration of the outer loop, the inner loop executes in its entirety:
row star 1 1 2 1 2 2 3 1 3 2 3 3 ... ...
Program below
import java.util.Scanner; public class GradeSumAverage { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int grade = 0; int totalScore = 0; double scoreCount = 0; do { System.out.print("Enter your score (Enter -1 to exit): "); grade = scanner.nextInt(); if (isValidScore(grade)) { System.out.println("Info: Grade entered is found to be valid."); totalScore += grade; scoreCount += 1; } } while (grade != -1); // loop until user enters -1
// dispplay the sum and average (to 2 decimals) System.out.println("Total Score: " + totalScore); System.out.printf("Average Score: %.2f ", totalScore / scoreCount); }
// method takes in a grade returns true if the grade is between 0 and 100 else returns false public static boolean isValidScore(int score) { if (0 <= score && score <= 100) return true; else return false; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
