Calculator - Using Conditionals and Lambda Mini-Lab
Throughout this lab, you will be asked to make modifications to your program code and write the code in new ways. When making modifications, it is best to comment out the prior mission and add the new mission, rather than deleting the prior mission. Label each with the mission you have fulfilled.
Required Knowledge
This series of labs require you to write program code to use the following topics. Tutorials for available topics have been linked.
- Boolean Expressions
- If.. else statements
- Ternary conditional operator
- Switch statements with lambda notation
- Switch expressions with lambda notation
- Interfaces
- Implementation of interfaces with lambda
Missions
Mission 1
Create a Calculator class based on the specification below. Do not use any pre-written methods for your calculations.
| Instance Variables |
|---|
|
| Constructors |
|
| Methods |
|
Create a CalculatorTester program that will create a Calculator object for each constructor. Use these objects to test the functionality of the four methods.
Allow the user to enter the values to be added and subtracted. Using an if statement, allow the user to determine the operation they want to be used by enter
+ for addition and +- for positive difference.
Mission 2
Modify the program you wrote in Activity 1 to use a ternary conditional expression in place of the if statement used to determine the operation entered by the user and complete the computation. Comment out your if statement and label it Mission 1 and then add the ternary conditional expression.
Mission 3
Modify the program you wrote in Activity 1 by adding the following methods to the Calculator class:
double product()- returns the product ofvalue1timesvalue2double product(double v1, double v2)- returns the product ofv1timesv2double quotient()- returns the quotient of dividingvalue1byvalue2as long asvalue2is not0; otherwise prints that a divide by 0 error has occurred and returns0. NOTE: If usingintvalues, use a try, catch expression to avoid dividing by 0.double quotient(double v1, double v2)- returns the quotient of dividingv1byv2as long asv2is not0; otherwise prints that a divide by 0 error has occurred and returns 0. NOTE: If usingintvaluse, use a try, catch expression to avoid dividing by 0.
Comment out the ternary conditional operator used in Mission 2, labeling it Mission 2. Then, uncomment your mission 1 program code. Modify Activity 1 to allow users to enter * for product and / for quotient. Modify the if statement to handle the new options to compute the product and quotient.
Mission 4
Modify the program you wrote in Activity 3 to use a nested ternary conditional expression in place of the if statement used to determine the operation entered by the user and complete the computation. Comment out your if statement and label it Mission 1 and 3.
Mission 5
Modify the program you wrote in Activity 4 to use a switch statement in place of the ternary conditional expression used to determine the operation entered by the user and complete the computation. Comment out the nested ternary conditional expression and label it Mission 4. Do not use lambda notation in this activity.
Mission 6
Modify the program you wrote in Activity 5 to use a switch statement with lambda anotation. Comment out the switch statement you wrote for mission 5 and label it Mission 5.
Mission 7
Modify the program you wrote in Activity 5 to use a switch expression in place of the switch statement used to determine the operation entered by the user and complete the computation. Comment out the switch statement you wrote in mission 6 and label it Mission 6. Do not use lambda notation in this activity.
Mission 8
Modify the program you wrote in Acitvity 7 to use a switch expression with lambda notation. Comment out the switch expression you wrote in mission 7 and label it Mission 7.
Mission 9
Create an interface called Computation that contains the abstract method operation that takes two double values as parameters. Re-write Activity 8 to use lambda to create instances of Computation that implements operation for each of the four operators. Comment out the switch expression you wrote in mission 8 and label it Mission 8.
Open Responses
- These activities demonstrated the use of different conditional structures. Which of the these do you think was the most appropriate for this program and why?
- Which of the different ways to write the program did you find the most readable and why?
- Describe a method that could be added to
calculatorthat would mean you couldn’t use the ternary conditional operator. - Describe the difference between a switch statement and a switch expression.
- Which version of the program did you prefer: the one that didn’t use lambda notation or the one that used lambda notation? Why?