Date
Assigned: Wednesday, April 20, 2005
Due Date: Wednesday, April 27, 2005, before the beginning of your
lab section.
Goals: to learn about arrays.
Points: This assignment is worth a total of 20 points. No late
assignments will be accepted, but credit will be given for partially completed
assignments turned in on time.
Assignment:
There are two parts in this assignment.
Part 2 is much more difficult than Part 1, so please plan your time
accordingly.
Part
1. Chemistry Tutor. Often the contents of an array are
arranged so that the index indicating the position of each item in the array
corresponds to a number associated with that item. For this exercise, you will be using
this type of array arrangement to create a “Chemistry Tutor” application. Declare three arrays: name (type String) to store the names of
the elements, symbol (type String) to
store the symbols for the elements, and mass (type double) to store average
molar masses for the elements. Only
values for the first six elements of the periodic table will be stored. Let the number of the array index
correspond to the atomic number associated with each element. For example, for element #1 (hydrogen),
the name array should contain a
reference to the string “hydrogen” in component [1], the symbol array should contain a reference
to the symbol “H” in component [1], and the mass array should contain the double
amount 1.01 in component [1].
Notice that we will not be using array component [0] in any of the arrays
because there is not an element #0.
Use an initializer list to initialize the arrays using the following
details for the first six elements:
|
Number |
Name |
Symbol |
Mass |
|
1 |
hydrogen |
H |
1.01 |
|
2 |
helium |
He |
4.00 |
|
3 |
lithium |
Li |
6.94 |
|
4 |
beryllium |
Be |
9.01 |
|
5 |
boron |
B |
10.81 |
|
6 |
carbon |
C |
12.01 |
Write an application named ChemApp to prompt
the user to enter an integer between 1 and 6 (inclusive) in order to select one
of the first six elements. Use the
number entered by the user as the index to access the element name, symbol, and
mass. Display on the screen a
message informing the user of the name, symbol, and mass of the element
selected. For example, if the user
enters the number 6, the message displayed would be as
follows:
Element
number 6 is carbon (symbol = C), with average molar mass of 12.01
g/mol.
Part
2. Working with an array of objects. When an object has several attributes, rather than
placing the attributes in separate arrays it is often better to create a class
for the object that includes these attributes as fields. Then instances of that class can be
created and stored in just one array.
For example, in the exercise above, the arrays contained attributes
related to elements. It would make
sense to create a class Element, with
fields representing the element’s name, symbol, and molar mass. We could instantiate a new Element object for each element, and
store a reference to each Element
object in the appropriate location in one array (for example, an array named
perTable). Notice that we would need to declare the
type of the perTable array to be the class
that we defined, Element, as shown in
this example: Element [
] perTable = new Element [ 6
].
In this exercise,
create an application to assist the
a)
Create an
object-creating pattern class named Student, with instance fields name (type String), age (type int), and gender (type char). Include appropriate accessor methods to
return the values of the fields.
Code a constructor method with three parameters.
b)
Write an
application class named HealthApp. Within this application, declare and
instantiate an array of length 50 named patients. (There will not be that many students
stored in the array, but when the exact number is unknown, declare the array
size to be larger than you think you will need.) This array will be used to store Student objects (more precisely,
references to Student objects). Fill in the patients array as
follows:
·
Use a while loop to read in the name, age, and
gender for each student from a file named student.txt.
·
The name, age, and
gender for each student are listed with one field per line in the
file.
·
For each student
represented in the file, instantiate a new Student object for that student, and
assign each new Student object as a
component of the array patients.
·
The Student objects should be located
sequentially in the array in the same order as the students were listed in the
input file. When the while loop is
finished executing, the patient array
will contain all of the Student
objects created from data in the file.
c)
Write three methods
within the application class to return the information listed below. Each method should have one array
parameter. The methods should use
a for loop
to process the array passed (actually what is passed is just the reference to
the array, not the whole array itself) in performing the tasks necessary to
obtain the information. Adjust the
return type of each method as appropriate for the type of information returned
as follows:
·
The average age
(double) of all the students represented in the patient array.
·
The name (String)
of the oldest student included in the array. (If there are duplicates of the oldest
age, return the name of the first person.)
·
The number (int) of
male students included in the array.
Deliverables: as announced in the labs.