Calculator Progress Overview
Published: 2019-03-17 02:13:46

---
title: Calculator Progress Overview
published: true
description: Overview of the progress of my calculator program
tags: 
---

The project I will be discussing is the calculator project I briefly mentioned in my last post. Any projects I mentioned will be in my GitHub. Below is a direct link to the project if you need it. I will be discussing the project as is without making any adjustments since it was touched a few months ago. This discussion is just so I can talk through the project and discover what needs fixing before I can continue. With every post, you can feel free to scroll down until the last paragraph to see my summary of what I've learned from analyzing my project. This post will be longer than my future posts just because I'm discussing days of work into one project from the past and I'm trying to reason out my train of thought.

https://github.com/KevinSarav/Calculator


-----
So in this project, I used Java as the programming language. I extended JFrame and implemented ActionListener so I could make the GUI for my calculator. JFrame lets me use GUI functions to make a GUI and ActionListener will be used later to take inputs from the user. I made two double variables: one to record the inputted number and another to record the result of the arithmetic. I also made a boolean variable that looks like it's checking if the inputted number is a decimal number.

I made two List arrays, one of double and one of String. The double List is used to hold the inputted numbers. The input double variable will keep adding to itself whatever the user is inputting. Once an operation is pressed, that input value will be added to the double List. This operation is placed in the String List. This process of adding numbers to the double List and operations to the String List continues until an equal sign is pressed. 

Once the equal sign is pressed, the inputted numbers will be added/subtracted/multiplied/divided depending on what the next operation in the String List is. At first, the first two inputted numbers will undergo the operation and then the result will undergo the next operation with the next number until the very end.

So far, my GUI has buttons for percentages, square roots, squared, inverse, clear entry, clear, delete, divide, multiply, subtract, add, minus sign, period, and equal sign.

PERCENT
The percentage operation will make input equivalent to whatever the input is so far multiplied by 0.01, which then gets multiplied to the value at the end of the double List, for some reason. I might need to reconsider why I'm doing that if multiplying the input by 0.01 will get me the result I want.

SQUARE ROOT/SQUARED/INVERSE/CLEAR ENTRY/CLEAR ALL
The square root operation just uses a simple Math.sqrt(input). The squared operation multiplies the inputted number by itself. The inverse simply divides 1 by the input. The clear entry operation makes the inputted number equal 0. The clear all operation will make input equal 0 and both of my Lists will undergo .clear() operations.

DELETE
The delete operation will first check to see if my input is not a decimal. If it's not, it will subtract input by input modulus 10 (the rightmost number on the right of the inputted number). This will be divided by 10. This makes it so that we subtract the rightmost number and then move the decimal over to the left. For example, 53 will turn to 50 after subtracting the input modulus 10, and then the 50 will turn to 5 after dividing by 10. This effectively removes the 3 from my original 53.

If the inputted number is a number with a decimal, then that's where I do something different. It doesn't look like I completed my thought. The code is unfinished here. First, I make a String value that is equal to the String value of my inputted number. I then make a String array that looks like it splits the number in two: the first half to the left of the decimal will be added to the array and the second half to the right of the decimal will be added next. More specifically, the code I put was: array.toString().split("\\.");. I'm not sure why I had two backspaces there. I could've very easily just left a . in the quotation marks. I will have to rethink why I did that.

I then made an integer variable that is equivalent to all the numbers to the right of the decimal since that's where I would be deleting the number. I fee like I could've done this without making two different arrays, but it was probably just to make the code easier to understand. I then have a for loop that goes through every value to the right of the decimal place. For every iteration, I was trying to multiply by 0.1, but I'm not sure what and why. I did not complete that code. I left it at *= 0.1. I will have to revisit this thought. I wouldn't be able to just subtract the rightmost number because then I would be subtracting a whole number. I would need to have the the rightmost number multiplied by however many decimal places there are for this to work. Perhaps that's what the for loop was trying to do, but I could've just as easily done that just using the length of the array with all numbers to the right of the decimal place.

DIVIDE/MULTIPLY/SUBTRACT/ADD
When an operation is pressed, I added the inputted number so far to the double List and made the input 0. I added the operation pressed to the String List and made my boolean for decimals false to reset.

NUMBER
When a number is pressed, I made an integer variable that is equivalent to the number pressed. If the boolean for decimals is false (no decimal in number), then I multiplied the inputted number by 10 and then added the number that the user pressed to the result. For example, if we want to add 5 to 34, then multiplying 34 by 10 gets us 340 and adding 5 gets us 345. It's effectively adding 5 to the right end of 34.

If the boolean is true (there is a decimal in the number), then I make an array that again splits the number in two like in my delete operation. I have a for loop going through the array (every value to the right of the decimal) and then multiplying the chosen integer by 0.1 for each iteration. The result is then added to the inputted number. This makes sense. For example, say we want to add 8 to 56.3. One iteration would multiply 8 by 0.1, resulting in 0.8. We would want to go through one more iteration to get 0.08. This then gets added to 56.3, which results in 56.38. We've effectively added 8 to the end of 56.3. I would need to do this for my delete operation as well to deal with decimals.

MINUS SIGN/DECIMAL/EQUAL
The minus sign operation simply multiplies the inputted number by -1. If the inputted number is positive, it turns to negative. If it was negative, it turns to positive. The decimal operation just makes the boolean for decimals true. The equal sign operation adds the inputted number to the double List. My result variable is equal to the double result from a calculate method that has parameters for both of my lists. This result is then printed, the boolean for decimals resets to false, both Lists are cleared, and the result from the calculate method is added to my empty double List so we can continue with further operations or if the user wants to delete all. 

CALCULATE METHOD
The calculate method starts by making a double variable that gets the first value in the double List. The method then goes through two for loops. The first starts at 1 and goes until the length of the double List. This seems to be because it wants to account for the fact that in the first iteration, the first two numbers are added. The second for loop starts at 0 and goes until the length of the String List. There is then a switch case that gets the operation at the current iteration of the second for loop. The result variable undergoes whatever this operation is with the value that is in the current iteration of the double List. I might need to rethink this method because the second for loop will go to completion first before moving on to the next iteration of the first for loop. This means the first operation will apply to every number in the double List, and then the second operation will apply to every number in the double List besides the first one, and so one. I want to assure that I'm going through both for loops concurrently, so to speak. I want to use the first operation for the first two numbers, and then use the second operation for the result of that with the next number, and so on.

-----
That pretty much summarizes my program so far. I will go through this for my next post and correct my code. Once I do, that should pretty much be everything. I was practically done with the project; I was just stuck on how to deal with decimals and how to calculate. I believe I'm on the right train of thought with my numbers operation when dealing with decimals, so I need to apply that to my delete operation. I also need to fix my calculate method so that it's going through every operation properly with the correct variables and I want to find out why I did my percent operation like I did. Next time, I want to add a text box to my GUI that will display what the user is pressing instead of just having the presses stored in the program invisible to the user and just printing out the result to the console.

Now, for a quick update on my group project. It's called The Clean Exchange. We've started on a basic home page, but no text is saved and the buttons don't do anything. I believe first on the agenda is to get our login working once the website is finished. We would take user input for username, password and email, then send a confirmation email to the user and record the username and password. The leader of the group mentioned that she wanted to securely encrypt passwords by using a salt and hashing. I will have to look into this more and how to implement it with my programming language of choice, which would either be Java or Python.

In my next post, I will discuss more about what I've done to fix my calculator program, what I've added to it, and the progress of the group project. I will see you guys then!