Results 1 to 3 of 3
Thread: Char as an arithmetic operator
- 01-27-2009, 02:01 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 11
- Rep Power
- 0
-
Not the way you're trying to use it. It's possible to map a Character to a binary method and use that method to do the calculation you desire.
It won't be nearly as clean looking. If you want operator overload, you'll need to learn C++.
For example:
Java Code:import java.util.HashMap; import java.util.Map; public class TestBinaryMethod { public static Map<Character, BinaryMethod> methodMap = new HashMap<Character, BinaryMethod>(); public static void fillMap() { methodMap.put('+', new BinaryMethod() { public double execute(double d1, double d2) { return d1 + d2; } }); methodMap.put('-', new BinaryMethod() { public double execute(double d1, double d2) { return d1 - d2; } }); } public static void main(String[] args) { fillMap(); System.out.println(methodMap.get('+').execute(100, 40)); System.out.println(methodMap.get('-').execute(100, 40)); } } interface BinaryMethod { public double execute(double d1, double d2); }
Last edited by Fubarable; 01-27-2009 at 02:13 AM.
- 01-27-2009, 04:21 PM #3
Similar Threads
-
drawing char by char with Graphics
By diggitydoggz in forum New To JavaReplies: 5Last Post: 12-27-2008, 12:49 PM -
Arithmetic Stacks
By unc123w in forum New To JavaReplies: 22Last Post: 10-21-2008, 08:24 PM -
Illegal Arithmetic Operations?
By Cruor in forum New To JavaReplies: 13Last Post: 09-19-2008, 04:46 PM -
How to convert infix arithmetic expressions to postfix
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:36 PM -
How to parse postfix arithmetic expressions
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:36 PM
Bookmarks