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

Class Notice- Please join RoboDuke's new Java Class Group, and use the group for class questions!

Welcome back to RoboDuke, the Javarian's lessons on the coolest language to hit the planet. I hope you did not get too dizzy with that 10,000 foot view of Java. If you are afraid of heights, do not worry. In this lesson, we will come down to ground level as we concentrate on variables - an item that makes up every Java program.

Download our jar file for lesson 2 from the link below:

http://www.vw.vccs.edu/bussci/Wolff/robot/lesson2.jar

Inside, you will see two packages (remember that every Java program must be contained inside of a package---and if you do not specify a package, it will put the program in the default package).

The first package is named answerslesson1. This contains the answers to some of the programs that you were to write in lesson 1 (you DID write them, didn't you?). There is a Breeze session that describes the solutions at http://live.vw.vccs.edu/p48208040/ (about 15 minutes long). Study these programs and make certain to understand the copious amounts of comments that I have inserted in these (yeah - I, unlike Diane, have no life so I have plenty of time to entertain you with my comments). Make certain to understand what the code does and why it is written as it is. Remember that we are now on ground level and need to slowly start understanding the details! Also - in the comments I have included some cool "RoboDuke Eclipse tricks" that will save you time. Make certain to note how I impressed you by using arrays in the ParrotDriver since we will be looking at them in this lesson. Bet that knocked your batteries right out (oh, yeah, I forgot---you use food and I use batteries).

So, I will wait while you study these........ Deedeedeedodee........ hmmmmdeduldum......... (are you studying???) ........hmmmdedumdedum........ OH! You are done? Well - then on to Chapter 3!!!

Chapter 3

Chapter 3 is an in-depth look at variables. There is a Breeze session describing the lesson at http://live.vw.vccs.edu/p14854705/. This is about 20 minutes long.

Java is a strongly typed language. This means that every variable (or memory location used to temporarily store some data) must have a data type. Your text does a GREAT job of explaining this so I will not repeat it all. We will, instead spend our time on coding examples. But in a nutshell:

  1. There are two types of variables
    • Primitive - used to actually store the value directly in that memory location.
      1. There are eight primitive types (see page 51).
      2. We typically will want to use the following primitives
        • Whole numbers - int
        • Numbers with decimals - double
        • Single key presses - char
        • True/false - boolean
    • Reference - used to refer to an object. The memory location stores a reference to where the object is actually located.
      • Has implications about when it is garbage-collected (see page 57)
  2. Arrays - either type of variable can be made into a "set of them" by defining an array.
    • An array is made up of similar objects (you cannot put Strings and ints in the same array)
    • Arrays are not dynamic (you need to define how big they are when you create them) - we will address this in our coding since it can be a problem.
    • Array elements are referred to by the index of their location on the collection (see our coding examples)
  3. There are three places to declare variables and their location is significant on what you are allowed to do with them. All three places allow primitives or reference variables or any type of collection like an array.
    • Instance variables - declared as part of a class outside of a method.
      1. Declared as accessModifier dataType variableName
      2. Can be given initial values
      3. If you do not assign initial values, Java will do it for you
      4. Are in scope (or valid) as long as the object exists.
    • Method local variables - declared inside of a method
      1. Declared as dataType variableName (they cannot have access modifiers)
      2. Can be given initial values
      3. If you do not assign initial values, Java will not do it for you
      4. Are in scope as long as the method is executing
      5. There are no global method variables like in some computer languages
    • Parameters - declared in the signature (top line) of the method
      1. Declared as dataType variableName (they cannot have access modifiers
      2. Values are obtained from the arguments that are being passed in by the method call (more on this in chapter 4)
      3. Are in scope as long as the method is executing

So --- let's get coding!

Look at the program named Ex1DeclaringPrimitiveVariables.java. Read the comments carefully. Then look at Ex2DeclaringReferenceVariables.java. Read the comments carefully. Also make certain you have watched the Breeze session for this lesson which will describe the details of these programs.

The Java class named String is a class and therefore, objects are made from it. But there is a very special exception when creating String objects. As we know, objects are created by calling the constructor and using the word new. So we did the following:

    Parrot p1 = new Parrot("Babs", "Cockatoo", 25, "Australia", 1200);    
   	Dog d = new Dog();    

Using this as a pattern, you would expect that we would need to do

   	String country2 = new String ("Peru");    

However, for the String class and only the String class, you can do

   	String country3 = "Brazil";   

But remember - Java Strings are still objects!

Now look at Ex3GasMileage.java. Carefully read the comments. Keep in mind the Scanner problem and the extra scan.nextLine() that may be needed. You need to watch this carefully with loops since you may be reading a primitive data type at the bottom of the loop and a String data type at the top.

Now It's Your Turn
Fill in the code for the program Ex4Expressions.java. Run the program and make certain that it works.

Extra Credit
Make the Ex5GuessingGame.java program run.

Now let's look at arrays. Look at the code and comments in the Ex6PrimitiveArrays.java program. Arrays are a type of Java collection. There are other collections that we will look at later in the class. Arrays in Java must be a collection that contains all of the same data types. And the program must declare how many items are in the array before the array is created. This can sometimes be a problem. If I am making an array of my Robot friends, I am a very popular dude, so I am not certain how many there are. The only choice that I have at that point is to create an array larger than I will ever need, and then keep track of which elements in the array have real Robots in it and which are still blank. See Ex7RobotFriendsDriver.java for an example. There will be much more on arrays in the future lessons!

Now It's Your Turn
I have copied our Dog blueprint from the first lesson into this package. Review this code. Then complete the Ex8DogDriver.java to make an array of Dogs and to print them out. NOTE: unlike the robot friends, we are asking the user how many dogs they have so we do not have the problem with the array size.