Header

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

divider

AP Statistics

• Course Calendar
• Chapter Notes
• Practice Tests
• Statistics Links

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

divider

CP Statistics
• Course Calendar
• Chapter Notes
• Practice Tests
• Statistics Links


divider

Algebra
• Table of Contents
Geometry
• Table of Contents

Math Power
• Table of Contents

Computer Programming Unit 4 Programs
The String Class

1. Write a program that asks you to enter a word, then displays that word in reverse order. See output format below.

Sample Run of program:

Enter a word: stressed

STRESSED BACKWARDS IS DESSERTS

2. You and a friend are eating at a restaurant that is offering a "buy one meal, get one meal free of equal or lesser value." What this means is that you pay for the the more expensive meal, and the less expensive meal is free. If both are the same price, then either one of them is free. You will write a program that will ask for the price of the two meals and then figure out the price of the meal, with tax (7%) and a tip (15%). See output format below.

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

(a)
Enter price of meal #1: 14.99
Enter price of meal #2: 12.99

Your bill:
--------------
Meal  = $14.99
Tax   = $ 1.05

Tip   = $ 2.25
--------------

Total = $18.29

(b)
Enter price of meal #1: 8.75
Enter price of meal #2: 9.75

Your bill:
--------------
Meal  = $ 9.75
Tax   = $ 0.68

Tip   = $ 1.46
--------------

Total = $11.90

3. 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 of a package (length, width and height). Then calculate the girth (girth = 2*height + 2*width). Then calculate the combined length and girth (length + girth). Finally, determine and output whether the package is acceptable or not acceptable. See output format below.

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

(a)
Enter the length: 17.5
Enter the width: 14
Enter the height: 8

Combined length and girth = 61.5 - package acceptable

(b)
Enter the length: 36
Enter the width: 22
Enter the height: 12

Combined length and girth = 104 - package NOT acceptable

4. Write a program that will accept coordinates for two points in the plane. Then calculate the slope betwen those points. Recall that for two points (x1, y1) and (x2, y2), the slope is calculated as:

slope formula

However, if the denominator is 0 (which only happens when x1 and x2 are the equal), the slope is said to be undefined as dividing by zero is not defined in mathematics. So the program needs to determine if it is possible to calculate the slope (denominator is not 0). If it is possible, calculate and display. otherwise, display UNDEFINED. See output format below.

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

(a)
Enter x1: 1
Enter y1: -3
Enter x2: -4
Enter y2: 5


Slope = -1.6

(b)
Enter x1: 2
Enter y1: 3
Enter x2: 2
Enter y2: 5


Slope = UNDEFINED

5. Write a program that will ask the user for an amount of a purchase then determine the discounted sale price, based on the following:

  • If the amount of the purchase is less than or equal to $50.00, they receive a 10% discount
  • If the amount of the purchase is more than $50.00, they receive a 20% discount

Use a sales tax rate of 7%. See output format below.

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

(a)
Enter the purchase price: 44.50

Original Price : $ 44.50
Discount = 10% : $  4.45
Sale Price     : $ 40.05
Sales Tax      : $  2.80
Final Price    : $ 42.85

(b)
Enter the purchase price: 64.88

Original Price : $ 64.88
Discount = 20% : $ 12.98
Sale Price     : $ 51.90
Sales Tax      : $  3.63
Final Price    : $ 55.53

6. Write a program to determine a person's paycheck based on their pay rate and hours worked. Assume they are paid their regular pay rate for all hours up to 40. For all hours over 40 (overtime hours), they are paid time-and-a-half, which means they are paid 1.5 times their normal pay rate for those hours. For example, in Test Data (a) below, the person is paid $12.43 per hour for the first 40 hours and then $12.43 * 1.5 = $18.65 for the 3 hours of overtime. See output format below. You will need to use EasyFormat to ensure that output lines up properly.

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

(a)
Enter hours worked: 43
Enter pay rate: 12.43

Hours Worked:
Regular Hours  = 40
Overtime Hours =  3

Total Hours    = 43

Itemized Pay:
Regular Pay    = $497.20

Overtime Pay   = $ 55.94
Total Pay      = $553.14

(b)
Enter hours worked: 36
Enter pay rate: 13.81

Hours Worked:
Regular Hours  = 36
Overtime Hours =  0
Total Hours    = 36

Itemized Pay:
Regular Pay    = $497.16

Overtime Pay   = $  0.00
Total Pay      = $497.16

7. Write a program to simulate the activity at a gas pump. The user will enter the amount of gas they want in dollars, then choose the type of gas they want (regular, midgrade or premium) from a menu. The program will then determine and display the number of gallons pumped. rounded to 3 decimal places.

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

(a)
Enter the dollar amount of gas you wish to pump:20

Type of fuel:
1. Regular - $3.29 per gallon
2. Midgrade - $3.39 per gallon
3. Premium - $3.49 per gallon

Enter the number of the type of gas you wish to pump: 3

You will be able to pump 5.731 gallons

(b)
Enter the dollar amount of gas you wish to pump: 35

Type of fuel:
1. Regular - $3.29 per gallon
2. Midgrade - $3.39 per gallon
3. Premium - $3.49 per gallon

Enter the number of the type of gas you wish to pump: 1

You will be able to pump 10.638 gallons

(c)
Enter the dollar amount of gas you wish to pump: 50

Type of fuel:
1. Regular - $3.29 per gallon
2. Midgrade - $3.39 per gallon
3. Premium - $3.49 per gallon

Enter the number of the type of gas you wish to pump: 2

You will be able to pump 14.749 gallons

8. Write a program to calculate library fines for overdue books. The user will enter the type of book (choosing from a menu) and the number of days it is overdue. The program will then determine and display the total fine.

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

(a)
Type of book:
1. Paperback - $0.15 per day
2. Hardcover - $0.20 per day
3. Magazine  - $0.25 per day

Enter the number of the type of book: 2

Enter the number of days book is overdue: 7

Your fine is $1.40

(b)
Type of book:
1. Paperback - $0.15 per day
2. Hardcover - $0.20 per day
3. Magazine  - $0.25 per day

Enter the number of the type of book: 3

Enter the number of days book is overdue: 3

Your fine is $0.75

(c)
Type of book:
1. Paperback - $0.15 per day
2. Hardcover - $0.20 per day
3. Magazine  - $0.25 per day

Enter the number of the type of book: 1

Enter the number of days book is overdue: 10

Your fine is $1.50

9. Write a program to determine the amount of income tax for a person based on their annual income. Your program should ask for their annual income, then calculate and display their income tax based on the following tax table:

  Annual Salary        Income Tax Rate
-----------------     -----------------
     $0 – $10,000           3.2%
$10,001 – $20,000           5.5%
$20,001 – $40,000          10.8%
$40,001 and up             15.7%

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

(a)
Enter your annual income: 10500

Your income tax rate is: 5.5%
Your income tax amount is: $557.50

(b)
Enter your annual income: 48800

Your income tax rate is: 15.7%
Your income tax amount is: $7661.60

10. 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 then calculate their percent and determine a letter grade using the following grading scale:

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

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

(a)
Enter the value of the test: 80
Enter the student's points: 65

65/80 = 81.3% for a grade of B

(b)
Enter the value of the test: 75
Enter the student's points: 48

48/75 = 64.0% for a grade of D

11. Write a program to determine whether you will be accepted to a college based on your high school GPA and your SAT score. Use the information below for reference.

Student has GPA >= 3.50 and SAT >= 2000 results in GUARANTEED acceptance
Student has GPA >= 3.50 and SAT < 2000 results in PROBATIONAL acceptance
Student has GPA < 3.50 and SAT >= 2000 results in WAITING LIST acceptance
Student has GPA < 3.50 and SAT < 2000 results in DENIED acceptance

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

(a)
Enter your high school GPA: 3.75
Enter your SAT score: 1950

Acceptance = PROBATIONAL

(b)
Enter your high school GPA: 2.68
Enter your SAT score: 1800

Acceptance = DENIED

12. 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

13. 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.

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




 






Home  •  About Me  •  Aeries Portal  •  Contact Me
© DanShuster.com