Sunteți pe pagina 1din 6

technology workshop craft home food play outside costumes

How to Make a Simple Calculator in Java


by ajenne1 on March 10, 2016

Table of Contents

How to Make a Simple Calculator in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Intro: How to Make a Simple Calculator in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 1: Download Dr. Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 2: Create your class structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 3: Declare Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Step 4: Build the user input method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Step 5: Declare output value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Step 6: Build our switch statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Step 7: Display our output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Step 8: Compile our progam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Step 9: Run our code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

http://www.instructables.com/id/How-to-Make-a-Simple-Calculator-in-Java/
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.

http://www.instructables.com/id/How-to-Make-a-Simple-Calculator-in-Java/
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.

http://www.instructables.com/id/How-to-Make-a-Simple-Calculator-in-Java/
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.

http://www.instructables.com/id/How-to-Make-a-Simple-Calculator-in-Java/
Step 9: Run our code
Now we can press run and see how well our code performs.

Related Instructables

Java How to Write Java Java Java


(Programming Setup
Programming 2 First Program in Programming Programming
| How to work Language) For Part1 (Setting Part2(Text and Java for
JAVA by Kevin-
with Eclipse Beginners by Up Eclipse) by running) by Beginners by
O-Brien
(Java) by Electrical Circut Coder0806 Coder0806 nfarrow
ZemByte

http://www.instructables.com/id/How-to-Make-a-Simple-Calculator-in-Java/
Advertisements

Comments
1 comments Add Comment

tomatoskins says: Mar 10, 2016. 4:43 PM REPLY


Very cool! I've never done much with Java. I'm only ever really done anything with C++. Looks very similar however.

http://www.instructables.com/id/How-to-Make-a-Simple-Calculator-in-Java/

S-ar putea să vă placă și