programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Mike Mo
Ranch Hand
Posts: 40
So my girlfriend is in pharmacy school and she has exams coming up next week. i told her i could make her  a program to help her study faster and to help me practice as well. she has 64 multiple choice a, b, c, d, or e. i want to store these in an array (a=1, b=2, c=3, etc). so i would have an array that is 64 int's long. then i could use a loop to ask the user for answer to question #(starting at 1 and ++ after each loop). it would then save question # 1 answer in thisArray[0] space and question # 2 answer in thisArray[1] space and so on. then i want to compare the 2 arrays and see how many of the corresponding entries are different. i can then use the # of false boolean values returned over the 64 questions and pring which ones were missed as well as a % and grade that you would have gotten with the given answers. she has an exam from 2008 that her professor gave them as a study guide. maybe im on the right track and maybe im totally off with my plan here. any help would be great as she needs it hopefully before tomorrow when she goes back home in the evening for 3 weeks.

Carey Brown
Saloon Keeper
Posts: 3310
46
Mike Mo wrote:
It doesn't seem like you want nested loops. The way you've written it the loop at line 19 runs 64 times and the loop at line 21 runs 65 times. That means that line 23 will be executed 4160 times.  Obviously you don't want someone to sit there entering 4160 int's. Looks like you need two loops, but they shouldn't be nested.

Piet Souris
Master Rancher
Posts: 2042
75
After the first inner loop is finished, the outer loop is also finished (to Mike: why?).

An alternative approach would be to create a Question class, containing one or more equal questions (Strings), but differently formulated, and a couple of answers (again Strings), of which one is the correct answer.
Having such a class, it is then possible to randomize the question to be asked, and to shuffle the possible answers as well. That would make it a more useful training program, at the cost of typing in a awful lot of text...

Junilu Lacar
Sheriff
Posts: 11477
180
• 1
@Piet Since Mike has all his code in main(), it highly unlikely he's been taught anything about objects and classes yet.

@Mike love can make you do some really... well, let's just say you ain't gonna get any points with your girlfriend on this one, buddy, at least not by tomorrow. Never bet your life on a program that has all its code in main(). Main is a Pain and in this case, it'll only bring you pain.

Piet's' suggestion is spot on but I'm afraid it's way over your head at this point. You haven't learned about proper classes and objects yet, have you?

Do you know how to make a Scanner read from a file?

Piet Souris
Master Rancher
Posts: 2042
75
That might very well be the case indeed. But it is not some course assignment, so I am curious for Mike's reply. Maybe he is interested in making a start with such a class, that could be somewhat simpler than described?

Mike Mo
Ranch Hand
Posts: 40
yeah i know how to import and use scanners and objects and classes lol. i didnt think that would all be needed. im not trying to expand this to something more or change anything. randomizing questions isnt what im after. she just wants to use something like this to study. i just need to compare the answer she inputs with the correct answer then tell her which ones she missed and her % out of 100 at the end.

i guess i need to make an answer class and a test class or something then just use the main to input. i could store her answers to a new array as she inputs them then later on call a method to compare them and get the differences. thats what the solution was to me. but iv been programming for like 5 ish weeks now so im sure im missing some things

thanks for the help

Junilu Lacar
Sheriff
Posts: 11477
180
Ok, if you're going to go for an object-oriented solution, then go all the way.

This isn't object-oriented thinking:
i could store her answers to a new array as she inputs them then later on call a method to compare them and get the differences. thats what the solution was to me

What you're doing here is taking over the responsibility of the object, pulling out information from the object, and doing something with that information outside of where the information should be kept encapsulated.

Think of it like this: You have a wallet with money in it. If somebody asks me how much money you have on your person, then I would reach into your pocket, pull out your wallet, open it, count your money, then tell whoever it was who asked how much I counted.  You'd think that as a courtesy, I could at least put the money back into the wallet and put the wallet back in your pocket, right? That's not what I do though, because once the wallet has been taken out of your pocket, the cat is already out of the bag, if you'll excuse the mixed metaphors. So I just leave the wallet out there in the open. Pretty rude, right?

That's exactly what you were thinking of doing with these objects though.

This is how you'd do it properly with objects, without breaking encapsulation:

Create a Quiz class. A Quiz will have a List<QuizItem>. A QuizItem would have a question, several possible answers to choose from, the correct answer, and the student's answer. When you're giving the quiz, you display each QuizItem's question and the possible answers. The student answers and you put their answer back into the QuizItem.  After you've done this for all the QuizItems, you just iterate over the List<QuizItem> and count how many of the items have been answered correctly. In code, some of that would look like this (I'm not going to give you a complete solution; she's your girlfriend, not mine).

Notice that the method only reports what it found out after it looked through the private allItems variable to calculate the raw score. Nothing was ever taken from the object to be used by something outside of it. What's private stays private. The only way you will know what the raw score is is by calling the public showResults() method.

The code shown in the rawScore() method is the imperative way to do the calculation. If you use Java 8 streams, you could do it in a more functional way that doesn't even show the details of iterating over the array list of QuizItem objects and counting correct scores how many were answered correctly. I'll leave that to you as an exercise if you want to try the Java 8 way.