Labs
These labs are designed to use a switch statement.
-
Write a program that asks for a temperature and scale. Use a
switchstatement and take advantage of fall through. -
If the scale is
C,c,Celsius, orcelsius, convert the number from Celsius to Fahrenheit with the formulaFahrenheit = (9.0/5.0 Celsius) + 32. - If the scale is
F,f,Fahrenheit, orfahrenheit, convert the number from Fahrenheit to Celsius with the formulaCelsius = 5.0/9.0 (Fahrenheit - 32). - Depending on the scale, the output should look like
32 degrees Fahrenheit is 0 degrees Celsius. - If the scale is invalid, print an error message.
(Solution: IfCelsFahrSwitch.java)
-
Write a simple calculater program.
-
Ask the user to enter two numbers, reading them into two
doublevariables. - Ask the user to enter an operation:
+,-,*,/, or%. - Use a switch to determine which operation they entered, and output the correct result.
- If an invalid operation is entered, output "Operation not valid".
- Use fall-through to allow a variety of valid input for each operation, such as
*,times,multiply, etc., calculating the result in only one place for each type of operation.
(Solution: CalculatorSwitch.java)