Lab 06 Spring 2021
Lab 06 Spring 2021
Chapter7: Arrays
Lab Objectives
• Be able to declare and instantiate arrays
• Be able to fill an array using a loop
• Be able to access and process data in an array
• Be able to write a sorting method
• Be able to use an array of objects
Introduction
Everyone is familiar with a list. We make shopping lists, to-do lists, assignment lists,
birthday lists, etc. Notice that though there may be many items on the list, we call the list
by one name. That is the idea of the array, one name for a list of related items. In this lab,
we will work with lists in the form of an array.
It will start out simple with a list of numbers. We will learn how to process the contents of
an array. We will also explore sorting algorithms, using the selection sort. We will then
move onto more complicated arrays, arrays that contain objects.
This class will allow a user to enter 5 scores into an array. It will then rearrange the data in
descending order and calculate the mean for the data set.
Attributes:
• data[]— the array which will contain the scores
• mean — the arithmetic average of the scores
/**
Constructor
@param title A reference to a String object
containing the song's title.
@param artist A reference to a String object
containing the song's artist.
*/
/**
The toString method
@return A String object containing the name
of the song and the artist.
*/
Copyright © 2019 Pearson Education, Inc., Hoboken NJ
3
public String toString()
{
return title + " by " + artist + "\n";
}
}
/**
This program creates a list of songs for a CD
by reading from a file. */
System.out.println("Contents of Classics:");
for (int i = 0; i < cd.length; i++)
{
// ADD LINES FOR TASK #3 HERE
// Print the contents of the array to the console
}
}
}