DEV Community

KevinSarav
KevinSarav

Posted on

Calculator Second Post

Today, I'll be discussing my work so far on the calculator project. From now on, I will try to talk about the challenges of my projects instead of also describing every detail. I will leave details of my projects as comments so my posts can be more brief.

I've fixed the issues I've noticed from my first post. My main dilemma with how I was doing my project was how to deal with decimal points. I've tried thinking of a way for me to handle decimal points when my inputted numbers are in double format, but I could not think of one. The main issue is when there is nothing in the decimal place. If there isn't anything, then I would have to put a 0 as a placeholder, but doing that affects how I add more numbers to the right of the decimal place. I can't omit the 0 because then, the double wouldn't make sense.

To solve the issue, I tried coming up with a bunch of boolean values that tell me when there's nothing to the right of the decimal and when there is something in the decimal. All of these extraneous values made it more confusing for me to tell what does what and how to make the project ultimately work. Besides the decimal place, I got everything else to work fine.

To solve this issue, I redid the project so that my input ArrayList now holds Strings like the operation ArrayList instead of doubles. I figured this would make more sense with how calculator programs work where they tack on numbers and decimals to the right and remove things to the left as if it was a String. For arithmetic operations, I kept making a variable that is the double representation of the String input. If this input ultimately ended in a decimal by the time you press an operation, my program will remove the decimal.

For the percent method, I found out what I was trying to do. I'm trying to model my project based off of the behavior of the Windows calculator. The Windows calculator will change a number to 0 if you're just typing the number by itself and trying to get the percent of it. The percent button only does something meaningful after an operation is first pressed. Then depending on the operation, if it's plus or minus, the percent will get the total so far and get the percent of that. For example, 10 + 20 + 50% would yield 10 + 20 + 15. If it's multiply or divide, the input will be turned to a percent representation. For example, 10 * 50% will turn to 10 * 0.5. I was able to do this a lot easier with a String ArrayList for my inputted numbers.

My delete operation was also made easier with a String ArrayList. I just had to use substring and include everything but the last value, regardless of if it's a decimal or number. When inputting a number, I just did += (String number) to add the number to the right. Same thing with decimals: I just add it to the end. For my calculate method, I found out that the two for loops was not necessary. I just used one for loop and got the values at the spots in the ArrayLists that I needed to do the operation.


The result of my project is that I am able to press number buttons without issue. However, I notice that when I press any arithmetic operation, an error message is thrown. I think it's related to me using .size() to get the size of the ArrayList. I believe I remember coming across this issue before in one of my undergrad classes when using .size(). I may have to go through every value with a for loop to get the length. I will revisit this on my next post. Also, I need to test to see if pressing the numbers actually stores them in the appropriate ArrayList. All I know so far is no error is thrown when pressing numbers.

Also, I wanted to discuss briefly a group project I'm doing for my software engineering class. My group is making an inventory management system. It will involve using SQL, Angular, Apache, HTML, CSS, and a whole host of other stuff to get to work. I'm very excited to partake in this project and learn new things. I will keep you guys updated. See you next time!

Top comments (0)