Making a Calculator using a Switch Case Statement
Start course
Difficulty
Beginner
Duration
3h 15m
Students
115
Ratings
4.6/5
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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8792184,"math_prob":0.82631135,"size":5920,"snap":"2023-40-2023-50","text_gpt3_token_len":1366,"char_repetition_ratio":0.14131169,"word_repetition_ratio":0.037463978,"special_character_ratio":0.2391892,"punctuation_ratio":0.14225589,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95291334,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-28T18:26:10Z\",\"WARC-Record-ID\":\"<urn:uuid:90aefa4f-d09c-46ce-bb96-d8c82b0f821f>\",\"Content-Length\":\"210402\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0861b79e-73fb-4330-8796-05469559f95d>\",\"WARC-Concurrent-To\":\"<urn:uuid:8526b572-50f8-4f18-9b79-d4fc86902673>\",\"WARC-IP-Address\":\"13.249.39.50\",\"WARC-Target-URI\":\"https://cloudacademy.com/course/control-flow-statements-3181/making-a-calculator-using-a-switch-case-statement/\",\"WARC-Payload-Digest\":\"sha1:3DDIX5KBS3S7XA2E3XULMNEJLHNO7MWV\",\"WARC-Block-Digest\":\"sha1:3WMTAAG6F6VT4CKZSUAIRBI3RZ4M3G2F\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510427.16_warc_CC-MAIN-20230928162907-20230928192907-00853.warc.gz\"}"}