Results 1 to 5 of 5
- 04-17-2011, 04:11 PM #1
Member
- Join Date
- Mar 2011
- Location
- San Diego, CA
- Posts
- 34
- Rep Power
- 0
Learning OOP and methods but missing something
Hoping someone can help me with this. I am almost done with this assignment but I have two problems - 1) I can't pass the className and classNumber info (which is in the method classInfo), and 2) my random answers are only picking the first choice.
Thanks for any help,
Gary
Java Code:import java.util.*; public class MathTutor { // Instance variables - will need these so students can enter in their own info private String className; private int classNumber; private int randomNum1; private int randomNum2; private int result; private int studentAnswer; private int correctAnswer; private int wrongAnswer; Scanner keyboard = new Scanner(System.in); public void setClassInfo(String cName, int cNumber) { className = cName; classNumber = cNumber; System.out.println("Your class name is " + className + " and your class number is " + classNumber); } public void equalsNumber() { for (int counter = 0; counter < 5; counter++) { randomNum1 = (int) Math.round(Math.random()*9+1); randomNum2 = (int) Math.round(Math.random()*9+1); result = randomNum1 *randomNum2; System.out.println("What is " + randomNum1 + " x " + randomNum2); studentAnswer = keyboard.nextInt(); if (result == studentAnswer) { correctAnswer(); correctAnswer++; } else { wrongAnswer(); wrongAnswer++; } } } public void correctAnswer() { int randomNumber = (int) Math.random()*5+1; switch (randomNumber) { case 1: System.out.println("You got it!"); break; case 2: System.out.println("Exactly right!"); break; case 3: System.out.println("Tremendous!"); break; case 4: System.out.println("That works!"); break; case 5: System.out.println("Nice Guess!"); break; default: System.out.println("Error"); } } public void wrongAnswer() { int randomNumber = (int) Math.random()*4+1; switch (randomNumber) { case 1: System.out.println("Don't give up!"); break; case 2: System.out.println("You'll get it next time!"); break; case 3: System.out.println("Try it again!"); break; case 4: System.out.println("Sorry, keep trying!"); break; default: System.out.println("Error"); } } public String toString() { if (correctAnswer * 20 < 70) { return ("your course was " + className + " and section is " + classNumber + "\nyour total number correct out of 5 was " + correctAnswer + "\nyour total number incorrect was " + wrongAnswer + "\npercentage of correct responses is %" + correctAnswer * 20 + "\nPlease see instructor for help"); } else { return ("your course was " + className + " and section is " + classNumber + "\nyour total number correct out of 5 was " + correctAnswer + "\nyour total number incorrect was " + wrongAnswer + "\npercentage of correct responses is %" + correctAnswer * 20); } } public boolean equals(MathTutor otherTutor) { return (this.correctAnswer == otherTutor.correctAnswer); } public String getClassName() { return className; } public int getClassNumber() { return classNumber; } }
Java Code:import java.util.Scanner; public class MathTutorTest { // Main program method to get the work done public static void main(String [] args) { Scanner keyboard = new Scanner(System.in); int runProgram = 0; MathTutor quiz = new MathTutor(); System.out.println("What is the name of the class?"); String className = keyboard.next(); System.out.println("What is the course number?"); int classNumber = keyboard.nextInt(); quiz.setClassInfo(className, classNumber); System.out.println(quiz.classInfo()); System.out.println("Would you like to test your math skills?"); System.out.println("Please enter 1 for yes, 2 to exit"); runProgram = keyboard.nextInt(); if (runProgram == 1) { quiz.equalsNumber(); } System.out.println(quiz.toString()); } }
- 04-17-2011, 04:33 PM #2
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
First notice is that you are calling a method quiz.classInfo() when in your MathTutor you haven't declared such a method. so this is causing the first problem.
You don't need to print that out since in your setClassInfo you printed them alreadyClick on REP and add to member reputation, if you find their advices/solutions effective.
- 04-17-2011, 04:37 PM #3
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
Try this where you are getting your random number I added a pair of parenthesis.
int randomNumber = (int) (Math.random()*5+1);
- 04-17-2011, 05:06 PM #4
Member
- Join Date
- Mar 2011
- Location
- San Diego, CA
- Posts
- 34
- Rep Power
- 0
You guys are awesome! I got it to work. Amazing how you can stare at code for hours and not see the missing parenthesis. Thanks so much, you gave me back my Sunday!
- 04-17-2011, 05:23 PM #5
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
need help learning methods and fixing my basic program
By shazakala in forum New To JavaReplies: 4Last Post: 03-21-2011, 09:12 AM -
Learning C...
By Learning Java in forum New To JavaReplies: 14Last Post: 09-30-2010, 09:54 PM -
e-learning
By Vipan Konnect in forum Advanced JavaReplies: 3Last Post: 11-21-2009, 04:29 PM
Bookmarks