These projects were created by Leigh Anne Fitz and Lisa Ferran to provide engaging, standards-aligned learning experiences for AP Computer Science A students.
Now that youβve learned how to create your own classes, itβs time to work with collections of data using one-dimensional arrays. In these projects, youβll learn how to store, access, and manipulate multiple values efficiently. Youβll practice traversing arrays, processing their elements with loops, and developing algorithms to search for information, calculate results, and modify data.
Each project is designed to reinforce these concepts through authentic programming tasks. As you complete them, focus on using loops effectively, paying close attention to array indices and boundaries, and breaking complex problems into manageable steps. Mastering one-dimensional arrays is an essential skill that prepares you for more advanced data structures and algorithms.
Subsection4.71.1Review - Reading a Text File into an Instance Variable
If you have an array instance variable that is to be filled with data from a text file automatically when an object is created, the best place to do this is in the constructor. For example, the file students.txt contains:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class StudentList
{
private String[] students;
public StudentList() throws IOException
{
Scanner input = new Scanner(new File("students.txt"));
students = new String[5];
for (int i = 0; i < students.length; i++)
{
students[i] = input.nextLine();
}
input.close();
}
}
The students array is created but remains empty until a method is called.
Think about when the constructor runs. Does the constructor contain code that fills the array?
The constructor automatically reads the five names from the file and stores them in the students array.
The constructor runs automatically when the object is created. It creates the array, reads each line from the file, and stores the names in the instance variable.
The students array is created inside the constructor and is destroyed when the constructor finishes.
Think about the difference between a local variable and an instance variable. Where is students declared?
The program reads the file only when students[i] is accessed later.
Look at the for loop. When does input.nextLine() actually execute? Does the program wait until an array element is accessed?
Write a program that will grade a multiple choice test. This program will contain two classes: Main and UnitTest. You are to code the UnitTest class first as described below:
The studentAns variable should copy all of the elements from the parameter into itself in the same order as the parameter. You will need to make sure each element is changed to uppercase as it is being copied into the array.
This method will return the number of correctly answered questions.
totalMistakes( )
This method will return the number of incorrect answers.
isPassing( )
This method will return true if the student passed the test, or false if the student failed. A student must correctly answer 14 out of 20 questions in order to pass the test.
toString( )
This method will return a display of the correct answers and the student answers. It must be in a side by side, 2 column format, with item numbers. See format below.
Example output from the toString method (remember to return the String, not print it):
ANSWER KEY Student's Answers
1) B 1) A
2) D 2) A
3) C 3) C
. .
. .
. .
20) E 20) A
Hint for the toString method: Create a String and initialize it to the first line of the output using a tab between the phrases instead of spaces (\t). Create another String for the rest of your output, initializing it to the empty String. Use a for loop to concatenate onto that string the problem number (which should be one more than the index number) and the String from each array at that index with tabs between the correct answer and the studentβs answer (two tabs should do it - \t\t). Return the first String, followed by a new line (\n), followed by the second String.
Use a for loop to allow the user to enter their answers one at a time until they have entered all 20 answers. Store these into the answerArray as they enter them. It should not matter if they enter upper or lower case letters.
Write a program about sales data. This program will contain two classes: Main and SalesData. You are to code the SalesData class first as described below:
Creates the sales array the same size as that parameter and copies all of the elements of the parameter array into the sales array.
Methods
getTotal( )
This method returns the sum of the elements in the sales array.
getAverage( )
This method returns the average of the elements in the sales array.
getHighest( )
This method returns the highest value stored in the sales array.
getLowest( )
This method returns the lowest value stored in the sales array.
toString( )
This method returns a String representation of the values stored in the sales array, one per line. Make sure the values are formatted to two decimal places and have a $ in front of the values.
Once the SalesData class is complete, you are to complete the Main class to test your SalesData class as follows:
Use a for loop to fill the array with input from the user. Include a data verification while loop to ensure that the user is entering values greater than or equal to zero.