Hello people,
I am trying to implement a program in Java that simulates a simple calculator.
Here is the thing, I have to start by creating an abstract class that represents any binary which i'm thinking it has to accept only two arguments and then i have to implement various sub classes of this class. I know that my abstract class should at least include two methods but i am not quite sure what objects i should include in it! I also know that my subclasses should include the binary as well as operations of addition subtraction multiplication and division and i should use NO static methods. I have already started and come up with the following:
import java.util.Scanner;
public abstract class (class name)
{
double opt1;
double op2;
public void evaluate(double op1, double op2);
}
public void display();
}
public class Binarycalculator extends ArithmeticExpression {
5 Replies  9844 Views  Last Post: 14 November 2010  04:01 PM
#1
Simple Binary Calculator in Java (Abstract class)
Posted 14 November 2010  12:15 PM
Replies To: Simple Binary Calculator in Java (Abstract class)
#2
Re: Simple Binary Calculator in Java (Abstract class)
Posted 14 November 2010  12:23 PM
It appears that some of your code wasn't pasted into the message. I'm sure that would help us.
Also...Pleas remember
Also...Pleas remember
#3
Re: Simple Binary Calculator in Java (Abstract class)
Posted 14 November 2010  12:34 PM
import java.util.Scanner; public abstract class (class name) { double opt1; double op2; public void evaluate(double op1, double op2); } public void display(); } public class Binarycalculator extends ArithmeticExpression {
#4
Re: Simple Binary Calculator in Java (Abstract class)
Posted 14 November 2010  12:59 PM
You have a misplaced "}" in your code after the declaration on the evaluate method.
also, when not implementing a method you must declare it as abstract!
like so:
you will have to implement it in the derived class.
As for the assignment, is it a binary calculator? or decimal calculator? I am not sure what exacly you mean...
I'll guess you want to make a decimal number calculator, and by binary you mean it accepts two decimals.
you can start by declaring an abstract class called ArithmeticOperation. have 2 double variables, pass them to the constructor.
have the evaluate method declared as abstract, and override it in each of the subclasses (multiply, divide, substract add).
something like:
to declare a subclass of ArithmeticOperation, simple declare it as:
also, when not implementing a method you must declare it as abstract!
like so:
public abstract void print();
you will have to implement it in the derived class.
As for the assignment, is it a binary calculator? or decimal calculator? I am not sure what exacly you mean...
I'll guess you want to make a decimal number calculator, and by binary you mean it accepts two decimals.
you can start by declaring an abstract class called ArithmeticOperation. have 2 double variables, pass them to the constructor.
have the evaluate method declared as abstract, and override it in each of the subclasses (multiply, divide, substract add).
something like:
public abstract class ArithmeticOperation { private double var1; private double var2; public ArithmeticOperation(double var1, double var2){ this.var1 = var1; this.var2 = var2; } public abstract double evaluate(); //perform calculations here //any other methods? }
to declare a subclass of ArithmeticOperation, simple declare it as:
public SubClass extends ArithmeticOperation { //don't forget to override evaluate!!! }
#5
Re: Simple Binary Calculator in Java (Abstract class)
Posted 14 November 2010  03:02 PM
Thank you so much for your reply,
it is actually a binary calculator, each sub class should include the binary and the operations (Addition, subtraction, multiplication and division). Method evaluate should evaluate the arithmetic operation/expression and return it as a double. For example, calling it in an object representing expression "(5.0+8.1)*(2.0)" should return 26.2.
Whereas method display should print the raw unevaluated on the screen. For example,
calling it in an object representing expression "(5.0+8.1)*(2.0)" should display the string
"(5.0+8.1)*(2.0)" without the quotes. I have no idea how i should be doing that any clues?
it is actually a binary calculator, each sub class should include the binary and the operations (Addition, subtraction, multiplication and division). Method evaluate should evaluate the arithmetic operation/expression and return it as a double. For example, calling it in an object representing expression "(5.0+8.1)*(2.0)" should return 26.2.
Whereas method display should print the raw unevaluated on the screen. For example,
calling it in an object representing expression "(5.0+8.1)*(2.0)" should display the string
"(5.0+8.1)*(2.0)" without the quotes. I have no idea how i should be doing that any clues?
#6
Re: Simple Binary Calculator in Java (Abstract class)
Posted 14 November 2010  04:01 PM
Do you have to get the arithmetic expression as a String?
In that case the class and logic should be different.
First check if the input is valid. (a valid arithmetic expression).
then look for the operations char ('(' ,')', '+', '', '*', '\')
just calculate it in the correct order (first braces, then mult\div, and then plus,minus)
There are some code snippets similar to what you need.
check this one for example:
This will help you design your code:
http://www.dreaminco...snippet5781.htm
In that case the class and logic should be different.
First check if the input is valid. (a valid arithmetic expression).
then look for the operations char ('(' ,')', '+', '', '*', '\')
just calculate it in the correct order (first braces, then mult\div, and then plus,minus)
There are some code snippets similar to what you need.
check this one for example:
This will help you design your code:
http://www.dreaminco...snippet5781.htm
Page 1 of 1
