Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6697177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:25:44+00:00 2026-05-26T06:25:44+00:00

I’am doing homework. Firstly, there was a task to make calculator, which can evaluate

  • 0

I’am doing homework.

Firstly, there was a task to make calculator, which can evaluate such expressions: 5*(2+1).

Now, I have new task. Depending on the input parameter, the expression must be calculated in different types (Integer, Double, Long, Float). And I should use generics.

The problem is that I can’t understand how to implement this in program’s structure. Now, I’ll try to describe it in brief.

Class Token {char kind, double value} – contains token.

Class TokenStream – splits the expression into tokens.

Class Parser – builds a parse tree, and saves tokens in Reverse Polish Notation

Class Evaluator – evaluates RPN

Class Calc – contains main function

Teacher advised to use:

interface Operation<E> {
    E parse(String)
    E add(E e1, E e2)
    ...
}
IntOperation implements Operation<Integer> {...}

I don’t understand how to do this and what did he mean. Can you advice something?

PS: Sorry for my english:)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:25:46+00:00Added an answer on May 26, 2026 at 6:25 am

    Your main problem is that you are using primitive type like int, long, double etc. insted of Objects. When you are working with Generics firs rule to remember is that generics work only on Object type. In this case would be Integer, Long, Double etc.

    Important information four your case is that, all numeric Object in Java should extend the Number class. When we know this a picture of solution is start to came up.

    public abstract interface Operation<T extends Number> {
       T parse(String expression);
       T add(T number1, T number2)
    
    }
    

    Then a class that will implement this interface, would handle all numeric types.

    For example for Long.class the implementation goes like this:

    public final OperationForLong implements Operation<Long> {
    
          @Override
          Long parse(String expression) {
    
           // code omitted 
        }
    
        @Override
        Long add(Long number1, Long number2) {
           // code omitted 
    
        }
    
    }
    

    PS. I have used your interface design. But i think that the logic of parse and calculation should be separated. This mean that instead having one interface that perform parse operation and also calculate the result. You should have two interfaces one for parse and second for calculation. For example you could in the future deliver only numbers, without the string with expression.

    So the idea of design:

    public abstract interface MathExpressionParser {
    
          public Calculation parse(String expression);
    
    }
    
    public abstract interface Calc<T extends Number> { 
    
       public abstract T add(T num1, T num2);
       public abstract T substract(T num1, T num2);
       public abstract T multiple(T num1, T num2);
       public abstract T divide(T num1, T num2);
    
    }
    

    And the example implementation:

     public RPNCalculation implements MathExpressionParser {
    
          public Calculation parse(String expression) {
    
                // do to parse 
           }
    
    }
    
    
    public SimpleCalc implements Calc<Number> {
    
        public Number add(Number num1,Number num2) {
    
             if(num1 == null || num2 == null) {
    
                throw new InvalidArgumentExpression("Method Calc.add operate only on non null valuers.");
             }
    
          return Double.parse(num1.doubleValue() + num2.doubleValue());
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a text area in my form which accepts all possible characters from
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.