I built a simple GUI calculator in JAVA-FX. Any scope for improvement in my code? via /r/learnprogramming


I built a simple GUI calculator in JAVA-FX. Any scope for improvement in my code?

http://ift.tt/2tbAv6h

The code is hosted in this repo. I wrote this around 3 months ago. I did this just as I was learning git, so the commit's etc might be messy. I also was not aware of the existance of polish or reverse polish notation at that time, so the implementation of the actual calculation is very messy.

What I do here is define an abstract object called CalculatableObject. Then the classes parenthesis (spelt wrong in my code, too late fml), operator and number derive from the CalculatableObject. Then I defined a wrapper class for a vector of CalculatableObjects, which was to store the actual string which would be parsed to return output. Like:

5+3-2 

would be stored in the vector as

Number(5), Operator(+), Number(3), Operator(-), Number(2) 

where Number(5) represents a Number(this is not the lang.Number class, this is my own which derives from the CalculatableObject class) with the member variable value=5

Then I defined methods to introduce paranthesis at appropriate points in the vector, like

5 * 2 - 3 

would be parsed to

( (5 * 2) - 3 ) 

I followed DMAS for the insertion of paranthesis.

My paranthesis class had a member variable called ParaLevel to keep track of the nested level of each paranthesis.

Then I defined a method to calculate the output of the vector, by simply running a loop which calculates the value of the things inside the innermost paranthesis, like

( (5 * 2) -3 ) 

would be reduced to

( 10 -3 ) 

which would be reduced to

( 7 ) 

When there is just one level of paranthesis left, then the loop ends and the value between the paranthesis is returned.

The GUI employs these methods. Each button press calls the push_back()function on the CalculatableObjectVector and inserts the appropriate CalculatableObject into the vector. The output of the vector is calculated and shown on the display.

My code surely has a lot of bugs, it would be appreciated if you guys report them. I hadn't learned unit testing when I wrote this, so there will be numerous bugs.

Please make suggestions to improve my code wherever possible!

Thank you for reading through all of this!

Submitted July 14, 2017 at 03:51PM by rusty_ballsack_42
via reddit http://ift.tt/2tUtmo8

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s