Lab #5 Arrays Lab Objectives Introduction Everyone is familiar with a list. We m
ID: 3554426 • Letter: L
Question
Lab #5
Arrays
Lab Objectives
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.
Task #1 Average Class
Create a class called Average according to the UML diagram.
Average
-data [ ] :int
-mean: double
+Average( ):
+calculateMean( ): void
+toString( ): String
+insertionSort( ): void
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:
Average
-data [ ] :int
-mean: double
+Average( ):
+calculateMean( ): void
+toString( ): String
+insertionSort( ): void
Explanation / Answer
Average Class:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Average {
int data[];
double mean;
Average( )
{
for(int i=0;i<5;i++)
data[i]=0;
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<5;i++)
{
System.out.println("Enter number "+(i+1)+":");
data[i]=Integer.parseInt(br.readLine());
}
calculateMean();
insertionSort();
}
catch(Exception e)
{
e.printStackTrace();
}
}
void calculateMean( )
{
int sum=0;
for(int i=0;i<data.length;i++)
sum=sum+data[i];
mean=sum/data.length;
}
void insertionSort( )
{
int temp;
for(int i=0;i<data.length;i++)
{
temp=data[i];
int j;
for(j=i-1;j>=0&&temp<data[j];j--)
data[j+1]=data[j];
data[j+1]=temp;
}
}
@Override
public String toString() {
return "Average [data=" + Arrays.toString(data) + ", mean=" + mean
+ "]";
}
}
import java.io.IOException;
AverageDriver Class:
public class AverageDriver {
public static void main(String args[])
{
Average a=new Average();
System.out.print("Numbers: ");
for(int i=4;i>=0;i--)
System.out.print(a.data[i]+" ");
System.out.println("Mean: "+a.mean);
}
}
CompactDisc Class:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
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, called cd, of size 6
Song cd[] = null;
for(int i=0;i<6;i++)
cd[i]=null;
for (int i = 0; i < cd.length; i++)
{
title = input.readLine();
artist = input.readLine();
// fill the array by creating a new song with
// the title and artist and storing it in the
// appropriate position in the array
cd[i]=new Song(title,artist);
}
System.out.println("Contents of Classics:");
for (int i = 0; i < cd.length; i++)
{
//print the contents of the array to the console
cd[i].toString();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.