• 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 4 Programs
Data Structures: Arrays, Strings and ArrayLists

 Working with Text Files Jump to: 1D Arrays 2D Arrays Strings ArrayLists

1. Write a program that will read an unknown number of single-digit integers from a text file numbers.txt. The numbers should be displayed on the screen and the summary statistics determined and displayed as shown below.

N=15
Sum = 87
Average = 5.8
High = 9
Low = 2

2. Write a program that reads an unknown quantity of x and y coordinates from a file. The text file is called points.txt. The program should count and display the number of points that lie each Quadrant (1-4) – output as shown below. Recall that in Quadrant 1, both the x and y coordinates are positive; in Quadrant 2, x is negative, y is positive; in Q3, both x and y are negative and in Q4, x is positive and y is negative.

Q1 – 5
Q2 – 3
Q3 – 3
Q4 - 4

3. Write a program that reads bowling scores for a team of four bowlers from a file called bowlers.txt. Each bowler bowls 3 games. The format of the contents of the file is as follows: Snoopy 142 182 191 Garfield 165 185 196 Etc… The output should provide each bowler’s total scores for the three games as well as which bowler had the highest total, as shown below:

File Format:

Snoopy
142
182
191
Garfield
165
185
196
etc...

Output Format:

Snoopy 515
Garfield 546
Tweety 472
Rocko 489

The winner is Garfield

4. Write a program that reads data for a basketball game (with an unknown number of players) from a file. The text file is called bsktbll.txt. The format of the file consists of the player’s name on one line and the number of 1-point, 2-point and 3-point shots that player made on the next line – as shown below. For example, Rice made three 1-point shots, ten 2-point shots and two 3-point shots for a total of 29 points.

File Format:

Rice
3 10 2
Reid
1 1 0
O'Neal
5 11 0
etc...

The output should provide each player’s total points scored and the total points scored by the entire team.

Output Format:

Rice 29
Reid 3
O'Neal 27
etc…

Team total is 101

5. What Day Were You Born On? Write a program that will determine the day of the week that someone was born on, based on his or her numerical birth date. The input is the numerical month, day and two-digit year (in 1900’s) and the program will use the algorithm below to find the day of the week they were born.

1. Let the month, date and year be m, d and y respectively
2. Let A = y + y / 4
3. Let B = A % 7
4. Let C = B + d
5. Let E = C + Month Code (based on m, as shown in table)

 m 1 2 3 4 5 6 7 8 9 10 11 12 Code 1 4 4 0 2 5 0 3 6 1 4 6

6. Let F = E % 7
7. If F=0 then Day = Saturday, F=1 (Sunday), F=2 (Monday), etc…

Note: There is one exception to this algorithm. If a person is born in January or February of a leap year, you need to subtract 1 from E before going to step 6. Leap years are those years evenly divisible by 4, such as 1988 or 2004.

Your task: write a program that reads names and birth dates from a file called birthday.txt stored in the txtfiles folder on the H drive.

The data will be stored in the following format:

Bob Douglas
9 15 86
Andrew Tai
2 6 86
etc…

Output the results as follows:

Bob Douglas was born on a Tuesday.
Andrew Tai was born on a Thursday.
etc…

6.