So I've been learning Java code for the past few weeks now and started learning about Arrays.
There is an assignment that I wanted to do and have been working on for the past couple days now and I cant for the life of me figure out how to go about what I have so far.
If someone could please help me that would be much appreciated!!
Test Scores Assignment Create a TestScores class and TestScoresViewer client class to do the following.
The TestScores class should contain the following two methods:
- Write a inputScores() method that continually prompts the user for test scores.
*No more than ten test scores should be entered.
*Test scores are integer values between 0 and 110 (we’ll assume that up to 10 points extra credit could be allowed on a test).
*Use an input dialog window (use the JOptionPane.showInputDialog method).
(If the user enters “999” then that indicates they are finished entering values, and the program should exit out of the loop. )
*Convert each test score entered into an integer and store it into an array.
*Keep track of how many test scores are entered.
- Write a processScores() method that loops through this test scores array as follows:
*Read each value from the test scores array.
*Print each test score to the console on one line, separated by commas (i.e. 100, 90, 85, 70).
*Print the lowest test score to the console.
*Print the highest test score to the console.
*Print the average test score to the console.
I am afraid your code isn't going to work. You have been told to take ten scores, but you have a 1‑element array in line 20. I am afraid I think you are going to have to start again.
I suggest you start by creating an 10‑element array and you try filling it. Verify that you have filled it like this:-
System.out.println(java.util.Arrays.toString(myArray)); Avoid while (true) ... as far as possible. I presume you know how to write a loop to fill an array when the array's size is already known? I presume you know how to get the size out of an array?
It is a really bad idea to have variables named "testScore" and "TestScore", and an array named "testScores - the names are too similar and can easily lead to a lot of confusion. Also, by convention variables start with a lower case letter. In your case, a better choice might be something like "String scoreInput" and "double scoreValue", and an array named "scores"
That is my problem, I don't exactly know how to go about making it where I can input 10 integers using JOptionPane and I don't know how to write a loop fill an array once those inputs have been made.
I fairly new to this so I need guidance in doing this through step by step.
From Greenhorn to Greenhorn haha! I'm going to do a little refactoring and see if I can make it simpler.
Definitely some room for improvement in the naming. I found I was thinking too hard about which value I was looking at. For example a string value had an identical name to the double array. So I change the naming and wrote some spaghetti code. I hope this helps in some way!
Campbell Ritchie wrote:Why are you using 999 and break? You were told to fill the ten‑element array, so you don't want to break out of the loop.
Actually he does. Check out the original instruuctions.
However (@John and Casey): it is still possible to do that without a break statement - something that many of us so-called experts tend to avoid - although in this case, it may be the simplest solution.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Would you like to try a free sample? Today we are featuring tiny ads: