Java Arrays Lab Objectives Be able to declare and instantiate arrays Be able to
ID: 3690006 • Letter: J
Question
Java
Arrays
Lab Objectives
Be able to declare and instantiate arrays
Be able to fill an array using a for loop
Be able to access and process data in an array
Be able to write a sorting method
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 +selectionSort( ): 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:
data[]— the array which will contain the scores mean — the arithmetic average of the scores
Methods:
Average –the constructor. It will allocate memory for the array. Use a for loop to repeatedly display a prompt for the user which should indicate that user should enter score number 1, score number 2, etc. Note: The computer starts counting with 0, but people start counting with 1, and your prompt should account for this. For example, when the user enters score number 1, it will be stored in indexed variable 0. The constructor will then call the selectionSort and the
calculateMean methods.
calculateMean – this is a method that uses a for loop to access each score in the array and add it to a running total. The total divided by the number of scores (use the length of the array), and the result is stored into mean.
toString— returns a String containing data in descending order and the mean. selectionSort—this method uses the selection sort algorithm to rearrange
the data set from highest to lowest.
Task #2 Average Driver
Create an AverageDriver class. This class only contains the main method. The main method should declare and instantiate an Average object. The Average object information should then be printed to the console.
Compile, debug, and run the program. It should output the data set from highest to lowest and the mean. Compare the computer’s output to your hand calculation using a calculator. If they are not the same, do not continue until you correct your code.
Explanation / Answer
AverageDriver.java
import java.util.Scanner;
public class AverageDriver {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the size of an array");
int size = scan.nextInt();
Average a = new Average(size);
a.calculateMean();
System.out.println(a.toString());
}
}
Average.java
import java.util.Scanner;
public class Average {
int data[];
double mean;
Scanner scan = new Scanner(System.in);
public Average(int num){
data = new int[num];
System.out.println("Please enter the numbers");
for(int i=0; i<num; i++){
data[i] = scan.nextInt();
}
selectionSort(data);
System.out.println("Array Elements sory by Selection Sort:");
String str = "";
for(int i=0; i<data.length; i++){
str = str + data[i]+" ";
}
System.out.println(str);
}
public void selectionSort(int arr[]){
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}
public void calculateMean(){
double sum = 0.0;
for(int i=0; i<data.length; i++){
sum = sum + data[i];
}
mean = ((double)sum/data.length);
}
public String toString(){
String str = "";
for(int i=data.length-1; i>=0; i--){
str = str + data[i]+ " ";
}
str = "Array Elements : "+str + " Mean : "+ mean;
return str;
}
}
Output:
Please enter the size of an array
5
Please enter the numbers
2 4 6 1 3
Array Elements sory by Selection Sort:
1 2 3 4 6
Array Elements : 6 4 3 2 1 Mean : 3.2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.