5 Replies - 10633 Views - Last Post: 14 November 2010 - 04:01 PM Rate Topic: -----

#1 Mohsin-110  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-November 10

Simple Binary Calculator in Java (Abstract class)

Posted 14 November 2010 - 12:15 PM

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 {

Is This A Good Question/Topic? 0
  • +

Replies To: Simple Binary Calculator in Java (Abstract class)

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

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--:code: :)
Was This Post Helpful? 0
  • +
  • -

#3 Mohsin-110  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-November 10

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 { 

Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

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:
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!!!
}

Was This Post Helpful? 0
  • +
  • -

#5 Mohsin-110  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-November 10

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?
Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1