Making a Calculator using a Switch Case Statement
Start course
Difficulty
Beginner
Duration
3h 15m
Students
55
Ratings
4/5
starstarstarstarstar-border
Description

In this course, we look at control flow statements in the Java programming language. 

Learning Objectives

  • Decision-making statements
  • Looping statements
  • Branching statements 

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language.
  • Basic computer knowledge
Transcript

Hi there, in this video, we will develop a calculator application using the knowledge we have learned. This will be a simple calculator. A calculator is one of the first things a programmer will learn to build in their introduction to coding. Our calculator will be doing some basic arithmetic operations: addition, subtraction, division, multiplication, and modulus. If you're ready, let's open the Eclipse and continue from there. First, let's create a new Java project. I'll click the File menu and select the 'New', 'Java Project' options. I'll specify the project name as Calculator. The Java version will be JavaSE-8 or JavaSE-1.8.

Don't change the other options and click the 'Finish' button. In this project, right click on the 'src' folder and select 'New Class'. Specify package name as com.academy.calculator. You can specify the package name like this and specify the class name as Calculator. And select the checkbox to add the main method. As you know, the calculator takes inputs from the user and makes calculations. So, we need to take inputs from the user by using the scanner class. The object of the scanner class can be input. In the first print method, we write a title for program output. Its name is CALCULATOR, with uppercase. We show a message to the user to enter two numbers by using the print method. And we declare two variables, number1 and number2 with int type.

And design the next int methods of scanner class to these variables. Okay, we show a message to choose an operation. The user may enter one of the plus (+), minus (-), asterisk (*), forward slash (/), and percent (%) marks. Also, we need to declare a char type operator, which will signify the operation we want to perform; "char operator =" and design the first element in the charAt method of the next method of scanner class to this variable. So, after the equal sign, I write, "input.next().charAt(0)".

The charAt method is used for taking a character input in Java. So, if the user enters two or many operators on the same line, it will deal with only the first operator. If I change this value to 1, it will deal with the second operator. Okay, we need to declare a variable result with double type. Its initial value is 0.0. Okay, let's start to write the switch case statement. The switch will receive the operator variable, and depending on what it is, choose a certain case. The first case value is the additive operator in a single quotation mark because we define the char data type between the single quotes. And in the first case, we'll do the addition of two numbers.

We assign number1 + number2 to the variable "result". And we want to use the "break" keyword. Let's copy the code and paste it five times. And now, let's make changes for case values and other arithmetic operations. We'll use the subtraction operator in the second case, multiplication operator in the third case, division operator in the forth case, and modulus operator in the last case. Okay, we have five operators. So, if the user enters except these five operators, we should warn the user in the default code block. In the default part we display the "Invalid choice operator" message to the user by using the print method.

And let's display the calculation and result to the user using the println method; "System.out.println(number1 + " " + operator + " " + number2 + " = " + result". Also, it's better to close the scanner object by using the close method. Okay, let's run the code. I'll enter 12 and 13 for two numbers, and I will choose the additive operator. So, the result is 12 + 13 = 25.0. Okay, let's run the code again. I'll enter 12 and 7, and I'll choose the subtraction operator this time. The result is, 12 -7 = 5.0.

Okay, let's run the code again. I'll enter 13 and 3, and I'll choose the multiplication operator. The result is 13 * 3 = 39.0. Okay, let's run the code again. I'll enter 15 and 5, and I'll choose the division operator. The result is 15 / 5 = 3.0. Okay, let's run the code again. I'll enter 8 and 5, and we choose the modulus operator. The result is 8 % 5 = 3.0. Okay, finally run the code, and I will enter 12 and 4, and choose illegal (&) operators this time. The result is "Invalid operator choice". This message is true. But if you notice, we also get 12 & 4 = 0. So, this is the wrong message to the user. The user mustn't see this statement. In order to solve this problem, instead of using the "break" keyword, we use the "return" keyword in the default code block. The "return" key is used to exit the current method and the "break" is used to exit a switch statement. Did you get it? "Return" keyword

exit the current method, "break" keyword exit the switch statement method. Cool, we're using the "return" keyword in the next exercises. Yes, let's run the code again. We enter 12 and 4, and choose a legal operator (&). Yes, we get only the "Invalid operator choice" message. Yes, we finished our project successfully. Let's take a short break here, see you in the next lesson.

 

About the Author
Students
1922
Courses
64
Learning Paths
4

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics