Problem Set 2: If & Switch To complete this set of problems you must have gone through the following lessons:
- All lessons for Problem Set 1
- If
- Switch
Operators for If Statements | Operator | Meaning | == | equal to | < | less than | <= | less than or equal to | > | greater than | >= | greater than or equal too | != | not equal to | ! | not | | or || | or | & or && | and |
Problem 1 | Write a program that asks the user to enter a number from 1 to 100. The computer should check to see if the number falls in the desired range. | Output Example | Enter a number from 1 to 100: 564 That number is not valid.
or...
Enter a number from 1 to 100: 26 That number is valid. |
Problem 2 | Write a program that allows two numbers to be entered. Have the computer compare them and print a message stating whether the first number entered is greater than, equal to, or less than the second number entered. The output should be as shown below. | Output Example | Enter a number: 3 Enter another number: 7 3 is less than 7
or...
Enter a number: 88 Enter another number: 12 88 is greater than 12
or...
Enter a number: 54 Enter another number: 54 The numbers are equal |
Problem 3 | Write a program that has the computer randomly choose a number from 1 to 10. The computer must determine whether the number is odd or even and print a message as indicated below. (Hint: If the number when divided by 2 has a remainder of 0, it is even) | Output Example | The number chosen by the computer is 7. 7 is an odd number
or...
The number chosen by the computer is 2. 2 is an even number |
Problem 4 | Write a program that asks for the temperature. If the temperature is below 60 degrees Fahrenheit the computer should respond with "Wear a coat today". Otherwise, the response should be "Have a nice day". | Output Example | Enter the current temperature (Fahrenheit): 55 Wear a coat today.
or...
Enter the current temperature (Fahrenheit): 75 Have a nice day. |
Problem 5 | Rewrite the previous program to include a response of "Wear your winter coat today" when it is below 40 degrees Fahrenheit and a response of "Wear your bathing suit" when it is 90 degrees Fahrenheit or greater. | Output Example | Enter the current temperature (Fahrenheit): 97 Wear your bathing suit. |
Problem 6 | Write a program that asks the user to input a positive number to be used in calculating the area of a square. The computer should either calculate the area and display the answer or if the number entered is negative, respond with "Invalid entry". | Output Example | Enter a positive whole number: 20 The area of the square is 400 units. |
Problem 7 | Write a program that asks the user to input 2 positive whole numbers that will be used to calculate the area of a rectangle. If the numbers entered are both positive whole numbers then the computer should calculate and display the results. If the numbers entered have one positive and one negative number then the computer should respond with "There is one invalid entry". If both numbers are negative the computer should respond with "The numbers entered are invalid". | Output Example | Enter a positive integer: 4 Enter another positive integer: 8 The area of the rectangle is 32 square units.
or...
Enter a positive integer: -1 Enter another positive integer: 8 There is one invalid entry
or...
Enter a positive integer: 24 Enter another positive integer: -7 There is one invalid entry
or...
Enter a positive integer: -3 Enter another positive integer: -1 The numbers entered are invalid |
Problem 8 | In mathematics, the quantity b2-4ac is called the "discriminant." Write a program that asks for the values of a, b, and c, and displays 'No Roots' if the resulting discriminant is negative, 'One Root' if the discriminant is zero, and 'Two Roots' if the discriminant is positive. | Output Example | Enter the value for a: 1 Enter the value for b: 2 Enter the value for c: 1 a=1, b=2, c=1, the discriminant = 0 and has One Root
or...
Enter the value for a: 3 Enter the value for b: 1 Enter the value for c: 1 a=3, b=1, c=1, the discriminant = -11 and has No Roots
or...
Enter the value for a: 1 Enter the value for b: 4 Enter the value for c: 1 a=1, b=4, c=1, the discriminant = 12 and has Two Roots |
Problem 9 | Write a program that asks for a number grade to be entered with values ranging from 0 to 100. The computer should respond with two messages for each grade entered. Use the Else If statement to account for all possible grades A through F. Use the following grade schedule for your solution:
90 - 100 A 80 - 89 B 70 - 79 C 60 - 69 D <60 F | Output Example | Enter a grade (0-100): 75
The grade you entered was 75 You have earned a letter grade of C
(other possible results omitted to save space) |
Problem 10 | Modify the previous program so that + and - grades are calculated based on the following grading schedule:
97 - 100 A+ 94 - 96 A 90 - 93 A- etc... | Output Example | Enter a grade (0-100): 87
The grade you entered was 87 You have earned a letter grade of B+
(other possible results omitted to save space) |
Problem 11 | Write a program that asks the user for the length, width, and height of a package, and displays "Package Rejected." if any of dimension is greater than 10 cm, and "Package Accepted." if all the dimensions are less than or equal to 10 cm. | Output Example | Enter the package length (cm): 4 Enter the package width (cm): 2 Enter the package height (cm): 5 Package Accepted.
or...
Enter the package length (cm): 10 Enter the package width (cm): 14 Enter the package height (cm): 2 Package Rejected. |
Problem 12 | Pam's Pizza Palace pays its employees time and a half for every hour worked over 40 hours. Write a program to calculate weekly wages given the hours worked and hourly rate. | Output Example | Enter the number of hours worked: 45 Enter the hourly rate: $10 Wages earned = $475.00 |
Problem 13 | Pam employs high school students who are exempt from taxes and others who are not. Rewrite the previous program so that if an employee is not exempt, 18% will be deducted from the weekly wage, otherwise "No Taxes Deducted" will be displayed. (Hint: use the compareTo() method as shown in the If_And Class of the If Project to determine if the letter String entered was a Y or and N) | Output Example | Enter hours worked: 45 Enter hourly rate: $10 Exempt? (Y/N): Y
Wages earned = $475.00 No Taxes Deducted
or...
Enter hours worked: 20 Enter hourly rate: $4 Exempt? (Y/N): N
Wages earned = $65.60 Taxes deducted = $14.40 |
Problem 14 | Write a program that allows two names to be entered. Have the computer compare them and print a message stating whether the first name entered is greater than, equal to, or less than the second name entered. The output should be as shown below. (Hint: use the compareTo() method as shown in the If_And Class of the If Project ) | Output Example | Enter a name: Sue Enter another name: Joe Sue comes after Joe alphabetically. |
Problem 15 | Write a program that allows the user to enter a word. Print the message "Is between." If the word comes alphabetically between the words "Bat" and "Man". The words "Not between." should be printed if the word entered is outside of "Bat" and "Man". (Hint: use the compareTo() method as shown in the If_And Class of the If Project ) | Output Example | Enter a word (first letter capitalized): Mixer Is not between.
or..
Enter a word (first letter capitalized): Car Is between. |
Problem 16 | Using If statements write a program that determines whether a letter entered is a vowel or a consonant. (Hint: use the compareTo() method as shown in the If_And Class of the If Project ) | Output Example | Enter a letter: e E is a vowel.
or...
Enter a letter: K K is a consonant. |
Problem 17 | Use the Switch statement to write a program that produces the following output. | Output Example | Enter a number from 1 to 5: 3 You will eat a banana.
or...
Enter a number from 1 to 5: 2 You will eat an orange.
or...
Enter a number from 1 to 5: 5 You will eat an apple.
or...
Enter a number from 1 to 5: 12 That is an invalid response
(other possible results omitted to save space) |
|
|
|