File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes math program help.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "math program help.." Watch "math program help.." New topic
Author

math program help..

Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
I have this project I need help with.. I am supposed to creat a simple math quiz/test and it will ask five questions each with four multiple choice answers..

The program needs to tell whether they are correct or incorrect for each answer, as well as display the correct answer if wrong.. It will also need to calculate the total percentage of correct answers at the end of the program..

I am listing what I have made so far, and wanted to know if something looks wrong here..



Any help is greatly appreciated...
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518

wanted to know if something looks wrong here


Yes, there are a few issues here.

I think you'll find many people willing to help, but unwilling to give you the solution.

I suggest asking a specific question. You'll get a specific answer that will get you closer to your goal.

It may be easier if you write your logic as psuedocode. For example:



That might allow you to focus on the logical steps without worrying about Java syntax. Once you've got the psuedocode correct, you can fill in around the comments with real code.
Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
I'm trying to compile and am getting 'else' without 'if' errors..
Here's the new code I am trying to compile..



I haven't started on the percentage calculation, or know how I should go about it regarding how many answers are right.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
might be easier for you if you were to create a Question object.
possible fields - the question itself, an array of choices, the correct answer etc
possible methods - displayQuestion(), displayChoices() etc

you can now put these Questions into a Question[]

iterate the Question[]:
display the question
display the choices
read the answer
compare to Question object's correct answer (if correct, correctCounter++)

do your percentages
Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
I don't think I can do the Question object thing because I haven't learned it yet and I think they want me to use only what I have learned so far..

Is that like a switch statement?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39



In Java, the "if" and "else" statements only execute one line (that follows it). If you want to run more than one line, you need to put it in blocks -- or you'll get "else without if" compilation errors.

Basically like this...



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Is that like a switch statement?

No, it's a class



then you create the objects
Question q1 = new Question(...);
Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
ok, now I'm getting variable a3,a4, and a5 might not have been initialized..
I thought I did initialize them correctly because the first two work ok..

[ September 03, 2006: Message edited by: Jeremy Parsons ]
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
Originally posted by Jeremy Parsons:
I don't think I can do the Question object thing because I haven't learned it yet and I think they want me to use only what I have learned so far..


That would be the day, a teacher who's unhappy with his students learning on their own...
Of course most teachers these days (and rightly so given the attitude of most students these days) don't expect their pupils to put in any effort beyond the bare minimum to get a passing grade (and preferably turn in work done by others instead), but that doesn't mean they wouldn't like things to be different.


42
parshuram bingi
Greenhorn

Joined: Aug 01, 2006
Posts: 10
Jeremy Parsons,

The following is the code and tested and it works fine, here you need to understand that after reading the input(you should not support to read 3 times as you done in your program) you should clean the buffer for that I am using 2 dummy read statements(any one is there any better way?).

import java.io.IOException;

public class MathTest
{

public static void main(String[]args) throws Exception
{

int a1,a2,a3,a4,a5;
int userInput;


// Question #1
System.out.println("1. 7 + 2 = ? a.6 b.8 c.9 d.10");
System.out.print("Enter your answer: ");
a1 = System.in.read();
userInput=System.in.read();
userInput=System.in.read();

if (a1 == 'c')
{

System.out.println("Correct!");
System.out.println();
}
else
{
System.out.println("Sorry! The correct answer is c");
System.out.println();
}

//Question #2
System.out.println("2. 9 - 5 = ? a.2 b.4 c.6 d.8");
System.out.print("Enter your answer: ");
a2 = System.in.read();
userInput=System.in.read();
userInput=System.in.read();

if (a2 == 'b')
{
System.out.println("Correct!");
System.out.println();
}
else
{System.out.println("Sorry! The correct answer is b");
System.out.println();
}
//Question #3
System.out.println("3. 144/12 = ? a.10 b.12 c.14 d.16");
System.out.print("Enter your answer: ");
a3 = System.in.read();
userInput=System.in.read();
userInput=System.in.read();

if (a3 == 'b')
{
System.out.println("Correct!");
System.out.println();
}
else
{System.out.println("Sorry! The correct answer is b");
System.out.println();
}
//Question #4
System.out.println("4. 5 * 5 = ? a.10 b.20 c.25 d.125");
System.out.print("Enter your answer: ");
a4 = System.in.read();
userInput=System.in.read();
userInput=System.in.read();

if (a4 == 'c')
{
System.out.println("Correct!");
System.out.println();
}
else
{System.out.println("Sorry! The correct answer is c");
System.out.println();
}
//Question #5
System.out.println("5. 2 + 3 + 5 = ? a.7 b.8 c.9 d.10");
System.out.print("Enter your answer: ");
a5 = System.in.read();
userInput=System.in.read();
userInput=System.in.read();

if (a5 == 'd')
{
System.out.println("Correct!");
System.out.println();
}
else
{System.out.println("Sorry! The correct answer is d");
System.out.println();
}




}
}

-Ram.


Hard Work, Honesty(2H) pays...
Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
ok, first of all I am not in a "class", I am self teaching through a distance learning school..

Second, I'm not asking someone to do my program for me and I turn it in, etc.. I am seeking help, but you are not helping, instead acting like an ass...

I'm working to figure out what my problem with my code is, and that's what this website is all about is Java, learning, and helpful people..

If you don't like it, stay off my posts...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

ok, now I'm getting variable a3,a4, and a5 might not have been initialized..
I thought I did initialize them correctly because the first two work ok..


Take a look at your code for question 3, 4, and 5. I believe that you may have gotten a bit aggressive with the cut-n-paste. Did you actually set the a3, a4, and a5 variables?

Henry
Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
I know why I got the last error.. in the line of code

a2 = System.in.read();System.in.read();System.in.read();

After the second one, I accidentally made them all a2 variables beside the input.. I changed and it now compiles..
Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
lol thanks.. I believe we both noticed it at the same time..

Now I just need help figuring out how to calculate correct answers for a total percentage of correct to display..

should I do something like this..

if (a1 && a2 && a3 && a4 && a5)
System.out.println("You graded 100%");

and just repeat that but take one away from each??
parshuram bingi
Greenhorn

Joined: Aug 01, 2006
Posts: 10
It is not fisable to do so...., how can you do the same if you are having more no of question. I suggest to go with the counter. when ever you find a correct answer increment the counter and finally you can computer the percentage with the counter.

-ram
Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
ah, didn't think about a counter, I was thinking way too complex or something.. Thanks, I'll try a counter and post the results..
Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
I have completed the code, and the counter works, but when my calculation into a percentage displays say if I get all 5 right, it only displays 20%

Here's the full code..


What do I need to look at to fix the calculation? PS I'm not that great at math as it is...lol
Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
ok I got it, you can disregard the last post.. I should have taken down the division part..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: math program help..
 
Similar Threads
Gradebook
Test 283
"Language Fundamentals" Questions
Break back to Topic Menu
Can't get to next question...