Writing code for a simple calculator using objects


#1

Java Language: Using Objects
Assignment I: Simple Calculator
In this assignment we shall look at a possible representation of a calculator in Java using
objects. We shall explore a way of representing a simple calculator using Java objects. We
shall restrict ourselves to operations that require two operands. Thus, for each calculator
object, we shall represent/store the first and second operands as floating point numbers.
We shall also require to represent/store the result as a floating point number.
We shall declare a new type, simpleCalc, to represent our calculator. We can now define
operations on data of this kind. In particular we need to implement some arithmetic
operations. We shall limit ourselves to the operations of addition, multiplication, and
exponentiation. (Note that subtraction is really addition in disguise and is no more
complicated. The same can be said for division vis a vis multiplication).
Your first task will be to implement the following constructors and methods:
simpleCalc();
simpleCalc(double op1, double op2);
double add();
double add(double op1, double op2);
double mult();
double mult(double op1, double op2);
double exp();
double exp(double op1, double op2);
void show();
void show(int dp);
The methods should do the following: The constructors, simpleCalc, should initialize
the calculator with the operands (if provided – otherwise, it initializes to 0 and 1
respectively); add should perform the addition operation on the operands (provided), store
and return the result; mult should perform the multiplication operation on the operands
(provided), store and return the result; exp should perform the operation of exponentiation
on the operands (op1op2), store and also return the result; show should print out the current
result in the calculator correct to the given number of decimal places (default: 2dp).
The next part of the assignment requires you to compute a close approximation to the
mathematical constant π, which is often approximated to 3.14…
In early history, π was approximated to 25/8 (about 0.5% below the exact value), later on
339/108 (correct to 2dp when rounded, or 0.09% below the exact value) later on between
223/71 and 22/7 (0.02% and 0.04% off respectively), later on 377/120 (correct to the
equivalent of 3dp), and later on between 3.141024 and 3.142708 the average of which gives
an error of less than 0.01%. Contemporary mathematicians noted the possibility of
concluding that π was an irrational number.
In the middle ages, Indian mathematician and astronomer Madhava of Sangamagrama,
discovered the infinite series for π, known as the Madhava-Leibniz series. We shall use
this series to approximate π:
􀟨 􀵌 √12 􀵬1 􀵆
1
3 ∙ 3
􀵅
1
5 ∙ 3􀬶 􀵆
1
7 ∙ 3􀬷 􀵅 ⋯ 􀵰
Compute this series as far as it will go without giving you overflow problems. In short,
implement a method:
double pi();
This method should compute and return π using the arithmetic methods you wrote above.
I have written for you a demo class, simpleCalcDemo, which you can use to test your
calculator. The main() method should work without any modifications. Try not to create
more bugs than you already have to deal with. As you may notice this code will not compile
until the simpleCalc class is defined and its methods implemented. You may choose to
define your simpleCalc class in this same file but it is smarter to create another file,
simpleClass.java for it.
Challenge for the bored:
In the 20th century (1910) the Indian mathematician Srinivasa Ramanujan found several
rapidly converging infinite series of π, including
1
􀟨
􀵌
2√2
9801
􀷍
􁈺4􀝇􁈻! 􁈺1103 􀵅 26390􀝇􁈻
􁈺􀝇!􁈻􀬸396􀬸􀯞
􀮶
􀯞􀭀􀬴
which computes a further eight decimal places of π with each term in the series. Modify
your π method to instead use the series above.
This assignment is due 2 weeks from the 5th of March, 2015.


#2

hi @stephen. Is this post part of a course you are running? We have a discourse forum specifically for courses here: http://discourse.p2pu.org.

If you want to improve the formatting of the code, have a look at Markdown syntax, the code can look like this if formatted correctly

simpleCalc();
simpleCalc(double op1, double op2);
double add();
double add(double op1, double op2);
double mult();
double mult(double op1, double op2);
double exp();
double exp(double op1, double op2);
void show();
void show(int dp);

#3

okay so how do i write the full code


#4