Skip to main content

more options

Answers to self-help exercise: Drawing objects

Below is 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".

 

We chose the names of the fields and methods. We annotated each field with its type; you don't have to do this. With each method, we put the types of its parameters in parentheses. You don't have to do this now. You will learn more about parameters and their types later on.

folder