home—info—lects—labs—exams—hws
tutor/PIs—Object120 + its docs—java.lang docs—java.util docs
lab06a
Objects with fields
define, create
Update:
A version of Object120 has been released as
of Oct.04 (v1.03 (adamant));
please use the updated version from now on.
Note that you can call Object120.getVersion().
Scenario:
In the registrar's computer system,
a student has a name, a sid, a gpa, a major,
and a number of credit hours completed.
For the following, it's expected that you'll refer back to
lect06a—Objects intro: compound data (fields).
-
Create a class Student to represent this information
— start with fields only, and make sure your code compiles.
-
What is the signature for the Student constructor?
Write the constructor;
for its body use the mysterious call to super as in the
lecture notes
for now.
(On Friday we'll see how to replace that call to super
with our own, actual code to initialize the fields.)
-
Call the constructor,
to create an example of a Student.
(Either call in the code pad; if that doesn't work right-click on the class.)
Inspect the result in BlueJ, by clicking on the small red return value (in code pad),
or the Inspect button if you called the constructor interactively.
-
As usual, make a function testStudent();
have it declare two variables s1,s2 which
can hold students and initialize each of them.
-
Draw a picture (memory diagram)
of what things look like after the computer
declares and initializes your two variables.
(As you do this, be aware of how the picture gets built step-by-step --
there are six things that happen to make that picture, and each of those
correspond to an intermediate picture.
For example: What is the picture
immediately after declaring the first variable and calling the constructor,
but before actually completing the initialization of the first variable.)
Do this on paper, and turn it in as you leave lab (with both you
and your partner's name on it).
-
Consider the function
onDeansRoll(Student)
(true for students whose gpa is above 3.6).
-
Write a stub function for onDeansRoll.
-
Write three test cases function for onDeansRoll
(two can use the variables you already have declared;
have the third test case call the constructor
without saving the result in a variable).
-
We have not yet talked about how to implement the function,
so don't do so yet.
(If you finish the stub/tests for this and the next function
raise your hand, and a (peer) instructor will tell you how
to access an object's field from inside the function,
using the “.” operator.)
-
Consider the function
sameMajorAs,
which takes in two Students and
tells whether they have the same major.
-
Write a stub function for sameMajorAs.
-
Write two test cases function for sameMajorAs
(one can use the variables you already have declared;
have the other test case call the constructor
without saving the result in a variable).
-
We have not yet talked about how to implement the function,
so don't do so yet.
(If you finish the stub/tests for this and the previous function
raise your hand, and a (peer) instructor will tell you how
to access an object's field from inside the function,
using the “.” operator.)
-
Submit your work on WebCT -- Assignments > lab06a.
- Bonus:
Write tests, a stub, and finally code for
enrollNewStudent which takes in a
name and sid,
and returns a new Student with 0 hours enrolled
and a 4.0gpa.
home—info—lects—labs—exams—hws
tutor/PIs—Object120 + its docs—java.lang docs—java.util docs