• Home
• Contact Me
•
Aeries Portal
• Royal High School
• Simi Valley USD

AP Statistics

• Course Calendar
• Chapter Notes
• Practice Tests

AP Comp-Sci
Course Content
• Programs
• Practice Tests

CP Statistics
• Course Calendar
• Chapter Notes
• Practice Tests

Algebra
Geometry

Math Power

AP Computer Science Unit 2 Programs
Conditional Statements

1.   The date for any Easter Sunday from 1982 to 2048 can be computed as follows:

Let Y be the 4-digit year for which it is desired to compute Easter Sunday.
Let A be the remainder of the division of Y by 19.
Let B be the remainder of the division of Y by 4.
Let C be the remainder of the division of Y by 7.
Let D be the remainder of the division of (19A + 24) by 30.
Let E be the remainder of the division of (2B + 4C + 6D + 5) by 7.

The date for Easter Sunday is March (22 + D + E).

NOTE: This will sometimes give a date in April. When? You figure it out!

Input : Ask for any year from 1982 to 2048.
Output : Print out “ The date for Easter Sunday, ???? is March ?? (or April ??)."

Test Data:

a) The date for Easter Sunday, 1989 is March 26
b) The date for Easter Sunday, 1999 is April 4

2.  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) Input: Pay Rate = \$12.43 Hours = 43 Output: Regular Pay = \$497.20 Overtime Pay = \$55.94 Total Pay = \$553.14 b) Input: Pay Rate = \$13.81 Hours = 37 Output: Regular Pay = \$510.97 Overtime Pay = \$0.00 Total Pay = \$510.97

3.   Write a program to simulate a cash register change calculator. Allow the user to enter a purchase price of up to 100 dollars. Then determine the best change to give back for a \$100 bill. Print the results to the screen as below, with no zero results displayed. Note: ignore the existence of half-dollars, \$2 bills and \$50 bills.

a) For a purchase price of \$52.32 and a \$100 bill tendered,

------------------------------
2 \$20 bill(s)
1 \$5 bill(s)
2 \$1 bill(s)
2 Quarter(s)
1 Dime(s)
1 Nickel(s)
3 Penny(ies)

b) For a purchase price of \$37.85 and a \$100 bill tendered,

------------------------------
3 \$20 bill(s)
2 \$1 bill(s)
1 Dime(s)
1 Nickel(s)

4.   Modify program 1-4 (Unit 1, Problem 4) so that it will print out a letter grade based on the overall percentage and the grading scale below.

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

Since the percentages are expressed as decimals, keep in mind that (80 - 89) means scores up to, but not including 90. So 89.7 is still a “B”. This is true for all of the above intervals. Try several sets of your own test data and output as follows.

Homework (95/120) = 79.2%
Tests (175/200) = 87.5%
Overall (270/320) = 84.4%

5.   Write a program that accepts the integer lengths of the 3 sides of a triangle and outputs whether that triangle is scalene, isosceles or equilateral. If the triangle is not possible, state that instead. Recall that for a triangle to be possible, the sum of any two of its sides must be greater than the 3rd side.

Test Data:

a) 3, 4, 5 = scalene
b) 3, 4, 3 = isosceles
c) 3, 3, 3 = equilateral
d) 3, 3, 8 = not possible

6.   Write a program that will determine the amount of an electric bill from the number of kilowatt-hours (kWh) used. The electric company charges its customers \$.0577 per kWh for the first 1000 kWh used, \$.0532 for the next 4000 kWh, and \$.0511 for any kWh over 5000. Use constants for these values.

Level
a) Input: 984 kWh
b) Input: 2984 kWh
c) Input: 5984 kWh
1
984 kWh = \$56.78
1000 kWh = \$57.70
1000 kWh = \$57.70
2
0 kWh = \$0.00
1984 kWh = \$105.55
4000 kWh = \$212.80
3
0 kWh = \$0.00
0 kWh = \$0.00
984 kWh = \$50.28
Total
\$56.78
\$163.25
\$320.78

7.  Write a program that will add, subtract, multiply or divide two integers. The user will enter two integers, as shown. They will then enter the choice of operation (+, -, * or /) as a character. The program will then display the result as shown in the example below.

Sample run of the program:

Enter integer 1: 2
Enter integer 2: 5
Enter the operation (+, -, *, /): -

2 - 5 = -3

8. 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. If they enter a number greater than 75, display an error message. See

Sample Run of program - try (a), (b) and (c) - input and output values in bold:

(a)
Enter a number from 1 to 75: 32

The BINGO letter for 32 is N

(b)
Enter a number from 1 to 75: 52

The BINGO letter for 52 is G

(c)
Enter a number from 1 to 75: 82

ERROR - BINGO numbers are from 1-75

9. 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 1-800-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.

Sample Run of program - try both (a) and (b) - input and output values in bold:

(a)
Enter a capital letter from A to Z: G

The keypad number for G is 4

(b)
Enter a capital letter from A to Z: X

The keypad number for X is 9

10.  Write a program to determine the cost of parking at a pay lot based on the type of vehicle and number of hours parked. The parking lot charges the following rates:

Cars Trucks Buses
First 2 hrs Free First 1 hr Free First 1 hr Free
Next 3 hrs \$0.50/hr Next 2 hrs \$1.00/hr Add'l hrs \$10.00/hr

The user will input a 1-character designator (C,T or B) and the number of minutes the vehicle has been in the lot. The program should then compute the appropriate charge and display the result. Note: any part of an hour is considered a full hour.

Test Data:

a) C, 250 = \$1.50
b) T, 250 = \$3.50
c) B, 250 = \$40.00

11.  Simi Valley Unified employees are paid on the last working day of each month. Working days are Monday through Friday, so if the last day of the month falls on a weekend, then payday is on the preceding Friday. Let's look at September as an example. September has 30 days in it, so if the 30th is on Saturday, payday is on Friday the 29th. If the 30th is on Sunday, then payday is on Friday the 28th. Write a program that will ask for the month (as an integer, 1-12) and the day of the week for the 1st of the month and then determine the date of payday for that month. For the day of the week, use the following numeric code:

Mon=1, Tue=2, Wed=3, Thu=4 , Fri=5, Sat=6, Sun=7

For months, use 1=January, 2=February, etc...

Hint: there are two key aspects to this program - 1) knowing how many days there are in each month and 2) use the fact that there are 7 days in a week (think modulus) .

Sample runs of the program (try them all):

What month is it? (1-12): 9

What day was the first day of the month?(1-7) : 1

Payday is on Tuesday, September 30,

What month is it? (1-12): 5

What day was the first day of the month?(1-7) : 5

Payday is on Friday, May 29

What month is it? (1-12): 2

What day was the first day of the month?(1-7) : 1

Payday is on Friday, February 26

12. THIS PROGRAM REQUIRES THE STUDENT CLASS: Modify the Student Class to include a letter grade based on the average test score. Use the basic grading scale: 0-59 = F, 60-69 = D, 70-79 = C, 80-89 = B, 90-100 = A. This will require an additional private data member for the grade (a char). It will also require you to modify all existing constructors. Finally, you will need to create a new method called getGrade() to determine the letter grade from the test score average (see getAverage() for an example. You will then need to modify your client program so that it can handle this new addition to the class - remember to update the toString() method. See the example below for output format.

Sample Run of the Program (input and output values in bold)

Enter student name: Kate
Enter test score 1: 84
Enter test score 2: 88
Enter test score 3: 86

Student: Kate
Test 1: 84
Test 2: 88
Test 3: 86
Average Score: 86