Você está na página 1de 8

How to Build a Calculator using Java

Shakti Patel
10/25/2015
I.

Introduction
Programming is used for various purposes. Many of the devices we
use daily function using a built-in program. Programs can be written
in many languages, many of them being similar or different to each
other. An example of the uses of programming can be as simple as
the greeting Hello world!. Less obvious examples of programming
that take part in our daily lives including fuel pumps (gas station),
elevators, and washing machines. A device we use very often is the
calculator. Whether it is used to compute a big equation or just to
ensure your answer, it has a built in program that allows you to
add/subtract/multiply/divide/etc. The procedures found below the list
of materials section are a step-by-step process to build a fourfunction (add/subtract/multiply/divide) calculator implementing Java.
Java is a commonly used programming language since it is objectoriented and allows for an infinite number of uses. This guide is a
great practice for beginners to gain familiarity of the syntax,
keywords, and the use of the API for Java before starting the
implementation of more than one class.

II.

Required Materials

o A computer running either one of the following operating systems:


Windows/Mac/Linux
o JDK (Java Developer Kit)
o BlueJ IDE
o Java API
III.

Procedures

1.

Open the BlueJ IDE

2.

Create a new project and save it in any location on your hard


drive

3.

Create and open a new .java file. Try to give it a meaningful


name in so that it is easy to identify for you and other users in
the future.

4.

In the flower box at the top of the java file, include the author
(you), the date, and the purpose of the specific file. Dont forget
to write a brief a description in the line provided above.
This is very crucial as you write
programs of more importance. Also, this
will declare your right over the code to
prevent plagiarism

The Java API is a directory of classes for


programmers to use. Importing the utility
class is important and your program will not
run correctly without it. This is a VERY
important step!

5.

Import the Java utility and language class (of the API)

6.

Remove all of the comments and source code from the


Calculator class

Make sure to have an empty green


box before you begin. Otherwise,
you will incur errors compile/run
the program.

7.

Create a main class (within the Calculator class) with a static


modifier to set up an entry point for the project

8.

Create an instance of the Scanner class (Hint: Declare a local


variable for it first)

9.

Create two double local variables that will store two values of
the users choice
3

10.

Create an int local variable that will be used for the user to
select a list of operations

** You will now start writing the functioning portion of the code **
11.

Write the output asking the user to enter two numbers

12.

Write an input line using the instance of the Scanner class you
created earlier to collect and store the values from the user

13.

Write an output line asking the user which function they would
like to use implement (add/subtract/multiply/divide) (Hint:
Consider the int variable you created earlier to store the userselected operation)

14.

Write an input line to store the users desired operation

This next portion of the code is important and is a great example of what
makes programming unique. There are several ways to code programs that
perform specific functions. This step can be done either with or without
following the next instructions based on your own knowledge. Your options
include but are not limited to: if/else statements, a switch, implementing
(static) methods. This guide will continue the program using if/else statements
due to its ease of use.

15.

Create an if/else statement for each operation you provided to


the user. Each statement should contain the code that performs
the operation respective to the condition of the statement.
(Hint: You have the ability to use else if)
** CHALLENGE: Implement input validation in this step for
the input the user provided when choosing from the list of
operations **

Congratulations! You have programmed a functional calculator! To


run the calculator, compile the code and run the file (void main
option) on BlueJ.

TROUBLESHOOTING
Always keep in mind that there are three types of errors in Java: runtime,
logical, syntax
Your program may run but may not always have the correct output
To fix any issues in calculations, most likely the error is in the if/else
statements (or another method) portion of the program
Refer to highlighted lines of code or the messages at the bottom if any errors
occur when you compile the program; they help identify the specific location
of the error and the type

PROVIDED BELOW IS THE VISUAL/SOLUTION TO THE PROCEDURES

THIS IS THE SOLUTION TO THE CHALLENGE IN STEP 15

Você também pode gostar