Intro: How to Make a Simple Calculator in Java
After the "Hello World!" program, a calculator is one of the first things a programmer will learn to build in their introduction to coding. The reason for this is because of the simplicity of its structure in addition to covering most of the basic concepts in programming. Follow the steps below and you too will be able to build your own fully functioning calculator!
Step 1: Download Dr. Java
All software programs are written on Development Environments, programs made specifically to build and compile software. For the programming language we're using, Java, the program Dr. Java is an excellent and simple introductory program to use. Start by downloading the program from www.drjava.org and selecting either the windows app or mac osx app depending on your computer type.
Step 2: Create Your Class Structure
After you first open Dr. Java, you'll be greeted with a blank screen of nothing. So beautiful and pristine, we're going to cover this baby with a lot of code. Start off by declaring a public Calculator class and then inside of it, a main method, exactly like shown in the picture. In Java, a class is simply an object which holds information. In our case, the object we're building is a calculator so we have named it as such. A method is a process which performs a certain task. All methods follow the IPO structure as we will learn later. Brackets in java denote what is contained in what. In our example, our calculator object contains the method we are going to build. The brackets of the method is where our code is going to be contained.
Step 3: Declare Inputs
As mentioned in the last step, all methods follow the IPO structure, short for input, process, output structure. This means that in short all methods receive some form of input, perform a process, and return an output. First we're going to declare the inputs. Inputs can be of different data types so we have to be specific in our declaration. The main data types are int(i.e. 1, 2, 3...), double (i.e. 1.0, 2.7, 0.8....), char(a,b,c,d,+,-,#...), and string(hello, yes, hi, word up....). For our example we need to declare two int type numbers and a char type operator which will signify the operation we want to perform. Also one thing we must note are the use of semi-colons. In coding these serve almost as periods and signify breaks in line for the computer reading the code.
Step 4: Build the User Input Method
Now we begin the process portion of the method. The first thing we must build is a method for the user to enter whatever type of input we like. We can achieve this with the use of a scanner which stores any data the user types in an assigns it to the inputs we declared previously. We import the scanner object (top line), declare one for use, and then type three lines asking the user for input and then assigning those values.
Step 5: Declare Output Value
We need to declare a value where our output value is stored. We'll use a double type in case the answer is not a whole number.
Step 6: Build Our Switch Statement
For the last portion of the process section, we must make a switch statement. The switch receives the operator variable and depending on what it is, chooses a certain case.
Step 7: Display Our Output
Finally we have to declare a line to display the output of our calculator.
Step 8: Compile Our Progam
Now that our code is complete, we must compile (or build) the code. Save the program in whichever location is best for you.
Step 9: Run Our Code
Now we can press run and see how well our code performs.