Question: Part one Chapter 7 Lab Arrays Lab Objectives Be able to declare and instantiate arrays Be able to fill an array using a for loop

Part one

Chapter 7 Lab Arrays Lab Objectives Be able to declare and instantiate arrays Be able to fill an array using a for 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 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. 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 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

Average.java

import java.util.Scanner;

public class Average { private int [] data; private double mean; public Average () // this is the constructor { data = new int [5]; Scanner keyboard = new Scanner (System.in); for (int i = 0; i < data.length; i++) { System.out.print("Enter score number " + (i+1) + ": "); data[i] = keyboard.nextInt(); } // Task #1 step 2: To do - call method selectionSort and calculateMean here } public void selectionSort() { int maxIndex; int maxValue; for (int startScan = 0; startScan < data.length-1; startScan++) { maxIndex = startScan; maxValue = data[startScan]; for (int index = startScan + 1; index < data.length; index++) { if (data[index] > maxValue) { maxValue = data[index]; maxIndex = index; } } data[maxIndex] = data[startScan]; data[startScan] = maxValue; } } public void calculateMean() { // Task #1 step 3: To do - Caluculate Mean of the data // using defined class fields data and mean } public String toString() { String output; output ="The test scores in descending order are "; for (int i = 0; i < data.length; i++) { output = output + data[i] + " "; } output = output + " The average is " + mean; return output; } }

AverageDriver.java:

public class AverageDriver { public static void main(String [] args) { Average testScore = new Average(); System.out.println(testScore); } }

Classic.txt:

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

CompactDisc:

/**This program creates a list of songs for a CD by reading from a file*/ import java.io.*;

public class CompactDisc { public static void main(String [] args) throws IOException { FileReader file = new FileReader("Classics.txt"); BufferedReader input = new BufferedReader(file); String title; String artist; //Declare an array of songs of size 6 Song [] cd = new Song [6]; for (int i = 0; i < cd.length; i++) { title = input.readLine(); artist = input.readLine(); // Task #2 step 2 - creating a new song with // the title and artist, and store it into the array, like // cd[i] = new Song(title, artist); } System.out.println("Contents of Classics:"); for (int i = 0; i < cd.length; i++) { // Task #2 step 3 - print the contents of the array to the console // using cd[i].toString() to get the contents string } } }

Song.java

/*This program represents a song*/ public class Song { /**The title of the song*/ private String title; /**The artist who sings the song*/ private String artist;

/**constructor @param title The title of the song @param artist The artist who sings the song */ public Song(String title, String artist) { this.title = title; this.artist = artist; }

/**toString method returns a description of the song @return a String containing the name of the song and the artist */ public String toString() { return title + " by " + artist + " "; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!