Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Concepts: Arrays  RSS feed

 
Casey Bright
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Here is the code that I have so far:

TestScores Class


TestScoresViewer Class


 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

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?
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
Casey Bright
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the feedback

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.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This and this will help.
 
Yuriy Paholkov
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Casey Bright, post it then the whole solution, if not difficult to please.
 
John Pacuta
Greenhorn
Posts: 26
1
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that code actually compile?
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.
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you importing Arrays? I don't think you actually ever use it.
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That isn't spaghetti code; the flow is as good as you get if you simply follow the instructions you were given. Of course there is a better way to write the processScores method:-
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Winston
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!