Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is completed using Arrays, re-do the code. Only this time, using ArrayLists

ID: 3855616 • Letter: T

Question

This is completed using Arrays, re-do the code. Only this time, using ArrayLists instead.

public class Average
{
// an array to hold 5 interger values
private int[] data = new int[5];

// the average of the data in the array
private double mean;

/**
* This constructor sets up the array and calls the
* selectionSort and calculateMean methods
*/
public Average()
{
Scanner keyboard = new Scanner(System.in);

for (int i = 0; i < data.length; i++)
{
System.out.println("Please enter an integer value: ");
data[i] = keyboard.nextInt();
}
  
selectionSort();
calculateMean();
}

/**
* This method calculates the mean of the data stored in the array
*/
public void calculateMean()
{
//Put your code here for calculating the mean of all the
//elements in the data array.
int i, j = 0;
  
for (i = 0; i< data.length; i++)
{
j = j + data[i];
}
mean = (double)j/(data.length);
}

/**
* This method returns the data in the array in descending order and the
* mean
*
* @return value stored in b and mean
*/
public String toString()
{
String b = " ";
for (int i = 0; i < data.length; ++i)
{
b += " " + data[i];
}
return "The numbers in the array are:" + b + " and the mean is: " + mean;
}

/**
* This method sorts data in array from highest to lowest
*/
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 class AverageDriver
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{

Average arr = new Average(); // create average object
  
// prompts user to enter values into array,
// then prints values and mean
  
System.out.println(arr.toString());
}
}

*************************************************************************************

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, called cd, of size 6
Song[] cd = new Song[6];
  
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
System.out.println(cd[i].toString()+" ");
}
}
}

***************************************************************************

public class Song
{
private String title; //The title of the song
private String 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;
}
}

Explanation / Answer

AverageDriver.java


public class AverageDriver
{

public static void main(String[] args)
{
Average arr = new Average(); // create average object
  
// prompts user to enter values into array list,
// then prints values and mean
  
  
System.out.println(arr.toString());
}
}

Average.java

import java.util.ArrayList;

import java.util.Scanner;

public class Average

{

// an array to hold 5 interger values

private ArrayList<Integer> data = new ArrayList<Integer>();

// the average of the data in the array

private double mean;

/**

* This constructor sets up the array and calls the

* selectionSort and calculateMean methods

*/

public Average()

{

Scanner keyboard = new Scanner(System.in);

System.out.println("Please enter an integer value(-1 to quit): ");

int n = keyboard.nextInt();

while( n != -1)

{

data.add(n);

System.out.println("Please enter an integer value(-1 to quit): ");

n = keyboard.nextInt();

}

  

selectionSort();

calculateMean();

}

/**

* This method calculates the mean of the data stored in the array

*/

public void calculateMean()

{

//Put your code here for calculating the mean of all the

//elements in the data array.

int i, j = 0;

  

for (i = 0; i< data.size(); i++)

{

j = j + data.get(i);

}

mean = (double)j/(data.size());

}

/**

* This method returns the data in the array in descending order and the

* mean

*

* @return value stored in b and mean

*/

public String toString()

{

String b = " ";

for (int i = 0; i < data.size(); ++i)

{

b += " " + data.get(i);

}

return "The numbers in the array are:" + b + " and the mean is: " + mean;

}

/**

* This method sorts data in array from highest to lowest

*/

public void selectionSort()

{

int maxIndex;

int maxValue;

for (int startScan = 0; startScan < data.size() - 1; startScan++)

{

maxIndex = startScan;

maxValue = data.get(startScan);

for (int index = startScan + 1; index < data.size(); index++)

{

if (data.get(index) > maxValue)

{

maxValue = data.get(index);

maxIndex = index;

}

}

data.set(maxIndex , data.get(startScan));

data.set(startScan , maxValue);

}

}

}

Output:

Please enter an integer value(-1 to quit):
1
Please enter an integer value(-1 to quit):
2
Please enter an integer value(-1 to quit):
3
Please enter an integer value(-1 to quit):
4
Please enter an integer value(-1 to quit):
7
Please enter an integer value(-1 to quit):
9
Please enter an integer value(-1 to quit):
8
Please enter an integer value(-1 to quit):
6
Please enter an integer value(-1 to quit):
-1
The numbers in the array are: 9 8 7 6 4 3 2 1 and the mean is: 5.0

Song.java

public class Song
{
private String title; //The title of the song
private String 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;
}
}

CompactDisc.java


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class CompactDisc
{
public static void main(String[] args) throws IOException
{
FileReader file = new FileReader("D:\Classics.txt");
BufferedReader input = new BufferedReader(file);
String title;
String artist;
  
//Declare an array of songs, called cd, of size 6
ArrayList<Song> cd = new ArrayList<Song>(6);
  
  
for (int i = 0; i < 6; 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.add(new Song(title, artist));
}
System.out.println("Contents of Classics:");
for (int i = 0; i < cd.size(); i++)
{
//print the contents of the array to the console
System.out.println(cd.get(i).toString()+" ");
}
}
}

Output:

Contents of Classics:
title1 by artist1

title2 by artist2

title3 by artist3

title4 by artist4

title5 by artist5

title6 by artist6

Classics.txt

title1
artist1
title2
artist2
title3
artist3
title4
artist4
title5
artist5
title6
artist6

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote