Practice Problems - Selection / Decision StructuresThese problems will help you learn to use various programming concepts.
|
Intro to Turing (2014)
Practice Problems
NOTE: If you are using Processing, just define a variable where you will ASSIGN the value to be received from the user for your structures to evaluate. NO need to implement ControlP5 for getting user input.
1. Write a program to divide a class of students in two groups: those whose last names begin with A to H and those that begin with I to Z. Ask a person to enter their last name and then output a message indicating which group they are in. Repeat for each student.
2. Prepare a check for someone eating lunch in a restaurant. If the meal costs more than $4.00 a 13% tax is to be added.
3. Write a program to classify athletes into three classes by weight. The categories are: over 80 kg - heavyweight, between 60 and 80 kg - medium weight, and less than 60 kg - lightweight. Prepare the program so that a team of 10 athletes can enter their weight one after another and be told what category they are in.
4. Write a program to read a series of first names of people. After reading the series (you will need an EOF), output the name that is alphabetically last.
5. Write a program to display a multiple choice question with five different answers in the window and then depending on which answer is chosen give a different comment. For example, your question might be
Processing is:
(1) a great programming language
(2) a kind of car
(3) a verb
(4) a machine
(5) all of the above
The choice will be an integer from 1 to 5. Use a SWITCH/CASE construct for this.
6. Write a program to give proper greetings to a person. Ask what the occasion is and then give the appropriate greeting.
You can offer a menu of available occasions. Use a SWITCH/CASE construct with string input.
7. Federal income tax is to be levied in stages on taxable income. On the first $27,500 you pay 5%, on the next $27,500 you pay 19%, and on the rest 29%. Write a program to read in a taxable income and compute the federal tax payable to the nearest cent.
8. Write a program to read in a series of positive integers and output the range of the integers, that is, the interval from the smallest to the largest.
9. Compute values of the function y = 3x^2 – 2x+1 in steps of 0.1 between x=0 and x=1 inclusive and find the value of x for which f(x) is a minimum.
10. Marks in a test are given out of 10 where 9 or 10 is A grade, 7 is B grade, 6 is C grade, 5 is D grade, below 5 is F grade. Use compound IF statements to change the numerical test mark into the appropriate letter grade. Note that if you want to give the same grade for both 9 and 10 you can do something like:
if((grade == 9) !! (grade == 10)) ......
Arrange to enter a number of test marks for a class ending with a mark of –1. At the end, output the percentage of the class with each letter grade. To do this you must keep track of the total numbers of marks and the numbers in each grade.
11. Letter grades are assigned as follows:
A - mark in the 80's or 90's B - mark in the 70's C - mark in the 60's
D - mark in the 50's F - mark less than 50
Write a Processing program that examines the numeric grade (can be an integer) and finds and displays the corresponding letter grade.
12. Write a Java program that uses a for loop to display the whole numbers from 1 to 15
along with their squares and cubes. You should display the numbers in a nice table with
column headings. Hint:
Example partial output:
Squares and Cubes
Number Square Cube
1 1 1
2 4 8
3 9 27
. . .
. . .
15 225 3375
13. Write a Java program which draws a rectangle of "*" on the screen. Your program should
have integer variables for the height and the width of the rectangle. The required code to solve this problem is quite simple if you plan out your code carefully. Extra: Only draw the outline of the rectangle (see example below). The following examples are for a rectangle with a height of 4 and a width of 6.
Normal: ****** Outline: ******
****** * *
****** * *
****** ******
14.Write a program called Mystery which offers a menu of mysterious alternatives. Use a case construct to do quite different things in response to the different choices.
15. Write a program to input an integer from the user and output all its factors and whether or not it is a prime number. Remember that a prime number has only two factors, 1 and itself. Three is prime while four is not.
16. Write a program to output all the prime numbers from 1 to 50. The way to find primes is to divide a number by all the whole numbers up to the INTEGER SQUARE ROOT of the number. If you ever get a REMAINDER of ZERO, it means that it has a factor other than 1 or itself and it's NOT PRIME. You'll want to use the '%' function in Processing for this.
17. Write a program that asks the user for 10 marks(percents) and, at end of the run, outputs the highest mark and the lowest mark.
18. Write a program to input a a series of positive integers until an EOF is entered. Use –1 as the EOF. Output the product of all the integers input.
19.Write a program to read in 5 marks that should be between 0 and 100 inclusive. Output an error message if a mark is not between 0 and 100. For the valid marks, output "good" if the mark is between 70 and 100 inclusive and "satisfactory" if the mark is between 50 and 69 inclusive.
1. Write a program to divide a class of students in two groups: those whose last names begin with A to H and those that begin with I to Z. Ask a person to enter their last name and then output a message indicating which group they are in. Repeat for each student.
2. Prepare a check for someone eating lunch in a restaurant. If the meal costs more than $4.00 a 13% tax is to be added.
3. Write a program to classify athletes into three classes by weight. The categories are: over 80 kg - heavyweight, between 60 and 80 kg - medium weight, and less than 60 kg - lightweight. Prepare the program so that a team of 10 athletes can enter their weight one after another and be told what category they are in.
4. Write a program to read a series of first names of people. After reading the series (you will need an EOF), output the name that is alphabetically last.
5. Write a program to display a multiple choice question with five different answers in the window and then depending on which answer is chosen give a different comment. For example, your question might be
Processing is:
(1) a great programming language
(2) a kind of car
(3) a verb
(4) a machine
(5) all of the above
The choice will be an integer from 1 to 5. Use a SWITCH/CASE construct for this.
6. Write a program to give proper greetings to a person. Ask what the occasion is and then give the appropriate greeting.
You can offer a menu of available occasions. Use a SWITCH/CASE construct with string input.
7. Federal income tax is to be levied in stages on taxable income. On the first $27,500 you pay 5%, on the next $27,500 you pay 19%, and on the rest 29%. Write a program to read in a taxable income and compute the federal tax payable to the nearest cent.
8. Write a program to read in a series of positive integers and output the range of the integers, that is, the interval from the smallest to the largest.
9. Compute values of the function y = 3x^2 – 2x+1 in steps of 0.1 between x=0 and x=1 inclusive and find the value of x for which f(x) is a minimum.
10. Marks in a test are given out of 10 where 9 or 10 is A grade, 7 is B grade, 6 is C grade, 5 is D grade, below 5 is F grade. Use compound IF statements to change the numerical test mark into the appropriate letter grade. Note that if you want to give the same grade for both 9 and 10 you can do something like:
if((grade == 9) !! (grade == 10)) ......
Arrange to enter a number of test marks for a class ending with a mark of –1. At the end, output the percentage of the class with each letter grade. To do this you must keep track of the total numbers of marks and the numbers in each grade.
11. Letter grades are assigned as follows:
A - mark in the 80's or 90's B - mark in the 70's C - mark in the 60's
D - mark in the 50's F - mark less than 50
Write a Processing program that examines the numeric grade (can be an integer) and finds and displays the corresponding letter grade.
12. Write a Java program that uses a for loop to display the whole numbers from 1 to 15
along with their squares and cubes. You should display the numbers in a nice table with
column headings. Hint:
Example partial output:
Squares and Cubes
Number Square Cube
1 1 1
2 4 8
3 9 27
. . .
. . .
15 225 3375
13. Write a Java program which draws a rectangle of "*" on the screen. Your program should
have integer variables for the height and the width of the rectangle. The required code to solve this problem is quite simple if you plan out your code carefully. Extra: Only draw the outline of the rectangle (see example below). The following examples are for a rectangle with a height of 4 and a width of 6.
Normal: ****** Outline: ******
****** * *
****** * *
****** ******
14.Write a program called Mystery which offers a menu of mysterious alternatives. Use a case construct to do quite different things in response to the different choices.
15. Write a program to input an integer from the user and output all its factors and whether or not it is a prime number. Remember that a prime number has only two factors, 1 and itself. Three is prime while four is not.
16. Write a program to output all the prime numbers from 1 to 50. The way to find primes is to divide a number by all the whole numbers up to the INTEGER SQUARE ROOT of the number. If you ever get a REMAINDER of ZERO, it means that it has a factor other than 1 or itself and it's NOT PRIME. You'll want to use the '%' function in Processing for this.
17. Write a program that asks the user for 10 marks(percents) and, at end of the run, outputs the highest mark and the lowest mark.
18. Write a program to input a a series of positive integers until an EOF is entered. Use –1 as the EOF. Output the product of all the integers input.
19.Write a program to read in 5 marks that should be between 0 and 100 inclusive. Output an error message if a mark is not between 0 and 100. For the valid marks, output "good" if the mark is between 70 and 100 inclusive and "satisfactory" if the mark is between 50 and 69 inclusive.