Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with this, completed assignment but something is not working correctly. Java program using itellik - jdk Write a Java program that determines which

Need help with this, completed assignment but something is not working correctly. Java program using itellik - jdk

Write a Java program that determines which 2 students have the highest grades and the average of all the grades. You will ask the user to enter the number of students. Then you will ask for each student and his or her grade. You will output the names and grades of the students with the 2 highest grades. You will NOT use an array for this assignment. Note: If two or more scores are tied for highest or next highest, you do NOT need to handle that as a special case in this assignment. You will output the first two with that grade. (Without the use of arrays, printing all students tied for the highest score would be very difficult.)

Objectives:

To learn to use for loops

To learn to use compound comparisons (if-then within if-then statements)

To learn to use additional local variables to save contents of other variables (in this case, two integers and two Strings)

To learn the very common coding pattern of finding the highest and next highest number in a set of numbers entered by the user without using an array

To practice testing various scenarios to make sure all solutions work as intended

To learn the coding pattern of keeping a running total to determine an average To learn to convert an integer to a double with casting

To debug any problems that you might run into Instructions:

3. Use comments liberally to document exactly what you are doing in the program.

4. Use descriptive variable names in camel-case with the first letter in lowercase.

5. Ask the user for the number of students and assign that to a variable of type int called numStudents using the nextInt method of Scanner.

6. At this point, try to compile and run your program. Dont move on until this part is working. It would be good to enter a temporary print statement just to make sure this is working: System.out.printf("numStudents = %d ", numStudents); It should be commented out before turning in the program (put // in front of it) or delete the line.

7. We need variables for highestName and highestScore of types String and int, respectively. Set highestName to the empty string and highestScore to 0.

8. Create a for loop. (Do NOT put a semi-colon after the for statement. Put an open curly brace instead, and go ahead and put the closing brace if your IDE doesnt do it automatically, then put all the statement to be repeated BETWEEN the curly braces): for (int i = 0; i < numStudents; i++) { a. In the loop, add an extra line that reads to the end of the line: input.nextLine(); This is needed after a nextInt if you are going to then read a String. This basically burns the rest of the line, even though there is nothing there that you can see. b. Ask the user for name and score, which will be String and int types, respectively, which will require nextLine and nextInt. c. Compare score to highestScore. If score is greater than highestScore, then assign highestScore equal to score and highestName equal to name. There will not be an else. (If they are equal, we are NOT going to change the highest score so that in the event of a tie, the first one wins. However, in real life we would deal with ties in a better manner.) d. This is the end of the for loop, so put the closing brace if its not already there.

9. Outside of the for loop, print highestName and highestScore.

10. You'll need to add variables for nextHighestScore and nextHighestName.

11. Add an if statement ABOVE the previous one to test if score is greater than nextHighestScore. This if statement will NOT have an else statement, but it will contain the if-else statements in #5 and #6 below. 12. In the if (score > highestScore) body, and above where you set your highest variables, set nextHighestScore = highestScore and set nextHighestName = highestName.

13. Add an else to the if (score > highestScore), and set nextHighestScore = score and nextHighestName = name. Be careful to make sure that this else goes with the right if statement.

14. After the closing brace of the for-loop, remember to print the next highest info after you print the highest info.

15. Once you get all that running, add additional variables above the for loop: scoreTotal of type int and scoreAverage of type double. Initialize each of these to zero.

16. Inside the for loop after inputting the score, take scoreTotal and add score to it using the += operator. (Typically, you would also add one to a count variable at this point, but since we already have the number of students variable numStudents, we don't need to track the count.)

17. After the for loop, set scoreAverage to scoreTotal divided by numStudents. You will have to cast scoreTotal to double before doing the division; otherwise, you will be doing integer division which will throw away all digits after the decimal point. You do not have to cast scoreCount because it will automatically be converted to double when the compiler sees that scoreTotal has been converted. scoreAverage = (double)scoreTotal/numStudents;

18. After printing the two highest scores, print scoreAverage with two digits after the decimal point.

19. Let your instructor know if you have any trouble with this

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

JDBC Database Programming With J2ee

Authors: Art Taylor

1st Edition

0130453234, 978-0130453235

More Books

Students also viewed these Databases questions

Question

1. Select the job or jobs to be analyzed.

Answered: 1 week ago