Math.random calculations

Add, subtract, divide and multiply generated random numbers.

Page 1 of 1

14 Replies - 6164 Views - Last Post: 04 December 2010 - 05:48 AM Rate Topic: -----

#1 Tara200  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 85
  • Joined: 13-October 07

Math.random calculations

Posted 29 November 2010 - 10:52 AM

Hi all,
i have built a Simple calculator that calculates arithmetic expressions, it is working but it has to evaluate more than 2 numbers eg (5.0 + 8.1) * (2.0) i am not sure as to how to get it to do this.....not for want of trying (i have to say!:-)

There is obviously something i am not getting, any help will be greatly appreciated, i have a super class Arithmetic Expression and the 4 basic calculations as child classes (as per specification)and then CalcTest as a test class. All functions work as they should but in the CalcTest class i have to evaluate an expression like the one above ^

Please dont be put off by the amount of code it is a very simple calculator (just had to use abstract classes and inheritance)It only has 4 basic operations add, subtract, divide and multiply
Thankyou

please help if you can?


package calculator;

import java.lang.String;

/*
 * @author 
 */
public class CalcTest
{
  
  public static void main(String[] args)
  {
       Addition add = new Addition();
       double add1 = add.getAdd();
       double add2 = add.getAdd();
       add1 = (5.0);
       add2 = (8.1);
       double result = add1 + add2;
       System.out.println("(5.0) + (8.1) = " + result);
   
  
      
       Subtraction sub = new Subtraction();
       double sub1 = sub.getSub();
       double sub2 = sub.getSub();
       double sub3 = sub.getSub();
       double sub4 = sub.getSub();
       sub1 = (34.90);
       sub2 = (23.89);
       double result2 = sub1 - sub2;
       System.out.println("(34.90) - (23.89 )= " + result2);

       Multiplication mul = new Multiplication();
       double mul1 = mul.getMul();
       double mul2 = mul.getMul();
       mul1 = (278.0);
       mul2 = 2.0;
       double result3= mul1 * mul2;
       System.out.println("(2.0) * 278.0 = " + result3);

       Division div = new Division();
       double div1 = div.getDiv();
       double div2 = div.getDiv();
       div1 = (345);
       div2 = (890);
       double result4= div1 / div2;
       System.out.println("345 / 890 = " + result4 );



       System.out.println( "(5.0 + 8.1) * (2.0) = " ); //Not sure as to how to
       //calculate more than one number?
   
   }

    private CalcTest()
    {
    }
    
}







package calculator;

/**
 *
 * @author 
 */
public abstract class ArithmeticExpression
{
    private double num1;
    private double num2;
    private double result;

   public abstract double evaluate();
   public abstract void display();

    /**
     * @return the num1
     */
    public double getNum1()
    {
        return num1;
    }

    /**
     * @param num1 the num1 to set
     */
    public void setNum1(double num1)
    {
        this.num1 = num1;
    }

    /**
     * @return the num2
     */
    public double getNum2()
    {
        return num2;
    }

    /**
     * @param num2 the num2 to set
     */
    public void setNum2(double num2)
    {
        this.num2 = num2;
    }

   
    /**
     * @return the result
     */
    public double getResult()
    {
        return result;
    }

    /**
     * @param result
     * @param result the result to set
     */
    public void setResult(double result)
    {
        this.result = result;
    }


    
    public double evaluate(double num1, double num2)
    {
        return getResult();
    }

   }






package calculator;

/**
 *
 * @author 
 */
public class Addition extends ArithmeticExpression
{
  private double add;

    /**
     * @return the add
     */
    public double getAdd()
    {
        return add;
    }

    /**
     * @param add the add to set
     */
    public void setAdd(double add)
    {
        this.add = add;
    }

   
    @Override

    public double evaluate (double num1, double num2)
    {
        this.setResult(num1 + num2);
        return getResult();
    }

   

    @Override
    public void display()
    {
      System.out.println( "("+ getNum1()+ "+ "+ getNum2() + ")" +"=" + getResult());
    }

    /**
     * 
     * @return
     */
    @Override
    public double evaluate()
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

  
   
    }

 



package calculator;

/**
 *
 * @author 
 */
public class Subtraction extends ArithmeticExpression
{
  private double sub;

  public double getSub()
    {
        return sub;
    }

    /**
     * @param sub 
     */
    public void setSub(double sub)
    {
        this.sub = sub;
    }

    /**
     *
     * @param num1
     * @param num2
     * @return
     */
    @Override
    public double evaluate(double num1, double num2)
    {
        this.setResult1D(num1 + num2);
        return getResult1D();
    }

    @Override
    public void display()
    {
    }

   @Override
    public double evaluate()
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private void setResult1D(double d) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private double getResult1D() {
        throw new UnsupportedOperationException("Not yet implemented");
    }



}




package calculator;

/**
 *
 * @author 
 */
public class Multiplication extends ArithmeticExpression
{
 private double Multiply;

    /**
     * @return the multiply
     */
    public double getMul()
    {
        return Multiply;
    }

    /**
     * @param Mul
     */
    public void setMul(double Mul)
    {
        this.Multiply = Mul;
    }

    @Override
    public double evaluate(double num1, double num2)
    {
        this.setResult1D(num1 + num2);
        return getResult1D();
    }

    @Override
    public void display()
    {
      
    }

    @Override
    public double evaluate()
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private void setResult1D(double d) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private double getResult1D() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
  
}




package calculator;

/**
 *
 * @author 
 */
public class Division extends ArithmeticExpression
{
 private double Division;

    /**
     * @return the division
     */
    public double getDiv()
    {
        return Division;
    }

    /**
     * @param Div
     */
    public void setDiv(double Div)
    {
        this.Division = Div;
    }

    @Override
    public double evaluate(double num1, double num2)
    {
       this.setResult(num1 + num2);
       return getResult();
    }

     @Override
    public void display()
    {
     System.out.println();
    }
   

    @Override
    public double evaluate()
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

   


This post has been edited by Tara200: 29 November 2010 - 04:19 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Math.random calculations

#2 Tara200  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 85
  • Joined: 13-October 07

Re: Math.random calculations

Posted 29 November 2010 - 05:03 PM

Come on guys....it can't be difficult code for all of you??especially the moderators??

I just need some help with it.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,262
  • Joined: 27-December 08

Re: Math.random calculations

Posted 29 November 2010 - 05:55 PM

You may have the classes for the operations, but you should use them. Use the setter methods to pass num1 and num2 as the operands.
Addition a = new Addition();
a.setNum1(50);
a.setNum2(60);



Also, in your ArithmeticExpression class, better practice would be to make the evaluate() method abstract, and override it in your subclasses. Addition should return num1 + num2;, etc. for each of the other operators. So no need for getter/setter methods for result, specifically the setter, b/c that would allow someone to say 1+2 = 35, which isn't true. That's part of limiting access to the outside world.

As for your class, the Addition object should receive 5 and 8.1 for num1 and num2. Then your Multiplication object sohuld receive additionObject.evaluate() and 2.0 for num1 and num2 respectively.
Was This Post Helpful? 1
  • +
  • -

#4 Tara200  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 85
  • Joined: 13-October 07

Re: Math.random calculations

Posted 30 November 2010 - 03:26 AM

View Postmacosxnerd101, on 29 November 2010 - 04:55 PM, said:

You may have the classes for the operations, but you should use them. Use the setter methods to pass num1 and num2 as the operands.
Addition a = new Addition();
a.setNum1(50);
a.setNum2(60);



Also, in your ArithmeticExpression class, better practice would be to make the evaluate() method abstract, and override it in your subclasses. Addition should return num1 + num2;, etc. for each of the other operators. So no need for getter/setter methods for result, specifically the setter, b/c that would allow someone to say 1+2 = 35, which isn't true. That's part of limiting access to the outside world.

As for your class, the Addition object should receive 5 and 8.1 for num1 and num2. Then your Multiplication object sohuld receive additionObject.evaluate() and 2.0 for num1 and num2 respectively.



Hi macosxnerd101,
firstly thankyou for responding it is appreciated.
A couple of things, the evaluate method is already abstract (check the super class) plus the reason i used the getter and setter for result is because i had to due to result having private access "private double result;"in Arithmetic Expression.

So otherwise i couldn't access it at all.Any suggestions?

As for your class, the Addition object should receive 5 and 8.1 for num1 and num2. Then your Multiplication object sohuld receive additionObject.evaluate() and 2.0 for num1 and num2 respectively.
Cool.....that is what i needed to know!
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,262
  • Joined: 27-December 08

Re: Math.random calculations

Posted 30 November 2010 - 07:23 AM

From your ArithmeticExpression class, evaluate is not abstract. It should be declared public abstract double evaluate();, as num1 and num2 are instance variables and evaluate() can access them using the getter methods when you implement it in the subclasses.
public abstract class ArithmeticExpression


    
    public double evaluate(double num1, double num2)
    {
        return getResult();
    }

   }


It doesn't make sense to have the setter for result. Just have evaluate return num1 and num2 after using the appropriate operation. No need for the result instance variable.

class Addition extends ArithmeticExpression{

    public double evaluate(){return getNum1() + getNum2(); } 
}


Was This Post Helpful? 0
  • +
  • -

#6 Tara200  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 85
  • Joined: 13-October 07

Re: Math.random calculations

Posted 30 November 2010 - 08:56 AM

View Postmacosxnerd101, on 30 November 2010 - 06:23 AM, said:

From your ArithmeticExpression class, evaluate is not abstract. It should be declared public abstract double evaluate();, as num1 and num2 are instance variables and evaluate() can access them using the getter methods when you implement it in the subclasses.
public abstract class ArithmeticExpression


    
    public double evaluate(double num1, double num2)
    {
        return getResult();
    }

   }


It doesn't make sense to have the setter for result. Just have evaluate return num1 and num2 after using the appropriate operation. No need for the result instance variable.

[code]
class Addition extends ArithmeticExpression{

public double evaluate(){return getNum1() + getNum2(); }
}
p/code]


Take another look at the abstract method "evalaute()" signature in the super class Arithmetic Expression....it is already abstract and exactly the same as your suggestion above.

I had already sorted the super class, i have gotten rid of the result setter method as you are right there i don't need them.

I also already have this code below!! :-)

class Addition extends ArithmeticExpression
{

public double evaluate()
{
return getNum1() + getNum2();
}
}

The assignment here IS to implement an abstract class and 2 abstract methods evaluate() and display()that is the spec.So the methods above have to be overidden in the concrete classes Addition etc.

I had no problem with any of that! I was just stuck on as to how to implement 3 or more numbers as an expression but i have sorted it now thankyou anyway simply:

System.out.println("(5.0 + 8.1) * (2.0) = " + (5.0 + 8.1) * (2.0));

I was looking for something far more complex than that i have to say but it works fine. :-)
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,262
  • Joined: 27-December 08

Re: Math.random calculations

Posted 30 November 2010 - 09:05 AM

Is the println() statement in your display() method? If not, I'm not sure it's what your instructor is asking for.

Glad I could help thusfar. :)
Was This Post Helpful? 0
  • +
  • -

#8 Tara200  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 85
  • Joined: 13-October 07

Re: Math.random calculations

Posted 30 November 2010 - 09:53 AM

View Postmacosxnerd101, on 30 November 2010 - 08:05 AM, said:

Is the println() statement in your display() method? If not, I'm not sure it's what your instructor is asking for.

Glad I could help thusfar. :)


Good point! I just have to add one more class afterwards that supplies the CalcTest with random numbers.
Was This Post Helpful? 0
  • +
  • -

#9 Tara200  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 85
  • Joined: 13-October 07

Re: Math.random calculations

Posted 02 December 2010 - 10:27 AM

Hi all,
i have a calculator program that has 4 basic arithmetic operations as inherited classes from a super class arithmetic expression. All works as it should. I have to implement an extra class RandomCalc.java which generates random numbers (which it is doing)

My problem is the generated numbers must be used with the Addition,Subtraction,Division and Multiplication classes eg: i have to somehow call these classes (in the CalcTest.java main program)and use them to calculate the generated random number expressions? The program does already calculate Arithmetic expressions and has to do the same with the random numbers?
Eg:
random number * random number + random number
random number + random number - random number

The 4 basic arithmetic classes are the same (except for their operations)so have just included the Addition.java


package calculator;

/**
 *
 * @author 
 */
public abstract class ArithmeticExpression
{
    private double num1;
    private double num2;
    private double result;

   public abstract double evaluate();
   public abstract void display();

    /**
     * @return the num1
     */
    public double getNum1()
    {
        return num1;
    }

    /**
     * @param num1 the num1 to set
     */
    public void setNum1(double num1)
    {
        this.num1 = num1;
    }

    /**
     * @return the num2
     */
    public double getNum2()
    {
        return num2;
    }

    /**
     * @param num2 the num2 to set
     */
    public void setNum2(double num2)
    {
        this.num2 = num2;
    }

   
    /**
     * @return the result
     */
    public double getResult()
    {
        return result;
    }


    
    public double evaluate(double num1, double num2)
    {
        return getResult();
    }

       
 
}

[b]CalcTest.java[/b]

import java.util.Random;
import java.util.*;
import java.awt.*;
/*
 * @author 
 */
public class CalcTest
{
  
  public static void main(String[] args)
  {
       // Testing the Addition Class
       Addition add = new Addition();
       double add1 = add.getAdd();
       double add2 = add.getAdd();
       add1 = (5.0);
       add2 = (8.1);
       double result = add1 + add2;
       System.out.println("(5.0) + (8.1) = " + result);
       

       // Testing the Subtraction Class
       Subtraction sub = new Subtraction();
       double sub1 = sub.getSub();
       double sub2 = sub.getSub();
       sub1 = (34.90);
       sub2 = (23.89);
       double result2 = sub1 - sub2;
       System.out.println("(34.90) - (23.89 )= " + result2);


       // Testing the Multiplication Class
       Multiplication mul = new Multiplication();
       double mul1 = mul.getMul();
       double mul2 = mul.getMul();
       mul1 = (278.0);
       mul2 = 2.0;
       double result3= mul1 * mul2;
       System.out.println("(2.0) * 278.0 = " + result3);


       // Testing the division Class
       Division div = new Division();
       double div1 = div.getDiv();
       double div2 = div.getDiv();
       div1 = (345);
       div2 = (8.0);
       double result4= div1 / div2;
       System.out.println("345 / 8.0 = " + result4 );


       // Testing multiple expressions
       System.out.println("(5.0 + 8.1) * (2.0) = " + (5.0 + 8.1) * (2.0));
       System.out.println("(34.90)-(23.89) * (2.0) = " + (34.90 - 23.89) * (2.0));
       System.out.println("(345 / 8.0) * (2.0) = " + 345 / 8.0 * (2.0));
       System.out.println("(2.0) * (278.0 + 8.1) = " + (2.0) * (278.0 + 8.1));

       RandomCalc rc = new RandomCalc(); //Generates random numbers
       rc.nextDouble();
       for(int i=0; i < 10 ; i++)
      {
        System.out.println("Random Number ["+ (i+1) + "] : " +   (Math.random()*10));
      }
   }


}

[b]RandomCalc.java[/b]

public class RandomCalc extends ArithmeticExpression
{
     
    public static double random()
    {
     for(int i=0; i < 10 ; i++)
      {
        System.out.println("Random Number ["+ (i+1) + "] : " + (Math.random()*10));
       }
      return random();
    }

    @Override
    public double evaluate()
    {
        return getNum1() + getNum2();
    }

    @Override
    public void display()
    {
       System.out.println();
    }

    void nextDouble()
    {
       
    }

    void display(String string)
    {
       System.out.println();
    }


[b]Addition.java[/b]

public class Addition extends ArithmeticExpression
{
  private double add;

    /**
     * @return the add
     */
    public double getAdd()
    {
        return getNum1() + getNum2();
    }

    /**
     * @param add the add to set
     */
    public void setAdd(double add)
    {
        this.add = add;
    }

   
    @Override

    public double evaluate (double num1, double num2)
    {
        return getResult();
    }

   

    @Override
    public void display()
    {
      System.out.println();
    }

    /**
     * 
     * @return
     */
    @Override
    public double evaluate()
    {
       return getNum1() + getNum2();
    }

   }

 

This post has been edited by Tara200: 02 December 2010 - 10:30 AM

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,262
  • Joined: 27-December 08

Re: Math.random calculations

Posted 02 December 2010 - 10:06 PM

As I explained in your last thread, you should be invoking the evaluate() method for your ArithmeticExpression objects rather than evaluating them manually, as you are currently doing.
Addition i = new Addition();
i.setNum1(Math.random()*100);
i.setNum1(Math.random()*100);
double result = i.evaluate();


Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,905
  • Joined: 06-March 08

Re: Math.random calculations

Posted 02 December 2010 - 10:42 PM

Duplicated threads merged.
Avoid duplicated posts.
Was This Post Helpful? 1
  • +
  • -

#12 Tara200  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 85
  • Joined: 13-October 07

Re: Math.random calculations

Posted 03 December 2010 - 02:07 PM

View Postmacosxnerd101, on 02 December 2010 - 09:06 PM, said:

As I explained in your last thread, you should be invoking the evaluate() method for your ArithmeticExpression objects rather than evaluating them manually, as you are currently doing.

Addition i = new Addition();
i.setNum1(Math.random()*100); What is setNum1 actually doing at this point?
i.setNum1(Math.random()*100);
double result = i.evaluate(); 



I put this in a seperate thread because it was just one specific question i was asking, NOT to do with anything else in my original program!

The above code does not do anything? I already have the math.random calculation, what i needed to know is how to "evaluate" the random numbers generated using add, subtract classes etc.

(Yes i know i have called the evaluate method in CalcTest for each class, i had intended to clean up the code when i had finished the difficult stuff! :-)
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,262
  • Joined: 27-December 08

Re: Math.random calculations

Posted 03 December 2010 - 04:00 PM

I explained how to do that in the last thread, that's why I linked back to it. :)
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,905
  • Joined: 06-March 08

Re: Math.random calculations

Posted 03 December 2010 - 08:22 PM

View PostTara200, on 03 December 2010 - 03:07 PM, said:

View Postmacosxnerd101, on 02 December 2010 - 09:06 PM, said:

As I explained in your last thread, you should be invoking the evaluate() method for your ArithmeticExpression objects rather than evaluating them manually, as you are currently doing.

Addition i = new Addition();
i.setNum1(Math.random()*100); What is setNum1 actually doing at this point?
i.setNum1(Math.random()*100);
double result = i.evaluate(); 



I put this in a seperate thread because it was just one specific question i was asking, NOT to do with anything else in my original program!

The above code does not do anything? I already have the math.random calculation, what i needed to know is how to "evaluate" the random numbers generated using add, subtract classes etc.

(Yes i know i have called the evaluate method in CalcTest for each class, i had intended to clean up the code when i had finished the difficult stuff! :-)

Yes but you posted exactly the same code, no need to pass through all it again
Was This Post Helpful? 0
  • +
  • -

#15 Tara200  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 85
  • Joined: 13-October 07

Re: Math.random calculations

Posted 04 December 2010 - 05:48 AM

Right, ok my apologies :-)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1