LAB07-Department of Computer Science
LAB07-Department of Computer Science
Objectives:
using arrays
cloning arrays
writing methods that receives arrays as parameters or have array as their return type
using array of objects
An array is a contiguous list of memory cells that can be identified using a single va
riable.
This creates the array object in the memory (just like any object) and stores its address in the reference variable gr
ades.
Notice that the index of an array begins from zero and ends one less than the size. Any attempt to access elements outside this range will make the java interpreter to
throw ArrayIndexOutOfBoundsException.
Each array has an instance final variablelength that holds the size of the array. This is very useful if we need to access each element of the arr
ay. For example, the
following for loop initializes each element of the arr
ay to its index:
for (int i = 0; i < grades.length; i++)
grades[i] = i;
This declares and creates a character array of five elements and initializes it with the vowel characters. Notice that in this case, the size is not specified and the new
operator is not used. The compiler automatically counts the elements, creates an arr ay of the appropriate size and fills the array with the elements.
We can also create array of objects. For example, the following declares an arr
ay to hold 10 student objects:
Student[] student = new Student[10];
However, unlike array of primitive types where primitive values are stored directly into the arr ay cells, array of objects is used to store the references to the objects.For
example, to store a reference to a student object in the first cell of the arr
ay, we create the student object as follows:
student[i] = new Student(. . .);
2. Example programs
Example 1: The following creates two array and prints their vector and dot products.Notice that the method vectorProduct() method receives two array parameters and
returns another array – the vector product of the two arrays.
public class Vector {
double[] z;
z = vectorProduct(x, y);
System.out.println("The vector product is: ");
return t;
double sum=0;
return sum;
int k;
double[] w;
y = x; // y and x refer to the same object. The object that was referenced b y y is lost
x[0] = 200;
x[4] = 66;
z[0] = 88;
z[2] = -99;
double[] quiz;
this.iDNumber = iDNumber;
this.quiz = quiz;
return iDNumber;
else
return -1.0;
quiz[quizNumber - 1] = quizGrade;
double sum = 0;
sum += quiz[k];
return sum;
String s = ""+iDNumber;
s += "\t"+quiz[i];
s += "\t"+average();
return s;
import java.util.Scanner;
/* destroy the object referenced by quiz, so that the only way to obtain quiz information is from the object referenced by student
*/
quiz = null;
System.out.println(student);
student.setQuiz(3, 90);
student.setQuiz(4, 75);
Example 4: The following example shows how to usearray of objects. It uses the same Student class as in example 3 above to create an array of three students objects,
each one having ID number and three quizzes.The Program then prints the average of each students and the overall average for all students.
import java.util.Scanner;
double sum=0;
student[i] = createStudent();
System.out.println("\nID\tAverage");
System.out.println(student[i].getID()+"\t"+student[i].average());
sum += student[i].sum();
int id;
id = stdin.nextInt();
quiz[i] = stdin.nextDouble();
return s;
3. Assignments
1. Modify example 3 so that after the array is created (as in example 3), it displays a menu that allows the operations shown in the following figure to be performed
in a loop until the Quit option is chosen.
Note: If the first option is chosen, your program should prompt for and read a quiz number.It then calls the getQuiz() method to print it. If option 3 is chosen,
your program should prompts for and read the quiz number and the new grade for that quiz. It then calls the setQuiz() method to change the grade and then
display the updated student information.
3. Ahmadu Bello University Press wants to know the maximum, minimum, total, and aver age profit gained from years 2005 to 2014.
Besides that, they are interested in knowing the difference between the maximum and minimum profit and those profits that are above aver
age.
The profits are as follows.
Year Profit( )
2005 5,000,000.34
2006 2,005.000.00
2007 3,020,000.97
2008 5,057,800.20
2009 4,500,000.67
2010 5,000,000.00
2011 3,048,900.56
2012 4,800,000.50
2013 2,980,000.71
2014 4,909,000.80
i. Two array fields to store the profit and year named profit and year using appropriate data types.
ii. A method called getMaxProfit() that will return the maximum profit.
iii. A method called getMinProfit() that will return the minimum profit.
iv. A method called getSumOfProfit() that will return the total profit.
v. A method called getAverageProfit() that will return the average profit.
vi. A method called getRange() that will return the difference between the maximum and minimum profits.
vii. A method called showProfitBelowAverage() that will display all profits that are below average.
(b) Create a test class that will instantiate theProfit class and display the required results with suitable headings and labels