Ads

# Java Calculator Program

Hi, so I need to make a program that "works like a calculator". I need to make two versions:

1) I'm given the Expression Class and need to implement the children classes, which are Number, Product, Sum, Quotient, Difference, and Modulus. The five types of binary operation expressions will each have two (sub) expressions, a left hand side and a right hand side (or in the case of a quotient a numerator and a denominator). Thus each binary operation class will have a constructor that takes two other Expression objects as arguments.

Numbers on the other hand will only store a single double. Thus their only constructor will take a double.

All expressions know how to answer two messages: the toString() method which will provide a String representation of the expression the object represents. On the other hand, the evaluateMe() method will cause the expression to compute its value and return it (as a double). There is only one twist to this, and that is the toString() of numbers should only print out to two decimal places (using a java.text.DecimalFormat object). To give you some idea of what is required, consider the following code:

Sum s1 = new Sum(new Number(7.123456789), new Number(11.3344)); Sum s2 = new Sum(s1, new Product(s1, new Number(3))); System.out.println("s2 = " + s2 + " with value " + s2.evaluateMe()); This should, when executed print out:

s2 = ((7.12 + 11.33) + ((7.12 + 11.33) * 3)) with value 73.831427156 The toString() method should hold no surprises really, except that in the Number version you'll have to format the return value a bit. But check out either the solutions to the first assignment or the description of the first project. The evaluateMe() method is also not very complex. In the Number case it should just return the stored value, while in the Sum case it will just ask its two subexpressions to evaluate themselves, and then return the resulting sum of those doubles.

2) The new intermediate class BinaryOperation does most/all of the work. Now define the classes Sum, Product, Quotient, Difference and Modulus as subclasses of BinaryOperation. They should be much much simpler.

The starter code for Expression is:

```public abstract class Expression {
public abstract String toString();
public abstract double evaluateMe();
}
```

The class BinaryOperation is:

```public abstract class BinaryOperation extends Expression{

private Expression lhs;
private Expression rhs;
private char binaryOperation;

public String toString(){
return "(" +
lhs.toString() +
" " +
binaryOperation +
" " +
rhs.toString()
+ ")";
};

public double evaluateMe(){
return myOperation(lhs.evaluateMe(),
rhs.evaluateMe());
};

protected abstract double myOperation(double d1, double d2);

BinaryOperation(Expression e1, Expression e2, char op){
lhs = e1;
rhs = e2;
binaryOperation = op;
}
}
```

THANKS IN ADVANCE!

View Answers

Ads

Related Tutorials/Questions & Answers:
Java Calculator Program
Java Calculator Program  Hi, so I need to make a program that "works like a calculator". I need to make two versions: 1) I'm given the Expression Class and need to implement the children classes, which are Number, Product, Sum
Calculator program in Java
Calculator program in Java is used by programmer to add, subtract, multiply... other wise it will be a fraction value) Example of Calculator program in Java... a class "calculator" is used. System.in takes the input from the system/user at run
Advertisements
thread program for calculator implementation
thread program for calculator implementation  Hi i'm prem i need calculator progrm in java that are implemented by Thread interface.....pls strong text
How to Write a Calculator Program in Java?
How to Write a Calculator Program in Java? In this Java Tutorial you will learn how to write a Calculator program in Java in easy steps. Calculator program... of Calculator program in Java package Tutorial; import java.io.*; class
simple calculator program in javascript
simple calculator program in javascript  strong textsimple calculator in javascript
How to write calculator in J2ME program?
How to write calculator in J2ME program?  How to write calculator in J2ME program
Calculator
Calculator  need a simple java program to degin a CALCULATOR without using ADVANCED JAVA....   Calculator in Java Swing
calculator in java with stack
calculator in java with stack  i want calcultor with interface in java and in interface there is button called postfix ,,, when the user enter opertions and numbers first check if is vaild or not then convert to postfix
java loan calculator applet help
java loan calculator applet help  Hi, I could use some help correcting a code here. I need to write a Java applet program (together with its html...); that java cannot find symbol and it points to where Loan is after new
Swimming Pool Calculator - Java Beginners
Swimming Pool Calculator  When I run the program the login window doesn't appear...Please help 1)LoginForm.java import javax.swing.*; import...()); mainFrame = new JFrame("Swimming Pool Volume Calculator"); calcButton = new
java program for
java program for   java program for printing documents,images and cards
a Java program
a Java program    Write a Java program to print even numbers from 2 to 1024? Write a Java program to print ? My Name is Mirza? 100 times? Write a Java program to print Fibonacci Series? Write a Java program to reverse a number
java program
java program  how to write an addition program in java without using arithematic operator
java program
java program  write java program for constructor,overriding,overriding,exception handling
java program
java program  write a java program to display array list and calculate the average of given array
java program
java program  write a java program to display array list and calculate the average of given array
Java Program
Java Program  java program to insert row in excel sheet after identifying an object
java program
java program  java program to implement the reflection of a particular class details like constructor,methods and fields with its modifiers
java program
java program  Write a java program to do matrix addition operation On two given matrices
java program
java program  Write a java program to find the number of Positive numbers in m* n matrix
java program
java program  Write a program to create an applet and display The message "welcome to java
java program
java program  hi friends how to make a java program for getting non prime odd numbers in a given series
java program
java program  Write a program to demonstrate the concept of various possible exceptions arising in a Java Program and the ways to handle them.  ... in Java
java program
java program  write a program to print 1234 567 89 10
Writing Calculator Program in Swing
Writing Calculator Program in Swing   ... shot. For developing a small calculator program in swing we need two different.... Calculator Code in Java Swing Please save the code as SwingCalculator.java
java program
java program  write a java program to compute area of a circle.square,rectangle.triangle,volume of a sphere ,cylinder and perimeter of cube using method over riding
java program
java program  write a java program to compute area of a circle.square,rectangle.triangle,volume of a sphere ,cylinder and perimeter of cube using method over riding
java program
java program  write a java program to read a file which hold email address validate email address tohave formate @.* and replace all .com email address
java program
java program  write a java program to create an array of size 10 by taking input from bufferreader and find out the average of array elements from that array
java program
java program  write a java program to create an array of size 10 by taking input from bufferreader and find out the average of array elements from that array
java program
java program  write a java program to create an array of size 10 by taking input from bufferreader and find out the average of array elements from that array

Ads
{"ft_lang_label":"__label__en","ft_lang_prob":0.782045,"math_prob":0.9021506,"size":7524,"snap":"2021-21-2021-25","text_gpt3_token_len":1660,"char_repetition_ratio":0.20492022,"word_repetition_ratio":0.23977087,"special_character_ratio":0.21570973,"punctuation_ratio":0.119456366,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9904808,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-17T02:19:07Z\",\"WARC-Record-ID\":\"<urn:uuid:29a9b123-4952-47c7-aa0c-631a01ae33d7>\",\"Content-Length\":\"39471\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a6192095-d880-43b8-8838-c8fd1852eec5>\",\"WARC-Concurrent-To\":\"<urn:uuid:be6585b9-35a0-41ea-b820-d3d277714011>\",\"WARC-IP-Address\":\"104.21.38.123\",\"WARC-Target-URI\":\"https://www.roseindia.net/answers/viewqa/Java-Beginners/27769-Java-Calculator-Program.html\",\"WARC-Payload-Digest\":\"sha1:4CYQ5OFN2GGJBUSE7JJ3JWJGSOLLB2H6\",\"WARC-Block-Digest\":\"sha1:4JSCMXLQN7WE3NTIWNWE3AMZRPAYZ7GU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623487626465.55_warc_CC-MAIN-20210617011001-20210617041001-00325.warc.gz\"}"}