Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Interface and Extends problem

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Thanked 0 Times in 0 Posts

    Default Interface and Extends problem

    Hi, my problem is more theoretical, I have a program for arithmetic operations and the input could look like this

    Node expression = new Plus(new UnaryMinus(new Value(1)), new Multiply(new Value(2), new Value(3)));
    // -1 + (2 * 3)

    and I could use a little help with designing structure of this... I have Interface for Print of this expression and for calculation, now I would like to use inheritance for those operands ( + , - , / , * ). How to make it most easier? The path I chosen isn't the best I think - I made a class "operator" like this

    public class operator implements Node {
        int a,b,outcome;
        public operator(Value a, Value b) {
            this.a = a.getValue();
            this.b = b.getValue();
        public operator(Value a, plus plusExp) {
            this.a = a.getValue();
            this.b = plusExp.outcome;

    public class plus extends operator implements Node
        public plus(Value a, Value b) {
            super(a, b);
            System.out.println("2x Value of " + a.getValue() + ", " + b.getValue());//just print
       public plus(Value a, plus plusExp) {
            super(a, plusExp);
      public void calculate() {
            outcome = a + b;

    public class Value {
        private int value;
        Value(int i) {
            this.value = i;
        public int getValue() {
            return value;
    public class unaryMinus extends Value{
        public unaryMinus(int a) {

    public interface Node
        void calculate();
        void printExp();

    of course this is only for Plus and for minus, etc. it would be quite long... could you please give me a hint how to make a structure of those operands ( plus, minus,...) and of class Operand ( or is this class even important? ) so I wouldn't have to make all possible combination of operands but only something like this..

    (Value a, Value b);
    (Value a, Operand b);
    (Operand a, Operand b);
    (Operand a, Value b);


    BTW I know that the code isn't complete - I just separated pieces of code so you could take a picture of what I have.
    Last edited by DannyGT; April 2nd, 2010 at 06:10 AM.

  2. #2

    Default Re: Interface and Extends problem

    You should consider implementing the Composite/Component Pattern and Visitor Pattern.

    The Composite/Component pattern lets you define a hierarchical tree structure that you can use for the operators and the Visitor pattern will allow you to visit each of the locations, letting you visually display the equation.

    Composite Pattern - Composite pattern - Wikipedia, the free encyclopedia
    Visitor Pattern - Visitor pattern - Wikipedia, the free encyclopedia
    Kenneth Walter
    Software Developer

Similar Threads

  1. interface
    By nasi in forum Object Oriented Programming
    Replies: 5
    Last Post: September 2nd, 2010, 10:36 PM
  2. Interface Implementation
    By Samyx in forum Object Oriented Programming
    Replies: 1
    Last Post: December 2nd, 2009, 03:46 AM
  3. keyword Extends
    By chronoz13 in forum Object Oriented Programming
    Replies: 3
    Last Post: November 27th, 2009, 07:30 AM
  4. Getting an error while altering a source code
    By marksquall in forum Collections and Generics
    Replies: 3
    Last Post: June 8th, 2009, 02:49 AM
  5. Problem while implementing a basic user interface menu
    By Rastabot in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 3rd, 2009, 04:38 PM