58 lines
1.6 KiB
Java
58 lines
1.6 KiB
Java
/*
|
|
* Build class called Calculator that emulates basic functions of a calc
|
|
* add sub mult div and clear
|
|
* one private member field, double value, for the calcs current val
|
|
*
|
|
* public Calculator() - set field to 0.0
|
|
* public void add(double val) - add to field
|
|
* public void subtract(double val) - remove from field
|
|
* public void multiply(double val) - multiply field
|
|
* public void divide(double val) - divide field
|
|
* public void clear() - set to 0.0
|
|
* public double getValue() - return field
|
|
*
|
|
* Given two double input values num1 and num 2, outputs following values:
|
|
*
|
|
* 1. Inital value of field value
|
|
* 2. value after adding num1
|
|
* 3. value after mult by 3
|
|
* 4. value after sub num2
|
|
* 5. value after dividing by 2
|
|
* 6. value after calling clear
|
|
*
|
|
* sample:
|
|
* input: 10.0 5.0
|
|
* output:
|
|
* 0.0
|
|
* 10.0
|
|
* 30.0
|
|
* 25.0
|
|
* 12.5
|
|
* 0.0
|
|
*/
|
|
|
|
/* Comments
|
|
* It's interesting. A long long time ago, when I was still barely able
|
|
* to make QBASIC beep and take inkey$ I thought a calculator woul be
|
|
* the perfect project to begin programming, and it gave me grief
|
|
* though now, it's easy.
|
|
* To be fair though, I don't think I even had a grasp of functions at the time
|
|
*/
|
|
|
|
|
|
public class Calculator{
|
|
private double value;
|
|
|
|
public Calculator(){
|
|
this.value = 0;
|
|
}
|
|
|
|
public void add(double val){ this.value += val; }
|
|
public void subtract(double val){ this.value -=val; }
|
|
public void multiply(double val){ this.value *=val; }
|
|
public void divide(double val){ this.value /=val; }
|
|
public void clear(){ this.value = 0; }
|
|
public double getValue(){ return this.value; }
|
|
|
|
}
|