How to Make a Simple Calculator in Java

by ajenne1 in Circuits > Computers

190265 Views, 24 Favorites, 0 Comments

How to Make a Simple Calculator in Java

image064.png

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!

Download Dr. Java

step 1.jpg

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.

Create Your Class Structure

step 2.jpg

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.

Declare Inputs

step 3.jpg

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.

Build the User Input Method

step 4.jpg

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.

Declare Output Value

step 5.jpg

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.

Build Our Switch Statement

step 6.jpg

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.

Display Our Output

step 7.jpg

Finally we have to declare a line to display the output of our calculator.

Compile Our Progam

step 8.jpg

Now that our code is complete, we must compile (or build) the code. Save the program in whichever location is best for you.

Run Our Code

step 9.jpg

Now we can press run and see how well our code performs.