|
| Csc 123 - Computer Science I, Programming in Java |
|
Practice Problems
-- Selection
Algorithms using simple IF statements 1. Read in a person's temperature. If the temperature is above 99.8, have the computer say "You have a fever." 2. Input the weight of the two people riding in a small Cessna airplane. Then input the weight of their luggage. If the total weight is over 700 pounds, say: Warning: Aircraft overloaded. 3. Read in 2 numbers. If the numbers are not equal, print the absolute value of their sum divided by the absolute value of their difference. Use the Math.abs() function in this calculation. Note: this is an example of using an if statement for error protection. If the numbers are equal, the program would attempt to divide by 0. You cannot divide by 0 in mathematics. An attempt to divide by 0 will crash a program in most computer languages, including C++. 4. Read in 2 numbers. If the numbers are not equal, print the absolute value of their sum divided by the absolute value of their difference. Use not the Math.abs() function in this calculation. Algorithms using IF and ELSE statements. 5. Read in two numbers and output the larger number. 6. Read in two numbers, output the larger, and then output the smaller. 7. Write a division program that asks the user for a number, and then for a divisor. If the divisor equals 0 say "You can't divide by zero", otherwise give the answer of the division. This is another example of a protective if statement. 8. An amusement park sells ride tickets for $1.00 each if you buy less then 10, but only charges 90 cents each if you buy 10 or more. Write a program that asks the user how may tickets she wants to purchase, then tells her the price. 9. Input the number of hours worked, and the hourly pay rate for an employee. Compute the gross-pay for the employee, giving time and a half for any hours over 40. Output the gross pay.
Algorithms requiring compound conditionals 10. Input a person's age and cholesterol count. If the age is over 40 and the cholesterol is over 120, print "Possible heart problems." 11. Input a pressure on a water coolant pipe. If it is above 300 pounds per square inch or below 50 pounds per square inch, say "Coolant Emergency!!". 12. Input a person's salary. If it between 0 and 8999 inclusive, say "No tax due", otherwise say, "You must file"
Multiple IF statements with mutually exclusive conditions 13. Prompt the user for a number. If the number is greater than 12 say "More than a dozen". If it is less than 12 say "Less than a dozen". If it is equal to 12 say "An even dozen.". 14. Write a children's program that inputs a single digit from 0 to 10, and then prints out a silly saying that a child would like using that number. Use cut and paste! 15. Ask the user for a light bulb wattage. If the wattage is 40 or 60 say "acceptable for socket". If the wattage is 75 or 100 say "socket overload". If the user enters something else, say "Not a standard light bulb wattage". 16. (Note: this will give you an idea how long I have been using these practice problems). Read the length of a television program, in minutes, from the user. If the length is under 122 minutes have the computer say "Use SP mode on your VCR". If the length is between 122 and 241 minutes, inclusive, say "Use LP mode on your VCR". If the length is between 242 and 361 minutes, inclusive, say "Use SLP mode on your VCR". If the length is over 361 minutes say "Use more than one video tape".
17. Prompt the user for the width and height of a rectangle, and input the width and height. If the width is negative, say that a rectangle cannot have a negative width and stop. Otherwise, if the height is negative, say that a rectangle cannot have a negative height and stop. If neither is negative, print "The area of the rectangle is " and then print the area. Note: there are a number of different solutions to this problem. Do one that has nested if statements. It will probably be the more efficient than one that does not have nested if's. 18. Input an angle representing a partial rotation and the number of times the wheel should do the partial rotation. If both inputs are positive, output the number of complete rotations the wheel does, and the remaining angle (under 360 degrees), that the wheel will rotate after doing the partial rotation the proper number of times. If either input is negative or zero, output an appropriate error message. 19. Input a person's age and cholesterol count. If the age is over 40 and the cholesterol is over 120, print "Possible heart problems." Otherwise, if the cholesterol is over 120, print "You should reduce your cholesterol." Otherwise print "Your cholesterol count is fine." Make sure your program prints exactly one message each time it is run.
Programs that can use descending nested greater-thans, or ascending nested less-thans. These programs can also be done with multiple IF's with compound conditionals. 20. Ask the user's age. If the user is over 60, say "That's the cat's pajamas.", otherwise if the user is over 30, say "Cool, man.", otherwise say "Radical!". 21. Input the measured length of a fish. If the length is less than 10 centimeters, print "Cat Food". Otherwise, if the length is less 20 centimeters, print "Fish Cakes". If neither previous statement is true, and the length is less than 30 centimeters, print "Super Market Quality". If none of the above are true, print "Restaurant Quality".
22. Read in 3 numbers and output the largest number. 23. Get the length of 3 sides of a triangle from the user, who is allowed to enter the lengths in any order. Tell the user if it is a right triangle. (Note: If it is a right triangle, the square of the longest side will be the sum of the squares of the other two sides.) 24. Read in 4 numbers and output the largest number. Note: this algorithm can be an extension of problem 22. 25. Input 4 numbers and print them in ascending order.
|
| Back to Csc 123 - Computer Science I, Programming in Java |
| Scott Badman Office: B132 Phone: 353-2250 sbadman@parkland.edu |
Parkland College, 2400 W. Bradley Avenue, Champaign, IL 61821 |