Advertisement
Huskymeister

Random Equation

Feb 14th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.96 KB | None | 0 0
  1. package uk.co.lhspro.src;
  2.  
  3. import java.util.Random;
  4.  
  5. public class Equation {
  6.  
  7.     //-----------------------------------------------------[Constant Properties]
  8.    
  9.     private final static int DEFAULT_RANGE = 10;
  10.     private final static int DEFAULT_NUM_RANGE = 50;
  11.     private final static int EXPRESSION_RANGE = 3;
  12.    
  13.     private final static int ADDITION = 0;
  14.     private final static int SUBTRACTION = 1;
  15.     private final static int MULTIPLICATION = 2;
  16.     private final static int DIVISION = 3;
  17.    
  18.    
  19.     //-----------------------------------------------------[Properties]
  20.    
  21.     private static Random rand = new Random();
  22.    
  23.     private int num1;
  24.     private int x;
  25.     private int num2;
  26.     private int y;
  27.     private char expression;
  28.     private float answer;
  29.     private final int range[] = {DEFAULT_RANGE, DEFAULT_NUM_RANGE};
  30.    
  31.     private char charX = 'x';
  32.     private char charY = 'y';
  33.  
  34.     //-----------------------------------------------------[Constructors]
  35.    
  36.     public Equation(int range, int numRange, char x, char y){
  37.        
  38.         setX(rand.nextInt(range));
  39.         setY(rand.nextInt(range));
  40.         setNum1(rand.nextInt(numRange));
  41.         setNum2(rand.nextInt(numRange));
  42.         setExpression(rand.nextInt(EXPRESSION_RANGE));
  43.         setCharX(x);
  44.         setCharY(y);
  45.         createEquation();
  46.     }
  47.    
  48.     public Equation(char x, char y){
  49.        
  50.         this(DEFAULT_RANGE, DEFAULT_NUM_RANGE, x, y);
  51.     }
  52.    
  53.     public Equation(boolean random){
  54.        
  55.         this(DEFAULT_RANGE, DEFAULT_NUM_RANGE, (char)(rand.nextInt(25) + 97), (char)(rand.nextInt(25) + 97));
  56.        
  57.         if(!random){
  58.             System.err.println("Please use default constructor Equation()");
  59.             setCharX('x');
  60.             setCharY('y');
  61.         }
  62.        
  63.     }
  64.    
  65.     public Equation(){
  66.        
  67.         this(DEFAULT_RANGE, DEFAULT_NUM_RANGE, 'x', 'y');
  68.     }
  69.    
  70.     //-----------------------------------------------------[Private Methods]
  71.    
  72.     private void createEquation() {
  73.        
  74.         switch(this.expression){
  75.         case '+':
  76.             this.answer = (num1 * x) + (num2 * y);
  77.             break;
  78.         case '-':
  79.             this.answer =  (num1 * x) - (num2 * y);
  80.             break;
  81.         case '*':
  82.             this.answer = (num1 * x) * (num2 * y);
  83.             break;
  84.         case '/':
  85.             this.answer = (float) (num1 * x) / (num2 * y);
  86.         }
  87.     }
  88.    
  89.     //-----------------------------------------------------[Getters & Setters]
  90.  
  91.     /**
  92.      * @return the x
  93.      */
  94.     public int getX() {
  95.         return this.x;
  96.     }
  97.  
  98.     /**
  99.      * @param x the x to set
  100.      */
  101.     private void setX(int x) {
  102.         this.x = x;
  103.     }
  104.  
  105.     /**
  106.      * @return the y
  107.      */
  108.     public int getY() {
  109.         return this.y;
  110.     }
  111.  
  112.     /**
  113.      * @param y the y to set
  114.      */
  115.     private void setY(int y) {
  116.         this.y = y;
  117.     }
  118.  
  119.     /**
  120.      * @return the expression
  121.      */
  122.     public char getExpression() {
  123.         return this.expression;
  124.     }
  125.  
  126.     /**
  127.      * @param num between 0 and 3 to generate an expression for the equation
  128.      */
  129.     private void setExpression(int num) {
  130.        
  131.         switch(num){
  132.        
  133.         case ADDITION:
  134.             this.expression = '+';
  135.             break;
  136.         case SUBTRACTION:
  137.             this.expression = '-';
  138.             break;
  139.         case MULTIPLICATION:
  140.             this.expression = '*';
  141.             break;
  142.         case DIVISION:
  143.             this.expression = '/';
  144.             break;
  145.         default:
  146.             System.err.println("Not valid int to generate Expression");;
  147.         }
  148.     }
  149.    
  150.     /**
  151.      * @param exp to set the expression
  152.      */
  153.     private void setExpression(char exp) {
  154.        
  155.         if(exp == '+' || exp == '-' || exp == '*' || exp == '/'){
  156.             this.expression = exp;
  157.         } else {
  158.             System.err.println("Expression is invalid or not yet implemented.");
  159.         }
  160.     }
  161.    
  162.     /**
  163.      * @return the charX
  164.      */
  165.     public char getCharX() {
  166.         return this.charX;
  167.     }
  168.  
  169.     /**
  170.      * @param charX the charX to set
  171.      */
  172.     public void setCharX(char charX) {
  173.         this.charX = charX;
  174.     }
  175.  
  176.     /**
  177.      * @return the charY
  178.      */
  179.     public char getCharY() {
  180.         return this.charY;
  181.     }
  182.  
  183.     /**
  184.      * @param charY the charY to set
  185.      */
  186.     public void setCharY(char charY) {
  187.         this.charY = charY;
  188.     }
  189.  
  190.  
  191.     /**
  192.      * @return the num1
  193.      */
  194.     public int getNum1() {
  195.         return this.num1;
  196.     }
  197.  
  198.  
  199.     /**
  200.      * @param num1 the num1 to set
  201.      */
  202.     public void setNum1(int num1) {
  203.         this.num1 = num1;
  204.     }
  205.  
  206.  
  207.     /**
  208.      * @return the num2
  209.      */
  210.     public int getNum2() {
  211.         return this.num2;
  212.     }
  213.  
  214.  
  215.     /**
  216.      * @param num2 the num2 to set
  217.      */
  218.     public void setNum2(int num2) {
  219.         this.num2 = num2;
  220.     }
  221.    
  222.     public int getRange(){
  223.         return this.range[0];
  224.     }
  225.    
  226.     public int getNumRange(){
  227.         return this.range[1];
  228.     }
  229.    
  230.     @Override
  231.     public String toString(){
  232.        
  233.         return (Integer.toString(this.num1) + this.charX
  234.                 + ' ' + this.expression + ' '
  235.                 + Integer.toString(this.num2) + this.charY
  236.                 + " = " + this.answer);
  237.     }
  238.    
  239.     //-----------------------------------------------------[Test Driver]
  240.    
  241.     public static void main(String[] args){
  242.         Equation eq = new Equation();
  243.         System.out.println( "Num1: " + eq.getNum1()
  244.                         +   "\nx: " + eq.getX()
  245.                         +   "\nExpression: " + eq.getExpression()
  246.                         +   "\nNum2: " + eq.getNum2()
  247.                         +   "\ny: " + eq.getY()
  248.                         +   "\nChar x: " + eq.getCharX()
  249.                         +   "\nChar y: " + eq.getCharY()
  250.                         +   "\nRange: " + eq.getRange()
  251.                         +   "\nNum Range: " + eq.getNumRange()
  252.                         );
  253.         System.out.println(eq);
  254.     }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement