Click here to Skip to main content
15,903,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Write a program that that will help a student learn multiplication. Use SecureRandom object to produce two positive integers between 1 and 20. The program should then prompt the user with a question (use a sentinel-controlled loop), such as: How much is 10 times 11?
The student then inputs the answer. If the answer is correct display the message “very good” and ask another question. If the answer is wrong to display the message “no, please try again” and let the student try the same question until the student finally gets it right. You must create two java files: Multiplication.java and MultiplicationTest.java. In Multiplication add two methods: CreateQuestion and CheckResponse. In MultiplicationTest.java, test your method.

What I have tried:

I have tried to do basically everything, but there's always something wrong with my code. I fix one thing and it messes up other things in the file. Here's my code for Multiplication class.

import java.security.SecureRandom;
import java.util.Scanner;

public class Multiplication {
private final static SecureRandom myRandom = new SecureRandom (); //create a SecureRandom objec
private int num1;
private int num2;
private int answer;
private int guess;

public void createQuestion (){
Scanner input = new Scanner (System.in);
num1 = 1 + myRandom.nextInt(20);
num2 = 1 + myRandom.nextInt(20);

answer = num1 * num2;
System.out.printf("What is %d times %d", num1, num2);
guess = input.nextInt();
}
public String checkResponse (){
if (guess == answer){
System.out.println("That's correct");
}
else {
System.out.println("No. Please try again!");
}
}
}
Posted
Updated 18-Oct-18 22:42pm

Quote:
I fix one thing and it messes up other things in the file.

When asking for help, it is a good idea to explain how things go wrong with an example to reproduce the problem.
Quote:
I have tried to do basically everything, but there's always something wrong with my code. I fix one thing and it messes up other things in the file.

May be it is time to try the debugger and watch your code performing, it should help you to understand what exactly happen behind the hood.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Member 14007867 19-Oct-18 14:45pm    
Thank you so much! I really appreciate it!
CPallini 20-Oct-18 11:43am    
My 5 against the Evil Downvoter. :-)
Patrice T 20-Oct-18 14:43pm    
Thank you
Try (I've not used a separate file for testing. Testing is done in the main function):
Java
import java.security.SecureRandom;
import java.util.Scanner;

public class Multiplication {
  private final static SecureRandom myRandom = new SecureRandom (); //create a SecureRandom objec
  private int answer;

  public String createQuestion ()
  {
    int num1 = 1 + myRandom.nextInt(20);
    int num2 = 1 + myRandom.nextInt(20);

    answer = num1 * num2;
    return String.format("What is %d times %d", num1, num2);
  }
  public boolean checkResponse (int guess)
  {
    return guess == answer;
  }

  public static void main( String args[] )
  {
    Scanner input = new Scanner (System.in);
    while ( true )
    {
      Multiplication mult = new Multiplication();
      String question = mult.createQuestion();
      boolean answerOK = false;
      do
      {
        System.out.println(question);
        int guess = input.nextInt();
        answerOK = mult.checkResponse(guess);
        if ( answerOK )
          System.out.println("That's correct");
        else
          System.out.println("No. Please try again!");
      } while ( ! answerOK );
    }
  }
}
 
Share this answer
 
Comments
Member 14007867 19-Oct-18 14:45pm    
Thank you so much! I really appreciate it!
CPallini 20-Oct-18 11:42am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900