RoboDuke's School of Java - Lesson 1, Chapter 2

I'm back! Batteries charged and ready to go! Let’s start Chapter 2 - A trip to Objectville (I always heard that was a wonderful place to visit!!!).

Java is Object Oriented

Read and absorb pages 27-35. In a nutshell-Java is object oriented. Applications usually mimic the real world (there is one???) with its classes and objects. For instance, business applications written in Java may have classes such as Customer, Invoice, Employee, Sales, etc. But I prefer classes like Robot, Roboraptor and cool classes like that.

Let’s discuss the difference between a class (the blueprint) and an object (the actual "thing").

Classes vs. Objects

Let’s look at a Dog class (blueprint) that indicates what a dog is like. Look at our Dog class code in Eclipse (we used this already without really understanding it).

Classes are made up of two things - instance variables and methods. Instance variables are the nouns (make certain to name them as nouns) that describe attributes of the class. In this case, we are defining a Dog by his name, breed, and size. Methods are the verbs (make certain to name them as verbs) that describe what a Dog can do or what we can do with the Dog. Look at the methods we have included for our Dog ---bark() and play(). We also include a lot of methods to help us program a dog including toString() - used to print the Dog to the Console and a group of getters and setters - used to give values to the instance variables or to change the values of the instance variables (more on that later).

But we do not yet have any real dogs!! Let’s look at some. I actually own Spot. He is a real dog (an object or an instance of the Dog class). He has a name (Spot), a breed (he is a Roboraptor), and a size (32" long) - actual values for the instance variables we saw in the class blueprint. Maybe you have a real dog also. Bet he has a different name, breed, and size. These are our objects.

So---classes are the blueprints and objects are the actual instances of the class.

Constructors

Now you want to know how to create these objects from the Dog class? Did you see those funny looking methods in the Dog class that looked like the following. These are called constructors and are used to construct objects.

  public Dog()  {  }  
  public Dog (String n, int s, String b)  { name = n; size = s; breed = b;  }   

There are more details on how to write constructors in Chapter 9. Here we only want to know how to use them. Read pages 36-37 in your text. Study our program named DogTestDriver.java. Run this.

Note on page 38 of your text. There are two uses for the main method - to launch/start your application and to test your real classes.

Look at the GuessGame.java in your text and start to understand the code (do not worry if you do not understand it all - we are still at 5,000 feet). See if you can type this in and run it.

Work through the rest of the chapter. Note carefully the bullet points on page 41.

But us Robots need to spend money so let’s look at another class (remember - blueprint??). Open up the Account.java blueprint and see how much of it you can understand. Read the comments carefully.

Run the AccountDriver.java program and try to understand how it works.

Now it's Your Turn

YOUR TURN TO TRY IT!!!!

My sidekick, Diane, raises parrots. Create a blueprint class named Parrot. Using your Dog class as a template, write the instance variables and methods for the Parrot class. Parrots have a name, breed, age, country of origin, and cost. Write the constructors, toString method, and getters and setters. Write a method named squawk() which has the bird going "Squawk, squawk, squawk!!" and a method named doTricks() which returns the name of the trick that the bird does. Have the method pick one of the following using a random number generator (see page 16)

String[ ] trickList = {"jump", "kiss", "play dead", "roll a ball", "talk"};

Next, write a driver program that creates three birds (Diane really has 21 of them and boy are they noisey!!!) and prints out the trick that they do. Use the following data:

 

Name

Breed

Age

Country

Cost

Babs

Cockatoo

25

Australia

1200

Alex

Hyacinth Macaw

18

Brazil

8000 (think about how many robots she could have bought for that money!!)

Annie

Green Wing Macaw

3

Peru

1600

Extra Credit

This next step is extra credit, and extra impressive! See if you can get the Stock and StockDriver included in your jar file to run. Follow the comments I have written in the program. All the red Xs should go away before you can run the program.

Lots of stuff but wasn’t that fun? And you thought Billy Joe Sapien was entertaining. We have barely begun!!

Keep playing with the code---making changes and writing your own small programs. And stay tuned for lesson 2.

 


Back to Lesson Plan