From the course: Java 8+ Essential Training: Syntax and Structure
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Solution: A more complex calculator - Java Tutorial
From the course: Java 8+ Essential Training: Syntax and Structure
Solution: A more complex calculator
- [Instructor] In this challenge, the job is to create a calculator that accepts two numeric values, and then asks the user what kind of operation they want to execute. They can type in a plus, a minus, a times or a divide symbol, and the result is whatever operation is requested. Here is my solution to the challenge. First of all, I've extracted my code to receive input from the console to a separate method called get input. This is exactly the same code I used in a previous video. I'm calling that method three times with three different prompts, and saving the responses as three different string variables. Next I'm declaring a variable typed as a double, named result, and setting it to zero. I declare it up here so that I can then affect that value anywhere in the switch statement, but then refer to the value later on. If you were to declare the variable within the switch statement, it wouldn't be available after the switch statement is complete. Next, I evaluate the operation that…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
Evaluate conditions with if-else4m 17s
-
Evaluate conditions with switch-case4m 32s
-
Create looping code blocks7m 13s
-
Create reusable code with methods8m 17s
-
Create overloaded methods3m 27s
-
Pass arguments by reference vs. value4m
-
Challenge: A more complex calculator1m 18s
-
Solution: A more complex calculator2m 20s
-
-