CS230

 

Lab: Object Oriented Programming

 

Solutions

Goals:

Practice with: User-defined classes and objects in java. Create a class from scratch. Your classes will include:

  • instance variables,
  • constructor(s),
  • toString() method
  • instance methods,
  • write some javadoc and
  • a main() method for testing
  • multiple constructors in a class

Possibly...

  • use of static method - reading from keyboard (Scanner)
  • use of a Driver class for testing

The User-defined Animal Class

Pre-Lab Tasks

On your own computer, set up a BlueJ project, add a class named Animal, and add code for the steps below marked as [PRELAB].

Step 1: instance variables[pre-lab]

Although there are many properties that one might choose to describe an animal, today we will stick with these:

  • type: the type of animal (e.g. horse, frog, dog, etc)
  • name: the name of the animal (e.g. Tonto, Froggy, Spot, etc)
  • voice: the sound the animal makes (e.g. neigh, ribbitt, bark)
  • canFly: indicates if the animal can fly (true) or not (false)
  • number_legs: the number of legs the animal has
  • and one more property of your own choice

Step 2: constructor[pre-lab]

The job of a constructor is to initialize the values of each instance variable. For this constructor, you can assume that there is an input parameter for each of the instance variables. For example, here are a couple sample invocations of your constructor (do not include the property of your choice):

    /* These are invocations of your constructor */

    Animal doggy = new Animal("dog","Rover","bark",false,4);
    Animal deer  = new Animal("reindeer","Rudolph","hi",true,4);

Step 3: main() method [pre-lab]

Set up the main() method, where execution will start. Add a couple of calls to the constructor you have defined to create some Animal instances. Compile and run your program. Although you do not expect to see any output yet, you should be able to have an error-free program at this point.

==========================================================

Upload the Animal class into gradescope, so you can download it at the start of lab and continue working on it.

Lab Tasks

Set up

  • Create a new folder on your Desktop, and name it lab3_userName1_userName2. All files you work on today should be saved in that folder.
  • Start BlueJ, create a new project called lab3Project.
  • Download the file Animal.java that contains your pre-lab work, into the lab3Project folder.
  • In BlueJ choose:

    --> Edit --> Add Class from File...

to add the pre-lab Animal class in your project. Make sure the class starts with a capital "A".

Step 4: toString() method

We recommend that you always write a toString() method early on in the definition a Java class.
This allows you to see the current state of your object and is really useful when debugging.

Write the toString() method, in the Animal class, so that when printing the doggy object above you produce something similar (doesn't have to be exact) to this:

Rover is a dog with 4 legs that says bark and cannot fly.

Test your code by printing out the Animal objects you created in the main() method earlier.

Step 5: "getter" and "setter" methods

A "getter" method simply returns the value of an instance variable.
For your Animal class, these are possible getters:

  • getType()
  • getName()
  • getVoice()
  • getCanFly(), and
  • getNumberLegs()

Write a couple of getters.(No need to write them all.) Test them in the main().

Parallel to a "getter" method, a "setter" method allows the user to change the values of an instance variable. Setter methods are always void. Each setter method should take a parameter with the value to assign to a particular instance variable.

For example, the setType() method should take a String parameter and the setCanFly() method a boolean parameter.

Write a couple of setters. Test them in the main().

Step 6: Add some javadoc

Take a look at the following resource:

javadoc from Oracle](https://www.oracle.com/technetwork/java/javase/tech/index-137868.html)

Based on what you read, add some javadoc to your program. At this point, it would be enough to use the following tags:

  • @author,
  • @version,
  • @param,
  • @return

Then, in the BlueJ Editor window, from the drop down menu, in the upper right corner, choose Documentation. What do you see? Then open the folder that contains your project. Do you see anything new there?

NOTE: From now on, we'll be expecting fully-fledged Javadoc in all the assignments you submit.

Step 7: instance methods

Define the following instance methods:

  • likesToImitateCats(): sets voice to be a cat sound
  • speak(): prints out the animal's voice three times (for example: meow meow meow)
  • converse(Animal a2): returns a String that contains a conversation between two animals.

    For example:

    Animal a1 = new Animal("dog","lassie","ruff",false,4);
    Animal a2 = new Animal("cow","helga","moooo",false,4);
    
    System.out.println(a1.converse(a2));
    a1.setVoice("everyone dance!");
    System.out.println(a1.converse(a2));

    produces:

    A conversation between lassie and helga:
    ruff moooo ruff moooo
    
    A conversation between lassie and helga:
    everyone dance! moooo everyone dance! moooo

Make sure to upload (submit) your work!

=========================================================

Extra tasks (optional)

Task: Add a second constructor

Examine the following code:

public Animal(String t, String n) {
   type = t;
   name = n;
   voice = "";
   canFly = false;
   numberLegs = 4;
}

It defines a second (alternative) constructor which takes only two inputs: The type and the name of the animal to be created. The rest of its characteristics are set to some default values.

Notice that the above (second) constructor can be written in a way that takes advantage of the first (already defined) constructor:

public Animal(String t, String n) {
   this(t,n,"",false,4); // calls the constructor that takes 5 inputs
}

Task: Get input from the user

So far the values properties of the Animal objects we have created are "hard-wired" into the constructor. In this step you will get these values from the user through the standard input (keyboard).

Set up a static method named readAndCreateAnimal(): ask the user about all the information you need to create an Animal object. Use a Scanner to get the user-supplied information. Create the object within the method, and return it at the end of it.

Why should this method be declared static?