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


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,

Your change is $47.68
------------------------------
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,

Your change is $62.15
------------------------------
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.

Here is your grade breakdown:

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

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 and divide two fractions. The user will enter integers for the numerator and denominator of each fraction, as shown. They will then enter the choice of operation (+, -, * or /) as a character. Do not worry about reducing fractions for this problem. The program will then display the result as shown in the example below.

  Sample run of the program

Enter the numerator 1: 2

Enter the denominator 1: 3

Enter the numerator 2: 1

Enter the denominator 2: 2

Enter the operation (+, -, *, /): +

2/3 + 1/2 = 7/6

Try all of these:
Op Result
+
2/3 + 1/2 = 7/6
-
2/3 - 1/2 = 1/6
*
2/3 * 1/2 = 2/6
/
2/3 / 1/2 = 4/3

8.  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
Add'l hrs $0.25/hr Add'l hrs $0.75/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

9.  Write a program that will determine the day of the week for a particular date. The program should accept the date of the day in the month, such as 9 for July 9. It should then ask the day of the week for the first day of the month, as shown below. Further test your program using a calendar (any month/year will do). For the day of the week, use a number as follows:

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

Sample run of the program

What is the number of the date?(1-31): 9

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

Day 9 of the month is on a Thursday.

10. THIS PROGRAM REQUIRES THE STUDENT CLASS: Modify the Student Class by adding a method called getHighScore() that will determine and return the highest score of the three tests.
11. 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
Letter Grade: B

12. THIS PROGRAM REQUIRES THE ACCOUNT CLASS: Make an interface for the Account Class that will serve as an ATM machine. The program should begin with a request for the password. Give the user 1 chance to enter the correct password. If they are not successful , the program should terminate. If the password is entered correctly, a menu of options should be displayed as follows:

  1. Make Withdrawal
  2. Make Deposit
  3. Check Balance
  4. Quit

Only one transaction may be made during one visit. The user’s account balance should be displayed after the transaction is completed.



 






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