Header

divider
Links
• Home
• Webgrades
• Contact Me

• Royal High School
• Simi Valley USD


divider

AP Statistics

• Homework Log
• Test Schedule
• Chapter Reviews
• Practice Tests
• Research Project
• Statistics Links

divider
AP Comp-Sci
 Course Content
• Programs
• Practice Tests
• Comp-Sci Links

divider

AP Calculus AB
• Homework Log
• Test Schedule
• Chapter Reviews
• Practice Tests
• Calculus Links


Computer Programming Unit 2 Programs
Conditional Statements


1.   Write a program to determine the slope of a line through two points, (X1, Y1) and (X2, Y2). The formula for slope is (Y2-Y1)/(X2-X1). However, if the denominator is 0, the slope is undefined. Output the slope to one decimal place if it is defined.

Test Data:                        Output:

a.  (1, -3) and (-4, 5)       Slope = -1.6

b.  (2, 3) and (2, 5)          Slope = Undefined

 

2.   The post office will only mail packages with combined length and girth of less than or equal to 100 inches. Write a program to accept the dimensions (length, width and height) in inches and calculate the girth (2*height + 2*width). Then combine the length and girth. Print the combined length and girth and state whether or not the package is acceptable.

Test Data:

a.  Length = 17.5”, Width = 14”, Height = 8”

Output: Combined Length and Girth = 61.5  (ACCEPTABLE)

b.  Length = 36”, Width = 22”, Height = 12”

Output:  Combined Length and Girth = 104  (NOT ACCEPTABLE)

 

3.   Write a program to determine a person’s paycheck based on their pay rate and hours worked. Assume that they are paid “time-and-a-half” for any overtime hours (they receive 1.5 times their pay rate for any hours worked over 40). Print the results as shown below.

Test Data (try both):

a.  Pay Rate = $12.43, Hours = 43                   b.  Pay Rate = $13.81, Hours = 36

Output:                                                             Output:

Regular Pay: $497.20                                       Regular Pay: $497.16

Overtime Pay: $55.94                                       Overtime Pay: $0.00

Total Pay: $553.14                                           Total Pay: $497.16

 

4.   You are writing a program to calculate library fines. The program needs to ask for the type of book and number of days overdue. The fine schedule is as follows (define as constants):

Book Type

Fine

Paperbacks

15 cents per day

Hardcovers

20 cents per day

Magazines

25 cents per day

Use a “menu” to prompt the user for the input. I will show an example in class.

Test Data and Output Form:

a.  Hardcover - 7 days overdue:  $1.40

b.  Magazine - 3 days overdue:  $0.75

c.  Paperback - 10 days overdue:  $1.50

5.   Write a program to determine the letter grade for a student on a test given the number of points possible and earned. Your program should calculate their percent, to one decimal place, and use the following Grading Scale:

90-100% = A, 80-89% = B, 70-79% = C, 60-69% = D, 0-59% = F

Test Data and Output Form:

a.  65/80 = 81.3%, for a grade of B

b.  48/75 = 64.0%, for a grade of D

6.   Write a program to determine whether a student will be allowed to become a member of the school honor society. GPA and citizenship determine acceptance. Levels of acceptance are:

GPA >= 3.50 and Citizenship = Satisfactory à Guaranteed

GPA >= 3.50 and Citizenship = Unsatisfactory à Probational

GPA < 3.50 and Citizenship = Satisfactory à Waiting List

GPA < 3.50 and Citizenship = Unsatisfactory à Denied

Note:  Enter a single character for the citizenship, not the entire word.

Test Data:                                                         Output:

a.  GPA = 3.65, Citizenship = U                       Probational

b.  GPA = 3.42, Citizenship = S                       Waiting List

c.  GPA = 3.28, Citizenship = U                       Denied

d.  GPA = 3.77, Citizenship = S                       Guaranteed

 

7.   Write a program that asks for the temperature in degrees Fahrenheit (as an integer), and then suggests an appropriate activity for that weather. The ranges should be: 

Below 0º,  0º-20º,  21º-40º,  41º-60º,  61º-80º,  81º-100º,  Over 100º

For example: Input is 75.

Output:  “Hey, let’s put the top down and cruise the Pacific Coast Highway!”

8.   Write a program that determines the amount of commission that a real estate salesperson earns on the sale of a home. The commission is based on the amount of the sale and the following percentages:

Sale Price

Commission %

$0-$200,000

4.0%

$201,000 - $500,000

3.5%

$501,000 - $1,000,000

3.0%

Over $1,000,000

2.5%

Note: Since the home sale amounts are so large, enter them in numbers of thousands. For example: If a house was sold for $250,000, only enter 250 for the sale price. Output the amount of the commission that the salesperson would get in currency format.

Test Data:                                                                           Output:

a.  Sale Amount in Thousands: 250                                     $8750.00

b.  Sale Amount in Thousands: 185                                     $7400.00

c.  Sale Amount in Thousands: 1250                                   $31250.00

d.  Sale Amount in Thousands: 730                                     $21900.00

 

9.   Write a program that asks for your weight (in pounds) and then tells you your weight on another planet, based on your choice. The planets and their factors (which are multiplied by your Earth weight) are:

Venus:  0.78                 Mars:  0.38                  Jupiter:  2.64                Moon:  0.17

Mercury:  0.38             Saturn:  1.15                 Neptune:  1.18             Pluto:  0.05

Enter a single character for the planet, not the entire name. Use a “menu” of choices to let the user know what to input. Use your own weight for test data.

10. Write a program that will determine the number of grade points that you earn in a class, based on your letter grades. The program should ask you to enter your letter grade for the class. It will then ask whether that class is an AP class. Finally, the program will output the number of grade points earned for the class, based on the following:

                           A=4, B=3, C=2, D=1, F=0

However, if the class is an AP class, you add one grade point only if the grade is a C or higher. Therefore, D’s and F’s do not get the extra grade point in AP classes. See the examples given below. All input should be done with char variables. Also, you must use a switch/case to assign the grade points.

Use these example runs to test your program. Try all four to verify that it works for all possible outcomes.

Example 1

Enter letter grade: B

Is it an AP Class? (Y or N): N

Grade Points = 3

Example 2

Enter letter grade: B

Is it an AP Class? (Y or N): Y

Grade Points = 4

Example 3

Enter letter grade: D

Is it an AP Class? (Y or N): N

Grade Points = 1

Example 4

Enter letter grade: D

Is it an AP Class? (Y or N): Y

Grade Points = 1

11. Bingo is a game played all over the world by people of all ages. The game uses balls with letters and numbers on them (the letters are B, I, N, G and O. The numbers are 1-75). For example, one such ball is G-58. The letters on the balls correspond to the following:

1 – 15 = B
16 – 30 = I
31 – 45 = N
46 – 60 = G
61 – 75 = O

Write a program that will allow the use to enter a number from 1 to 75. The program should then display the appropriate letter as described above.

Sample run of the program:
Please enter a number from 1 to 75:  32
The BINGO letter for 32 is N

12. Below is a telephone keypad. Some businesses buy phone numbers that spell the name of the business or something about it. For example, there is a service for making collect phone calls, named 1 800 COLLECT (which is also the phone number of the company). According to the keypad below, that phone number would be 1800-265-5328. Write a program that will allow the use to enter a letter (use a char variable). The program should then display the appropriate number as described by the keypad.

keypad

Sample run of the program:

Please enter a letter from A to Z: G

The keypad number for G is 4



 






Home  •  About Me  •  Webgrades  •  Contact Me
© DanShuster.com