Home Answers Viewqa Java-Beginners Java Calculator Program

 
 


Regamus Regamus
Java Calculator Program
0 Answer(s)      8 months ago
Posted in : Java Beginners

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









Related Pages:
Calculator
Calculator  need a simple java program to degin a CALCULATOR without using ADVANCED JAVA....   Calculator in Java Swing
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
Program for Calculator - Swing AWT
Program for Calculator  write a program for calculator?  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/swing/calculator-in-swing.shtml Hope that it will be helpful
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
Java Calculator program - Java Beginners
Java Calculator program  import java.awt.*; import javax.swing.*; import java.awt.event.*; class Calculator implements ActionListener { int c,n...://www.roseindia.net/java/example/java/swing/calculator-in-swing.shtml Hope
Calculator class
Calculator class  I am a beginner in Eclipse. I have to do a program called calculator that adds numbers. This is my code so far: //Margaret //ICS... for this generated file go to * Window - Preferences - Java - Code Style - Code Templates
base calculator.. - Java Beginners
?? there's always an error (Error : Invalid path, "C:\Program Files\Java\jre1.6.0_01\bin\javac.exe" -classpath "C:\Program Files\Java\jdk1.6.0_01\bin" -d "C... to be set. In first path: change as C:\Program Files\Java\jdk1.6.0_01\bin
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
Simple Java Calculator - Java Beginners
Simple Java Calculator  Write a Java program to create simple Calculator for 4 basic Math operations, Addition, Subtraction, Multiplication and Division. The calculator should simulate the look of handheld calculator containing
simple calculator program in javascript
simple calculator program in javascript  strong textsimple calculator in javascript
Example - Simple Calculator
Java: Example - Simple Calculator Here is the source for the simple calculator shown at the left. It's divided into three source files. Main (Calc.java) - A simple main program. User interface (CalcGUI.java
Java: Example - Simple Calculator
Java: Example - Simple Calculator Here is the source for the simple calculator shown at the left. It's divided into three source files. Main (Calc.java) - A simple main program. User interface (CalcGUI.java
Swimming Pool Calculator - Java Beginners
Swimming Pool Calculator  I have to write a program to calculate... Calculator is one that is very simple to use. As you can see, it is a user... depth of a pool and the program will calculate the volume of that pool based
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 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
Swimming Pool Calculator - Java Beginners
Swimming Pool Calculator  Okay, so I tried making the program with this coding: import java.awt.*; import javax.swing.*; import java.awt.event.... Calculator", tempCalc ); jtabbedPane.addTab( "Customers", customers
How to write calculator in J2ME program?
How to write calculator in J2ME program?  How to write calculator in J2ME program
Simple Calculator Application In Java Script
Simple Calculator Application In Java Script  ... the basics of JavaScript and create your first JavaScript program. What is simple Calculator The objective of this project is  learn how to write a simple
base calculator.. - Java Beginners
base calculator..  Help, i need some help about a base calculator.. i don't know how to start
Graphical calculator using AWT - Java Beginners
calculator program."); class CalcGUI extends JFrame { private final Font...Graphical calculator using AWT  Hi Sir, Thanks for the reply...); this.pack(); this.setTitle("Simple Calculator"); this.setResizable(false
calculator - Java Interview Questions
calculator  create calculator by java code  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/swing/calculator-in-swing.shtml Thanks
simple calculator - Java Beginners
simple calculator  how can i create a simple calculator using java codes?  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/swing/calculator-in-swing.shtml Thanks
Scientific Calculator - Java Beginners
Scientific Calculator  Develop a scientific calculator using even-driven programming paradigm of Java.? Thanks in ADVANCE  Hi Friend, Please visit the following link: http://www.roseindia.net/tutorial/java
matrix calculator - Java Beginners
matrix calculator  hi..... can you help me in writing source code of matrix calculator in java... i know you are the best you can do it!!! show yourself
calculator - Java Server Faces Questions
calculator  Some ideas for the project of calculator
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
Calculator - JSP-Servlet
Calculator  Dear Deepak Sir, Calculator program is avilable in Jsp... calculator program in jsp function checkValue(){ var msg...; } Simple calculator program in jsp /> >
swimming pool calculator
swimming pool calculator  i'm writing a program to calculate the measurements of a swimming pool & a hot tub. and then the user can enter...", customers); jtabbedPane.addTab("Temp Calculator", tempCalc
Example - Calc Extensions
. The calculator example program (See Example - Calc Main) uses integers. Extend the calculator to use double floating-point numbers... Java: Example - Calc Extensions Example - Calc Main, Example - Calc GUI
Java Swing Scientific Calculator
Java Swing Scientific Calculator A Scientific Calculator is a very powerful and general purpose calculator. In addition to basic arithmetic functions... calculator using java swing. Here is the code: import java.awt.*; import
Writing Calculator Stateless Session Bean
Writing Calculator Stateless Session Bean...;String    COMP_NAME="java:comp/env/ejb/CalculatorBean";  ... for our Calculator Session Bean:   /* * 
Graphical calculator using AWT - Java Beginners
Graphical calculator using AWT  hi Sir, I need a source code for the following prgm...pls help me.. Implement a simple graphical calculator using AWT.The calculator shd perform simple operation like addition, subtraction
Java program - Applet
Java program  Can you please provide me a code for scientific calculator in java I need that in urgent.Please post me the answers  Hi Friend, Please visit the following link: http://www.roseindia.net/tutorial/java
Write a program in java...
Write a program in java...  Hi, friends Please, can you help me? Q1: Write a program in java to simulate a calculator. Your program should take two... to enter an integer number. Write a program in java to find the factorial
calculator midlet
calculator midlet  give me code calculator midlet in bluetooth application with j2me
How can i implement the calculator programe in jsp code
;calculator program in jsp</title> <script> function checkValue(){ var...; } } %> <center> <h2>Simple calculator program in jsp</h2> <...How can i implement the calculator programe in jsp code  Please send
program
program  write a program different between to dates in terms of days in java
program
program  WAP a java program to form 1/2+3/4+5/6+7/8 series
program
program  explanation of program on extending thread class   Hi Friend, Please go through the following link: Java Threads Thanks
program
program  write a java program to input a no. and print wheather the no. is a special no or not. (special no. are those no whose factorial of the sum of digit of the no is same as the original
program
is working in Java department and salary is 10000". Instantiate the Employee class
java program for
java program for   java program for printing documents,images and cards
PHP Tax Calculator - PHP
PHP Tax Calculator  In my project i required a tax calculator that can calculate the property tax
Java Program
Java Program  A Java Program that print the data on the printer but buttons not to be printed
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
rogram - Java Beginners
Java program for calculator  I need the calculator program in Java

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.