• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Array to Determine Grades

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok so i know this is wrong......very wrong. i obviously don't know whats going on here. i have a base array with an output of sum, average, largest test score and lowest test score that i created. i have to add a letter grade to these scores and how many scores are of that letter grade for example if a score above 90 gets entered 3 times it needs to say "the number of students with scores of 90-100 (A) is 3. so here is what i did....very wrong i know.




here is what i built earlier and began to modify



i cant find any information on building an array to add up these numbers based on what numbers they are. couldn't find the correct way to search for it on the net and i don't believe my book goes over it.

again any help is very much appreciated.

Thanks,
Rob
 
Rob Frank
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry i did something wrong with posting and i don't see an edit post button.



and here is my original






 
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see that you attempted to use Code tags. Two points for trying but they didn't go in quite right. I fixed it for you this time. You first need to highlight your block of code by dragging the mouse over it, then click the "Code" button. To verify that it worked correctly, click on the "Preview" button just below the edit window.
 
Rob Frank
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lol i wondered how my original got fixed by the time i fixed it myself on the second post . thanks
 
Bartender
Posts: 5563
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Rob,

as you will have noticed, this way is not very handy, to say the least... As soon as you get a new set of observations, you need to edit your main class.

So, a better way is to create a Statistics class. As soon as you have your observations, you instantiate this class, add the observations, and off you go.

To get you started, I made a simple Statistics class, to demonstrate how it could work. I used an ArrayList in stead of a fixed size array, so you don't need to know upfront how many observations to deal with.
I also used some handy java 8 methods, but if that is currently a brindge too far, then you can replace them by the means you have currently available. Here goes:
 
Rob Frank
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey thanks. i'll work with that tonight after work and see how it goes. i will say that using throw new runtimeextension was not covered at all in this class so i will stay away from that for now.
 
Rob Frank
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok so i tried playing with that a little bit but i have to ask. could you dumb that down for me.  i know that i can't use the throw new and runtime exception because that wasn't covered in my course but i know how to replace that. but the variables ArrayList and List i'm not sure what do with. I've only had practice with using an array on the last couple assignments and they were simple and actually spelled out. to see the level of difficulty of the previous assignments my original i have posted is the last assignment that i've done that i'm trying to modify. i'm so frustrated that i get assignments like this that seem so simple to others and i have to beat my head off the desk because this material wasn't covered.
 
Carey Brown
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I myself use the Statistics class approach, however I feel that that is above your experience level and your requirements can be achieved with some minor additions to your original code.
 
Ranch Hand
Posts: 108
2
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Mr. Rob Frank,

Variable have to initialized before its implementation:
So, I have corrected your end of source part as show in below:




Thanks

Regards
Dr. Noorul
scr1.png
[Thumbnail for scr1.png]
scr2.png
[Thumbnail for scr2.png]
 
Carey Brown
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not that by using "else if" you no longer need the "&&" component because that is taken care of by the order that these comparisons are made.
 
Rob Frank
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok so if i get all that going it works but if i enter 5 scores i have to click 5 times and i see my letter grades appear 1 by 1 and the sum and average also go up 1 at a time. my loop is not looping back before displaying the final joptionpane. i'm not sure if i need to change the for loop or if i need a return statement. i tried using a return statement a couple different ways and either got an error or the program just doesn't run (visually) and says build complete.

 
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ok so if i get all that going it works but if i enter 5 scores i have to click 5 times and i see my letter grades appear 1 by 1 and the sum and average also go up 1 at a time.
my loop is not looping back before displaying the final joptionpane.



I ran the program. What do you mean by "my loop is not looping back before displaying the final joptionpane."?

i'm not sure if i need to change the for loop or if i need a return statement. i tried using a return statement a couple different ways and either got an error or the program just doesn't run (visually) and says build complete.



This is not clear to me.

What is it the program is not doing and what is the expectation.
 
Noorul Hameed
Ranch Hand
Posts: 108
2
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The problem is to close the for loop befor last statement, JOptionPane...

It can not be inside the loop. It is after for loop completed.

Thanks
Regards
Dr.Noor
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Noorul Hameed wrote:Hi, The problem is to close the for loop befor last statement, JOptionPane...It can not be inside the loop. It is after for loop completed.
Thanks
Regards
Dr.Noor



I get it, thanks.
 
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Not that by using "else if" you no longer need the "&&" component because that is taken care of by the order that these comparisons are made.

Anther thing about else‑if. If your boundary value is included in the upper category, start small. The boundary value for a C is 70, and 70 is included in the C grade, not the D grade. If you start small, you don't need >=, but <. The >= and <= operators aren't actually bad, but < and > are easier to understand.When you write that sort of thing always keep the terms in order, ascending or descending. You might so well to precede that if with something likeRemember it is usually not a good idea to write lots of number literals like that, but that is a different problem.
 
Rob Frank
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok so all you guys are great. i was able to get this going. had to play around with adding a "do while" loop so i just get one output at the end. here it is. I still can't use the throw new because my material doesn't cover that and i'm trying to stay away from adding things that i really shouldn't know to my code. so on that note i'm trying to add a return for line 13


 
Rob Frank
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to do this
it obviously doesn't work.

 
Noorul Hameed
Ranch Hand
Posts: 108
2
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Carey Brown wrote:

Hi Mr. Rob
This task has already been explored by Mr Cambell Richie. Please have a look at quote here.
something likeRemember it is usually not a good idea to write lots of number literals like that, but that is a different problem.



Thanks.

 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Frank wrote:I'm trying to do this
it obviously doesn't work.



Try this code. This loops until the valid scores are entered:



Note the VALID_SCORE_LOOP is called as a loop label. And, this can also be used with for-loops.
 
Rob Frank
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok its put together and everything works great. i cant enter anything to make the program not work correctly so i'm thrilled. just to let anyone know because it seems like i'm asking stupid questions and i'm telling you i can't use certain items because they weren't in my course. this is my final project (not worth a lot of points or anything but it is my last assignment) i've only asked for help on this one and the one previous. i should finish this class with over %100. so if you can imagine i'm having trouble with this that means everything else i've done has been so simple its stupid and it obviously didn't teach me enough. a few of you have taught me alot with just a few lines of code. I really appreciate it.


 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if it's in the scope of your project, but I can easily crash your program by entering letters instead of numbers.  Do you need error processing?
 
Rob Frank
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are correct it does crash when a letter is entered. although i do not think its required at all to actually fix that for this assignment i am very curious on how you do that. i tried to change my statement to something like if scores != Integer and other things like that and it doesn't even come close to working or really giving me any other ideas.
 
Rob Frank
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm also wondering if count A , B, C, D and F can be made into an array. i'm done being stressed about this now so i would actually like to spend some time with this program and learn.
 
Campbell Ritchie
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Frank wrote:i'm also wondering if count A , B, C, D and F can be made into an array. . . .

Of course they can. But I can see problems lining up the grades and the numbers in the array.
 
Carey Brown
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need your
do { ... } while (countA + countB + countC + countD + countF < scores.length);
Your for() loop already deals with that. The problem you were having before was that your showMessageDialog() needed to be outside the for() loop.

To use an array for the counters
Seeing as how you only have 5 grades, I don't see that this buys you much, but it is interesting from a coding standpoint.
 
Piet Souris
Bartender
Posts: 5563
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's "dumb" the class I gave. Mind you, the story goes that with java 8 you only need to tell java what to do, and it decides for itself how to do it, while with previous java versions, you must also tell how to do it. Well, that's the story, and if true, we are not "dumbing" it, but making it more difficult....

So, here is the dumbed version, that now uses an array, and has a constructor that takes an array of observations as parameter.

Now, suppose you are qiven the results of two groups of students, groupA and groupB, with the question to compare both results on average, min, max, variance and grades frequency. You could now do it like this:

You see that the method 'grades' returns an array, representing the frequencies of A, B et cetera. Ahead of schedule, compare this to what I did to get the frequencies with java 8: just one line of code!

And also: inputting an array via JOptionPane is not easy, as you noticed. It is far easier to make some files in this format:
73.4
80.8
91.6
30.20

and read these files in your code (if that subject has been taught yet). Well, I hope the advantages of a Statistics class are clear. It just lets you focus on the methods that you want, without getting a large inflexible main method. So, if you want things like curtosis or some sample from some distribution, you only need to adapt that class.
 
Carey Brown
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a refactored version. This encapsulates the GradeStatistics into its own class, similar to Piet's. It also uses an enum for a Grade. The Grade ordinal can be used as an index into a counts array. It uses an array of counts, one for each grade. The logic of entering and verifying a score has been moved from main() to its own method.

The overall number of lines of  code is a bit more but there is more encapsulation of responsibilities. Note how tiny main() has become and therefore much easier  to follow. Grade and GradeStatics could potentially be used in another project. It is also now possible to have multiple instances of GradStatistics going at the same time.
 
Rob Frank
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is all really good stuff. i can see how only knowing how to do a few things with java can really complicate the code. not sure learning how to complicate the code should be in a beginning java class. anyways i removed my do while loop for the count and added a brace to move my output outside the loop. that did correct it and the program works fine. at this point it looks like the program functions the way it should and only includes items that i have gone over in this class.  and building the array using joptionpane wasn't something i came up with. that was the program i was instructed to build previously and had to work off of that. yes i see that it again complicates things.  i do think that if i take another java class i'll invest in a real book. the required book for this class was alice 3 with java and while alice 3 is "fun" i didn't find it very useful in taking up so many pages in the book and not going over enough actual coding.
 
Carey Brown
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm glad you were successful. As you can see as a thread on this forum gets past the stage where the original poster has (mostly) succeeded other people tend to chime in with their own refactorizations using various features of Java that may not be in-scope for the OP but it does help to stretch the OP's knowledge.

Congrats.
 
Carey Brown
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I might still suggest making an enterScore() method and get some of that mess out of main(). Data entry and validation usually requires their own methods in order to keep the calling methods cleaner and possibly yielding some reuseability.
 
Piet Souris
Bartender
Posts: 5563
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Frank wrote:(...) i can see how only knowing how to do a few things with java can really complicate the code. not sure learning how to complicate the code should be in a beginning java class. anyways


The problem with java 8 is that the way to describe what must be done is often pretty complicated, taking quite some time to master. Being used to ways used in previous versions you may well find it a lot easier dong it the pre java 8 way...  Java does not always come easy.
 
Rob Frank
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well other people posting what they think is the correct way is very useful even if i don't use it. it is a great way for me to see how things can be used and work. i should have became a member of this site a while ago. even though i didn't have that much trouble with previous assignments allowing other users to input their ideas would have really broadened my knowledge of using java. once again thanks to all that have replied. and thanks to you guys this assignment isn't graded yet but i'm sure i will get a good score on it and should finish with over 100 percent with the extra credit i'm due on some previous projects.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's my take on how to get an integer.  My getInt() is reuseable.  I use scan.nextLine() and Integer.parseInt() to give me a little more control, although it's not necessary in this situation.  You can also pass the prompt into getInt() or have a default value that gets set if you enter just an <enter> if you want to add that.
 
Campbell Ritchie
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there are easier ways to get an int, using the trick Rob Spoor taught me a long time ago:-That method can be overloaded to print a custom error message, or to constrain the input to a predefined range. No need for any exceptions. The \u2019 Unicode character is a posh apostrophe ’.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a cool little method.
 
Piet Souris
Bartender
Posts: 5563
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure whether this iopic is about getting some decent statistics, or getting an int from the keyboatd: as I remarked before, in real life the number of observations will be such that inputting these according to the methods mentioned, will be awkward. A  better way is to type the observations into NotePad or such, save it, and read the file into the code, even if that involves some extra java to learn.
 
Skool. Stay in. Smartness. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic