Switch Exercises
Lec04 - switch exercises
Create a new class called SwitchExercises
- Create a method called switchOnAString(). In the method, read in a name from the user. Using a switch construct, determine if the name entered is “john” or “mary”. If “john”, output “Male”, if “mary” output “Female”. Call the method from main().
a. expand to include “JOHN”, “John”, “MARY” and “Mary”.
Enter a name : john
MaleEnter a name : mary
FemaleEnter a name : JOHN
Male
- Create a method called switchMonth(). In the method, read in a month number from the user. Using an switch construct, output the text for that month e.g. 1 is January etc.. Call the method from main().
a. Change the application to use constants.
Enter a month : 1
JanuaryEnter a month : 5
MayEnter a month : 12
DecemberEnter a month : 20
Invalid month 20
- Create a method called switchChar(). In the method, read in two numbers of type double (num1 and num2) and a character (operation). Using a switch construct, perform the mathematical operation identified by operation e.g. if ‘+’ add num1 and num2. Output the result (there should be only one output statement (which is after the switch statement)).
Enter number1 : 3
Enter number2 : 4
Enter a char : +
3.0+4.0=7.0Enter number1 : 5
Enter number2 : 4
Enter a char :
5.04.0=20.0Enter number1 : 10
Enter number2 : 4
Enter a char : -
10.0-4.0=6.0Enter number1 : 15
Enter number2 : 3
Enter a char : /
15.0/3.0=5.0Enter number1 : 4
Enter number2 : 5
Enter a char : @
Invalid mathematical operator @
- Create a method called switchSquareOrCube(). In the method, read in a number n within the range 1..10. Using a switch construct, if the number is even (case 0:), output the square of n; if the number is odd (case 1:), output the cube of n.
Enter a number (1..10) : 4
4 squared is 16.0Enter a char : e
e is a vowelEnter a number (1..10) : 5
5 cubed is 125Enter a number (1..10) : 11
11 outside range of 1..10
- Create a method called switchVowelOrConsonant(). Take in a letter from the user and using a switch statement, determine if the letter is a vowel or a consonant. Ensure that your switch statement only has case statements for lowercase letters and that the default section is for consonants only.
Enter a char : e
e is a vowelEnter a char : f
f is a consonantEnter a char : @
@ is invalid input
- Create a method called switchDaysInMonth(). In the method, ask the user for a year and a month. Using a switch statement calculate the number of days in the month and output the result. Note: the year input by the user is needed to calculate if February occurs on a leap year (Google the leap year logic).
Enter year : 2018
Enter month (1..12) : 2
Number of days : 28Enter year : 2016
Enter month (1..12) : 2
Number of days : 29Enter year : 2017
Enter month (1..12) : 12
Number of days : 31Enter year : 1996
Enter month (1..12) : 6
Number of days : 30