im creating a java program using Jgrasp and im completely stuck on how to apply my random operator to calculate two random numbers? heres my code:

//Import Random Generator
import java.util.Random;
import java.util.*;
//Program name
class TimesTableTestTimer
{//open main
public static void main(String[] args)
{//Create while loop to restart
char Restart = 'Y';
while (Restart == 'Y' | Restart == 'y')
Random Generator = new Random();
//Create Random Operators
char[] chars = new char[]{'+','-','/','*'};
int r = Generator.nextInt(chars.length);
char op = chars[r];
char Answer = op;
//Create 2 Random Number Generators
float X = 1 + (int)(Math.random()*13);
float X2 = 1 + (int)(Math.random()*13);

{//Timer variables
double t1, t2, ttaken;
String Key = "";
System.out.println("Welcome to the Times Table Test Timer!");

System.out.println("Press the 'Enter' Key to start the Next Question");
//Start Timer
for (int i = 1;i <= 4;i++)
t1 = (System.currentTimeMillis());

float   Sum = (X + X2); //How to use the random operator instead of '+'

System.out.println(X +" ? "+X2+" = "+Sum+" "+op);
System.out.println("What is the Value of '?'...");

Answer = KBInput.readChar();

if (Answer == op)

//End Timer
t2 = (System.currentTimeMillis());
ttaken = t2-t1;
System.out.println("Your time taken is "+ttaken/1000+" Seconds.");
}//Finish sequence to loop

{//Ask to restart
System.out.println("Thank you for completing the Quiz");
System.out.println("Press 'y' to restart or any other key to exit");
Restart = KBInput.readChar();
}//Close restart option

}//Close Loop
}//Close Introduction
}//Close Variables
}//Close Randoms
}//Close While Loop
}//Close Main

ive tried looking everywhere but the forums seem to be the best place for help thanks in advanced

Edited by happygeek: fixed formatting

8 Years
Discussion Span
Last Post by Antenka

Here's little example for better understanding:

Operation op = new Minus();  // you can dynamicly set the operation         
XObject x = new XNumber(5.0); //creating numbers
XObject x1 = new XNumber(4.0); //creating numbers
XObject r = op.operate(x, x1); //doing operation
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.