Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package uk.co.lhspro.src;
- import java.util.Random;
- public class Equation {
- //-----------------------------------------------------[Constant Properties]
- private final static int DEFAULT_RANGE = 10;
- private final static int DEFAULT_NUM_RANGE = 50;
- private final static int EXPRESSION_RANGE = 3;
- private final static int ADDITION = 0;
- private final static int SUBTRACTION = 1;
- private final static int MULTIPLICATION = 2;
- private final static int DIVISION = 3;
- //-----------------------------------------------------[Properties]
- private static Random rand = new Random();
- private int num1;
- private int x;
- private int num2;
- private int y;
- private char expression;
- private float answer;
- private final int range[] = {DEFAULT_RANGE, DEFAULT_NUM_RANGE};
- private char charX = 'x';
- private char charY = 'y';
- //-----------------------------------------------------[Constructors]
- public Equation(int range, int numRange, char x, char y){
- setX(rand.nextInt(range));
- setY(rand.nextInt(range));
- setNum1(rand.nextInt(numRange));
- setNum2(rand.nextInt(numRange));
- setExpression(rand.nextInt(EXPRESSION_RANGE));
- setCharX(x);
- setCharY(y);
- createEquation();
- }
- public Equation(char x, char y){
- this(DEFAULT_RANGE, DEFAULT_NUM_RANGE, x, y);
- }
- public Equation(boolean random){
- this(DEFAULT_RANGE, DEFAULT_NUM_RANGE, (char)(rand.nextInt(25) + 97), (char)(rand.nextInt(25) + 97));
- if(!random){
- System.err.println("Please use default constructor Equation()");
- setCharX('x');
- setCharY('y');
- }
- }
- public Equation(){
- this(DEFAULT_RANGE, DEFAULT_NUM_RANGE, 'x', 'y');
- }
- //-----------------------------------------------------[Private Methods]
- private void createEquation() {
- switch(this.expression){
- case '+':
- this.answer = (num1 * x) + (num2 * y);
- break;
- case '-':
- this.answer = (num1 * x) - (num2 * y);
- break;
- case '*':
- this.answer = (num1 * x) * (num2 * y);
- break;
- case '/':
- this.answer = (float) (num1 * x) / (num2 * y);
- }
- }
- //-----------------------------------------------------[Getters & Setters]
- /**
- * @return the x
- */
- public int getX() {
- return this.x;
- }
- /**
- * @param x the x to set
- */
- private void setX(int x) {
- this.x = x;
- }
- /**
- * @return the y
- */
- public int getY() {
- return this.y;
- }
- /**
- * @param y the y to set
- */
- private void setY(int y) {
- this.y = y;
- }
- /**
- * @return the expression
- */
- public char getExpression() {
- return this.expression;
- }
- /**
- * @param num between 0 and 3 to generate an expression for the equation
- */
- private void setExpression(int num) {
- switch(num){
- case ADDITION:
- this.expression = '+';
- break;
- case SUBTRACTION:
- this.expression = '-';
- break;
- case MULTIPLICATION:
- this.expression = '*';
- break;
- case DIVISION:
- this.expression = '/';
- break;
- default:
- System.err.println("Not valid int to generate Expression");;
- }
- }
- /**
- * @param exp to set the expression
- */
- private void setExpression(char exp) {
- if(exp == '+' || exp == '-' || exp == '*' || exp == '/'){
- this.expression = exp;
- } else {
- System.err.println("Expression is invalid or not yet implemented.");
- }
- }
- /**
- * @return the charX
- */
- public char getCharX() {
- return this.charX;
- }
- /**
- * @param charX the charX to set
- */
- public void setCharX(char charX) {
- this.charX = charX;
- }
- /**
- * @return the charY
- */
- public char getCharY() {
- return this.charY;
- }
- /**
- * @param charY the charY to set
- */
- public void setCharY(char charY) {
- this.charY = charY;
- }
- /**
- * @return the num1
- */
- public int getNum1() {
- return this.num1;
- }
- /**
- * @param num1 the num1 to set
- */
- public void setNum1(int num1) {
- this.num1 = num1;
- }
- /**
- * @return the num2
- */
- public int getNum2() {
- return this.num2;
- }
- /**
- * @param num2 the num2 to set
- */
- public void setNum2(int num2) {
- this.num2 = num2;
- }
- public int getRange(){
- return this.range[0];
- }
- public int getNumRange(){
- return this.range[1];
- }
- @Override
- public String toString(){
- return (Integer.toString(this.num1) + this.charX
- + ' ' + this.expression + ' '
- + Integer.toString(this.num2) + this.charY
- + " = " + this.answer);
- }
- //-----------------------------------------------------[Test Driver]
- public static void main(String[] args){
- Equation eq = new Equation();
- System.out.println( "Num1: " + eq.getNum1()
- + "\nx: " + eq.getX()
- + "\nExpression: " + eq.getExpression()
- + "\nNum2: " + eq.getNum2()
- + "\ny: " + eq.getY()
- + "\nChar x: " + eq.getCharX()
- + "\nChar y: " + eq.getCharY()
- + "\nRange: " + eq.getRange()
- + "\nNum Range: " + eq.getNumRange()
- );
- System.out.println(eq);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement