ISTE-120 Lab 03: Calculators Exercise 1: The Calculator Class The first part of
ID: 3882241 • Letter: I
Question
ISTE-120 Lab 03: Calculators Exercise 1: The Calculator Class The first part of this lab is to write a class that simulates a calculator. Our calculator will be pretty simple. It will do addition, subtraction, multiplication, and division Calculators work with a single storage cell, called the accumulator, that keeps track of the current value in the calculator. This accumulator will be (the only) attribute of this class. It will be private (of course) and its type will be double. A double value in Java is one which stores a number (as opposed, for instance, a String). Further, a double can store a number with a fractional component (i.e. with digits to the right of the decimal point) Our only constructor will be the default constructor (the one without any parameters inside the parentheses). It will initialize the accumulator to zero The methods will be An accessor (getAccumulator) A method to add a number to the accumulator A method to subtract a number from the accumulator A method to multiply the accumulator by a number A method to divide a number into the accumulator . . . A method to clear the calculator (i.e. set the accumulator to zero) A toString method that returns the accumulator as a String . The division method will refuse to divide 0 into the accumulator. If the user tries to do this, it will print the error message It will NOT change the accumulator in this case Consider this simple test class to understand how the calculator can be made to work. public class TestCalcl [ ATTEMPT TO DIVIDE BY ZERO - IGNORE public static void main (String[] args) Calculator myCalc = new Calculator(); double myWeight = 145; // instead of 145 use your weight in pounds myCalc.add (myWeight) myCalc.multiply (0.453952) System.out .print 1n("Hy weight is " + myWeight + "lb = " + /I convert weight to KG myCalc + "kg")Explanation / Answer
As per Chegg policy we are supposed to answer only the 1st complete question. So request you to post the exercise 2 and 3 as separate questions.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Classes for Exercise 1
Calculator.java
===============
public class Calculator {
private double accumulator;
public Calculator()
{
accumulator = 0;
}
public double getAccumulator()
{
return accumulator;
}
public void add(double val)
{
accumulator += val;
}
public void subtract(double val)
{
accumulator -= val;
}
public void multiply(double val)
{
accumulator *= val;
}
public void divide(double val)
{
if(val == 0)
System.out.println("ATTEMPT TO DIVIDE BY ZERO - IGNORED");
else
accumulator /= val;
}
public void clear()
{
accumulator= 0;
}
public String toString()
{
return accumulator +"";
}
}
TestCalc1.java
============
public class TestCalc1 {
public static void main(String[] args) {
Calculator myCalc = new Calculator();
double myWeight = 145; //weight in pounds
myCalc.add(myWeight);
myCalc.multiply(0.453952); //convert weight to kg
System.out.println("My weight is " + myWeight + " lb = " + myCalc + " kg");
System.out.println("Trying division by 0");
myCalc.divide(0);
System.out.println("Clearing the calculator");
myCalc.clear();
System.out.println("Trying to calculate pi = 22/7");
myCalc.add(22);
myCalc.divide(7);
System.out.println("The value of pi is " + myCalc);
System.out.println("Calculating number of people per square mile in NYS");
long population = 19849399;
double area = 54555;
myCalc.clear();
myCalc.add(population);
myCalc.divide(area);
System.out.println("Total Population of NYS: " + population);
System.out.println("Area of NYS: " + area + " sq mi");
System.out.println("No. of people per square mile: " + myCalc);
}
}
output
=====
My weight is 145.0 lb = 65.82304 kg
Trying division by 0
ATTEMPT TO DIVIDE BY ZERO - IGNORED
Clearing the calculator
Trying to calculate pi = 22/7
The value of pi is 3.142857142857143
Calculating number of people per square mile in NYS
Total Population of NYS: 19849399
Area of NYS: 54555.0 sq mi
No. of people per square mile: 363.84197598753553
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.