Docsity

Arrays in Java – Arrays of Objects, Study notes of Java Programming

Toggle sidebar
bg1
Arrays in Java – Arrays of Objects
Elements of an array can be of any type, and when applied to objects, this can prove very practical
and useful. For example, consider collecting data on a group of children. We might have multiple
arrays to store their names, ages, heights, and genders.
String[] name = new String[MAX_CHILDREN];
int[] age = new int[MAX_CHILDREN];
double[] height = new double[MAX_CHILDREN];
char[] gender = new char[MAX_CHILDREN];
Arrays of the same length, carrying data on different aspects of a problem, are sometimes called
parallel arrays. Rather than four parallel arrays for our study, we could use a single array of objects.
class Child
{
private String name; // family, given
private int age; // in years
private double height; // in metres
private char sex; // M or F
}
Child[] patient = new Child[MAX_CHILDREN];
So far, this has created an array where each element can refer to a Child object. We have not yet,
however, created any actual Child objects. To accomplish this, we could traverse the array, and
create an object for each element.
for (int i = 0; i < patient.length; i++)
{
patient[i] = new Child();
}
It is only with this second fragment of code that we actually invoke the constructor for the Child class,
which will create the necessary Child objects in memory.
In this case, all of the information about the first child can be referenced through patient[0], such
as patient[0].age or patient[0].sex.
There are a number of advantages to using an array of objects rather than multiple arrays of data.
1. A method to process data about one object can be given a single parameter rather than many
parameters.
2. If the program is later modified to include different kinds of data, we only need to change the
fields in the object class. The parameter lists of any methods remain unchanged.
3. By making the class fields private, we can ensure the data will be handled appropriately
through the use of accessor and mutator methods.
17. Nov. 2010 Page 1 of 3
pf3

Partial preview of the text

Download Arrays in Java – Arrays of Objects and more Study notes Java Programming in PDF only on Docsity!

Elements of an array can be of any type, and when applied to objects, this can prove very practical and useful. For example, consider collecting data on a group of children. We might have multiple arrays to store their names, ages, heights, and genders. String[] name = new String[MAX_CHILDREN]; int[] age = new int[MAX_CHILDREN]; double[] height = new double[MAX_CHILDREN]; char[] gender = new char[MAX_CHILDREN]; Arrays of the same length, carrying data on different aspects of a problem, are sometimes called parallel arrays. Rather than four parallel arrays for our study, we could use a single array of objects. class Child { private String name; // family, given private int age; // in years private double height; // in metres private char sex; // M or F } Child[] patient = new Child[MAX_CHILDREN]; So far, this has created an array where each element can refer to a Child object. We have not yet, however, created any actual Child objects. To accomplish this, we could traverse the array, and create an object for each element. for (int i = 0; i < patient.length; i++) { patient[i] = new Child(); } It is only with this second fragment of code that we actually invoke the constructor for the Child class, which will create the necessary Child objects in memory. In this case, all of the information about the first child can be referenced through patient[0], such as patient[0].age or patient[0].sex. There are a number of advantages to using an array of objects rather than multiple arrays of data.

1. A method to process data about one object can be given a single parameter rather than many

parameters.

2. If the program is later modified to include different kinds of data, we only need to change the

fields in the object class. The parameter lists of any methods remain unchanged.

3. By making the class fields private, we can ensure the data will be handled appropriately

through the use of accessor and mutator methods.

Exercises

  1. For the child development study discussed above:

a) write a method that could be added to the^ Child^ class that prints the data (name, age,

height, and gender) for a child on two lines with the name on the first line and the other data on the second line. Print the height in centimetres, rounded to the nearest centimetre.

b) write a fragment that uses this method to print the data on all the children.

c) write a method that would produce the same output at the method from part (a) if we had

chosen to use parallel arrays rather than an array of objects to represent our data.

  1. Assuming that a Fraction class has been defined as shown, write a fragment that could be used to create an array of 100 Fraction objects representing the fractions

class Fraction { private int num; private int den; public Fraction (int n, int d) { num = n; den = d; } }