public class Average { private int[] data; private double mean; public Average(){ } public Average(int[]data,double mean,int j) { this.mean=mean; System.out.println("you should input score number"+(j+1)); this.data=data; } public void selectionSort(int[] data) { int startScan, index, minIndex; int minValue; int n=data.length-1; for (startScan = 0; startScan < n; startScan++) { minIndex = startScan; minValue = data[startScan]; for(index = startScan + 1; index < n+1; index++) { if (data[index] < minValue) { minValue = data[index]; minIndex = index; } } data[minIndex] = data[startScan]; data[startScan] = minValue; } } public void calculateMean(int[] data,double aver) { int sum = 0; int n=data.length; for (int i = 0; i < n; i++) { sum += data[i]; } aver = sum / 5.0; System.out.println("the average number is:"+aver); } public String toString(int[] data) { selectionSort(data); double aver=0; calculateMean(data,aver); for(int i=0;i<5;i++){ System.out.println(data[i]+" "); } return " "; } }
public class Song { private String title; // The song's title private String artist; // The song's artist public Song(){ } public Song(String title, String artist) { this.title = title; this.artist = artist; } public void set(String title,String artist){ this.title=title; this.artist=artist; } public String title(){ return title; } public String artist(){ return artist; } /** The toString method @return A String object containing the name of the song and the artist. */ public String toString(Song[]arr,int i){ System.out.println(arr[i].title + " by " +arr[i].artist() + "\n"); return " "; } }
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; final int size=6; Song[] cd=new Song[size]; // ADD LINES FOR TASK #3 HERE // Declare an array of Song objects, called cd, // with a size of 6 System.out.println("Contents of Classics:"); for (int i = 0; i < size; i++) { title = input.readLine(); artist = input.readLine(); cd[i]= new Song(); cd[i].set(title,artist); // 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 cd[i].toString(cd,i); // ADD LINES FOR TASK #3 HERE // Print the contents of the array to the console } } }
import java.util.Scanner; public class AverageDriver{ public static void main(String[] args) { int[] aver = new int[5]; double mean=0; for(int j=0;j<5;j++) { Average av1 = new Average(aver, mean, j); Scanner sc = new Scanner(System.in); aver[j] = sc.nextInt(); } Average av1 = new Average(); av1.toString(aver); } //when I input 3,8,1,2,0.I algorith average is 2.8. //and sorted from max to min is 8,3,2,1,0 }