These are the required files for this assignment:
- CompactDisc.java, “Driver” program for Task #3
- classics.txt, Data file for Task #3
- Song.java, Class definition for the Song class.
Upload the following files:
- Student.java, Your definition of the Student class.
- GradeDriver.java, “Driver” program for Tasks #1 and #2
- CompactDisc.java, Your modified version.
Copyright© 2022, Dallas College. Page 1 of 3
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 create and 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 even though there may be
many items on the list, we call the list by one name. That is the idea of an
array, one name for a list of related items. In this lab, we will work with lists
in the form of an array.
This work will begin simply 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 on to more complicated arrays,
arrays that contain objects.
Task #1 Student Class
Create a class named Student according to the following UML diagram.
Student
-grades[] : int
-average : double
+Student():
+calculateAverage() : void
+toString() : String
+selectionSort() : void
This class will allow a user to enter five (5) test grades into an array. It will
then arrange the test grade values in descending order and calculate the
average value (arithmetic mean) of the test grades. Create this class in its
own file named Student.java.
Attributes:
• grades[] – the array which will contain the test grades.
• average – the arithmetic mean of the test grades.
Copyright © 2022, Dallas College. Page 2 of 3
Methods:
• Student – the constructor. This method will allocate memory for the
array of test grades. Use a for loop to repeatedly display a prompt to
the user which will indicate that the user should enter test grade 1,
test grade 2, etc. Remember, the computer starts counting at 0, but
people start counting at 1. Your prompts should account for this. For
example, when the user enters test grade number 1, it will be stored
in the array element indexed by 0. The constructor will then call the
selectionSort and calculateAverage methods.
• calculateAverage – this method will use a for loop to access each
test grade in the array and add it to a running total. The total will be
divided by the number of test grades (use the length of the array),
and the result will be stored in the instance field named average.
• toString – returns a String containing test grades in descending
order and the average.
• selectionSort – this method will use the selection sort algorithm to
arrange the elements in the test grades array in descending order.
Descending order means highest to lowest.
Task #2 Grade Driver
1. Create a GradeDriver class in its own file named GradeDriver.java.
This class will only contain the main method. The main method will
declare and instantiate an object of the Student class. Instantiate
means to create an object that is an instance of a class. The main
method will then call the object’s toString method by displaying the
object with a System.out.println statement. This will display the
object’s information to the console. For example:
System.out.println(myStudent.toString());
2. Compile, debug, and run the GradeDriver program. It should prompt
for the five test grade values and then display them in descending
order followed by the average test grade value. Compare the
computer’s output to your hand calculations using a calculator. Do not
continue until your program is running correctly.
Task #4 Array of Objects
1. Copy the files Song.java, CompactDisc.java, and Classics.txt from
the Blackboard course. Song.java is a class definition, it is complete
and will not need to be edited. Classics.txt is the data file that will
be used by CompactDisc.java, the file you will be editing.
Copyright © 2022, Dallas College. Page 3 of 3
2. In CompactDisc.java, there are comments indicating where the
missing code is to be placed. Declare an array of Songs, called cd, with
a size of 6.
3. Fill the array by creating a new song with the title and artist and
storing it in the appropriate position in the array.
4. Print the contents of the array to the console.
5. 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
Notes:
• The Selection Sort algorithm is shown on pages 465 and 466 of our
textbook. You must modify this code to sort in descending order.
• The toString() method is intended to build a string representing the
current state of an instance of your class, reporting on the most
important fields of data stored in that object.
• When you submit a Java source code file that you have created or
modified, include a comment containing your full name.
• Submit the Java source code files for the programs you created or
modified. For this assignment, that means:
o Student.java,
o GradeDriver.java, and
o CompactDisc.java.
- Lab Assignment 4
Arrays
Lab Objectives
Introduction
Task #1 Student Class
Task #2 Grade Driver
Task #4 Array of Objects
Notes:
Ode to Joy
Bach
The Sleeping Beauty
Tchaikovsky
Lullaby
Brahms
Canon
Bach
Symphony No. 5
Beethoven
The Blue Danube Waltz
Strauss
import java.io.*;
/**
This program creates a list of songs for a CD
by reading from a file.
*/
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;
// ADD LINES FOR TASK #3 HERE
// Declare an array of Song objects, called cd,
// with a size of 6
for (int i = 0; i < cd.length; i++)
{
title = input.readLine();
artist = input.readLine();
// ADD LINES FOR TASK #3 HERE
// Fill the array by creating a new song with
// the title and artist and storing it in the
// appropriate position in the array
}
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
}
}
}
/*
This class stores data about a song.
*/
public class Song
{
private String title; // The song’s title
private String artist; // The song’s artist
/**
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.
*/
public Song(String title, String artist)
{
this.title = title;
this.artist = artist;
}
/**
The toString method
@return A String object containing the name
of the song and the artist.
*/
public String toString()
{
return title + ” by ” + artist + “\n”;
}
}