Calculator - Using Conditionals and Lambda Mini-Lab
This series of labs require you to write program code to use the following topics. Tutorials for available topics have been linked.
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.
Mission 1
Create a Calculator class based on the specification below. Do not use any pre-written methods for your calculations.
Instance Variables |
double value1
double value2
|
Constructors |
Calculator() - assigns value1 and value2 to 0
Calculator(double v1, double v2) - assigns value1 to v1 and value2 to v2
|
Methods |
double getValue1() - returns the value of value1
double getValue2() - returns the value of value2
void setValue1(double v1) - sets the value of value1 to v1
void setValue2(double v2) - sets the value of value2 to v2
double sum() - returns the sum of value1 and value2
double sum(double v1, double v2) - returns the sum of v1 and v2
double posDifference() - returns the positive difference between value1 and value2
double posDifference(double v1, double v2) - returns the positive difference between v1 and v2
|
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 of value1
times value2
double product(double v1, double v2)
- returns the product of v1
times v2
double quotient()
- returns the quotient of dividing value1
by value2
as long as value2
is not 0
; otherwise prints that a divide by 0 error has occurred and returns 0
. NOTE: If using int
values, use a try, catch expression to avoid dividing by 0.
double quotient(double v1, double v2)
- returns the quotient of dividing v1
by v2
as long as v2
is not 0
; otherwise prints that a divide by 0 error has occurred and returns 0. NOTE: If using int
valuse, 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
calculator
that 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?