home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
hw07
Objects containing objects
Students with Dates
Part (a)
Due Feb.29 (Fri), 20pts:
only hardcopy is needed.
In the following exercises,
we'll use both Date objects as seen in lab,
and
Student objects.
Every Student has four fields:
their name, their birth-date, their major, and their expected graduation date.
01. Date b1;
02. b1 = new Date(1990,2,20);
03.
04. Date g1 = new Date(2011,5,14);
05.
06. Student s1;
07. s1 = new Student( "Amy", b1, "ITEC", g1 );
08.
09. Student s2;
10. s2 = new Student( "Mike", new Date(1989,11,30), "ITEC", new Date(2011,12,14))
|
-
What are the types of each of a Student's four fields?
-
How many times does new occur, in lines 1-10?
- How many times is the Date constructor called?
How many times is the Student constructor called?
(Your answers should sum to the number you answered for #2, of course.)
-
How many local variables are declared, in lines 1-10?
(Your answer need not be the same as your answer for #2;
an object can be created without assigning it to a local variable,
just as you can use the number 17
or the result of Math.sqrt(121.0)
without assigning it to a local variable.)
-
Which line(s) declare a local variable?
-
Which line(s) initialize a local variable?
-
Draw the picture
corresponding to the above code.
Your result will look
like lect07c—Objects as fields: Dogs and Kennels
which is reminiscent of
2g from hw06a-soln,
except that picture had only one local variable
(“g”)
and only one object (a Glarkzle).
For each box you draw (whether for a local variable, an object,
or a field inside an object),
tag it with the number of the
line which created1 that particular box.
NOTE:
You know everything you need to draw this picture
(including the fact that new returns a reference (arrow) to
an object).
But Friday's lecture will work through another example of objects which
contain (references to) other objects;
we will give you 3min at the end of Friday's lecture to finish this picture.
-
What type of value does
s1.getName() return?
-
What is the actual result of calling
(s1.getName()).length()?
(Your answer should be a particular number.
The String function length()
returns the number of characters in that string.)
-
What is the actual result of calling
(s2.getName()).length()?
-
What type of value does
s1.getGraduationDate() return?
-
What is the actual result of calling
(s1.getGraduationDate()).getMonth()?
-
What is the actual result of calling
(s2.getGraduationDate()).getMonth()?
Part (b)
Due Mar.03 (Mon), 20pts:
You'll turn in two things (stapled together):
-
The html as usual (via Tools >Project Documentation).
You will need javadoc for all your methods, including getters.
No setters are required.
-
Your test cases
(found inside the test file which BlueJ auto-creates).
-
Make a copy of your project which contains class Date.
(You may use all or part of lab07b-Date-tests.jar;
add a second @author tag for your name in that case.)
(how-to-get-jar-files-into-BlueJ—Downloading .jar files into BlueJ)
-
Add a second class to that project: class Student.
-
Declare the necessary fields,
getters/setters,
and write the constructor (with documentation).
-
Make three Students,
and save them to the test fixture.
-
Write and compile a stub for a
Student method
sameMajorAs(Student)2.
Two majors are the same even if they differ in capitalization:
that is, "Math", "math",
and "maTh" are all considered the same major.
Record two test cases for this method.
(one which will return true,
and one which will return false).
-
Write and compile a stub for a
Student
method
willGraduateInMay().
Record two test cases for this method
(one which will return true,
and one which will return false).
This method returns true if they are scheduled to graduate
(or did graduate)
in some May, not necessarily this-upcoming-May.
-
Write the signature for a
Student
method
willGraduateWith(Student).
Record two test cases for this method
(one which will return true,
and one which will return false).
Part (c)
Due Mar.05 (Wed) (15pts):
implementation (Java code)
for the above methods
sameMajorAs(Student),
willGraduateInMay(),
and
willGraduateWith(Student).
Turn in hardcopy as usual.
Your code should use getters,
but not access any fields directly.
Note that you can call the getter on any object,
even an object which was just returned by another getter!
(as in #11).
1
That is, the line which caused the box to come into being.
This may or may not be
the same line which initialized the box's contents.
↩
2This notation
means a function sameMajorAs which takes in one argument,
of type Student.
You'll need to make up an actual parameter name,
as well as decide on an appropriate return-type, of course.
↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs