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
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; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started