Introduction

An array is 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.

Task #1 Average Class

Create a class called Average according to the UML diagram. see image.

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

Methods:

  • Average - the constructor. It will allocate memory for the array. Use a for loop to repeatedly display a prompt for the user to enter score number 1, score number 2, etc. Note: The computer starts counting with 0, but people start counting with 1. For example, when the user enters score number 1, it will be stored in indexed variable 0.
  • calculateMean - this is a method that uses a for loop to access each score in the array and add it to a running total. The total divided by the number of scores (use the length of the array), and the result is stored into mean.
  • toString - returns a String containing data in descending order and the mean.
  • selectionSort - this method uses the selection sort algorithm to rearrange the data set from highest to lowest.

1. Copy Average.java and AverageDriver.java into NetBeans or other Java IDE tools. The AverageDriver class only contains the main method to declare and instantiate an Average object, and print the Average object information into the console.

2. Call the selectionSort and the calculateMean methods in the constructor of Average class.

3. Implement the calculateMean method.

4. Compile, debug, and run the program by right click the AverageDriver.java file and select "Run File" on the drop down menu.

Task #2 Arrays of Objects and File I/O

1. Copy the files Song.java , CompactDisc.java , and Classics.txt into NetBeans or other Java IDE tools. Song.java is complete and will not be edited. Classics.txt is the data file that will be used by CompactDisc.java. We have learned about data file path in Project for chapter 4.

2. Creating a new song with the title and artist and storing it in the appropriate position in the array.

3. Print the contents of the array to the console.

4. Compile, debug, and run. Your output should be as follows:

Contents of Classics
Ode to Joy by Bach
The Sleeping Beauty by Tchaikovsky
Lullaby by Brahms
Canon by Bach
Symphony No. 5 by Beethoven
The Blue Danube Waltz by Strauss

Task #3 File Input and Output

1. Read "FilePath.doc" to know where to put the Classics.txt.

2. The file input and output part is coded. Try to read through the code and understand the program.

3. Turn in output file "Result.txt" as part of the lab.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.