Results 1 to 5 of 5
  1. #1
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default 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;
    	}
    
    
    
    
    }
    AND THE DRIVER PROGRAM

    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());
    
    
    	}
    }

  2. #2
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    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 already
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  3. #3
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    Try this where you are getting your random number I added a pair of parenthesis.

    int randomNumber = (int) (Math.random()*5+1);

  4. #4
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default

    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!

  5. #5
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    You are welcome, and yes it happens so often that you stare a lot for a simple mistake :) and Have a nice Sunday pal :)
    Click on REP and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. need help learning methods and fixing my basic program
    By shazakala in forum New To Java
    Replies: 4
    Last Post: 03-21-2011, 10:12 AM
  3. Learning C...
    By Learning Java in forum New To Java
    Replies: 14
    Last Post: 09-30-2010, 10:54 PM
  4. e-learning
    By Vipan Konnect in forum Advanced Java
    Replies: 3
    Last Post: 11-21-2009, 05:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •