Self-help exercise: Drawing objects
It is important that you be able to draw manila folders (objects) of a class when you have to. The need will arise when executing some part of a program by hand in order to find an error. Further, the ability to draw objects will promote understanding of various concepts.
It is also important that we all draw objects the same way, so that we can communicate better.
Please do not skip the following simple exercises. Doing them will not only implant in your mind how we draw objects but will also help you become familiar with our terminology —class manila folder or object, name of an object, component, field, method, function, and procedure.
1. Draw an object for a class Animal that has three fields:
(1) the kind of Animal (a String).
(2) its sex (a boolean, true for female and false for male).
(3) its age, in years (an int).
and four methods:
(1) A function that yields the kind of the animal.
(2) A function that yields the sex of theanimal.
(3) A function that gives the age of theanimal.
(4) A function older(an1, an2) that returns the value of the sentence "animal an1 is older than animal an2".
Put some reasonable values in the fields. Note that we are not asking you to define functions and procedures in Java code —we haven't seen how to do that yet. We are just asking you to indicate in the object what methods there are, as done in the lecture.
Here is our sample answer.
2. Draw an object for a class Person, which allows us to maintain a geneology tree for people. Person has three fields:
(1) name (a String).
(2) father (of class Person) null if unknown.
(3) mother (of class Person) null if unknown.
and the four methods:
(1) A function that yields the name of the person
(2) A function that yields the father of the person (null if unknown)
(3) A function that yields the mother of the person (null if unknown)
(4) A function with one parameter, of class Person, that yields true if the parameter is this person's father.
Put some reasonable values in the fields. Note that we are not asking you to define functions and procedures in Java code —we haven't seen how to do that yet. We are just asking you to indicate in the object what methods there are, as done in the lecture.
Here is our sample answer.