CISC 3115

Unit 2 Reading Guide

Learning Targets

  1. I can write a class definition that includes instance variables and methods that use those variables.
  2. I can distinguish between instance variables and local variables.
  3. I can argue for the importance of encapsulation in class design, and use the public and private keywords appropriately.
  4. I can write "enhanced" for loops and use them appropriately.
  5. I can describe the class development process, including the role of test code.
  6. I can find and the online Java API documentation in order to understand how to use a particular method in the API.

Unit 2 covers chapters 4 through 6—sounds like a lot, but most of this material should be familiar. There are a few important new concepts, though!

The first part of chapter 4 contains stuff you should already know about writing methods. (In this chapter, it's in the context of class design, but the essential principles are the same.) In particular, I suspect you're already familiar with these ideas:

One of the most important ideas in Chapter 4 is encapsulation (starting on p 80). In your own words, what's the problem with the "theCat" scenario at the bottom of the page? After you read the "Java Exposed" dialog on p 81, pause and name as many advantages of encapsulation as you can. (Hint: it's not only about fixing theCat.)

The discussion on pp 84–5 highlights a small but important distinction in Java: different kinds of variables are initialized differently. Be sure you understand the distinction between local and instance variables.


Chapter 5 is based on a very siple game, basically a one-dimensional version of the game commonly known as "Battleship." The main focus of this chapter is developing a simple game program, including designing a few classes, from scratch. Note a few points about their recommended design process:

On p 105 there's a list of a few things that might be new to you. Be sure to study these (in the chapter), particularly if they are new to you.

Be sure you understand the game's code (bring questions to class)—and be sure you understand the problem that's pointed out on 113 (not that it's so hard...).

The discussion on p 114–115 should be familiar to you, but the enhanced for loop on p 116 may be new to you. Be sure you understand how it works.

You may or may not have discussed "casting" before. The first real discussion in this book is on p 117. Casting becomes more and more important as we advance in object-oriented programming, so spend some time on this page. In general, the casting operator is a way to force the compiler to convert a value of one type to another, even if it might not look like a good idea. We'll be talking about when the compiler does this kind of type-conversion automatically, when it can never be done, and when we as programmers need to explicitly tell the compiler to do it.

As usual, the more time you can spend on the end-of-chapter paper-and-pencil exercises, the more successful you'll be in this class.


Chapter 6 has a strong focus on developing the full code for the "Sink-A-Dot-Com" game. So a lot of your reading here is studying code. The code discussion really starts on p. 144 (note the exercise they sneak in on pp. 146–147: take this exercise seriously, as it's a very good way for you to assess whether you understand what's going on with their code). The "ready-bake" code on pp. 152–153 is a little bit on the hairy side (it's the logic for randomly placing the dot-coms on the game board). There's nothing here you can't understand, but don't spend a ton of time trying to figure it out. If you have questions about the code, ask me in class or in office hours.

What about the rest of the chapter? There are a few valuable chunks scattered throughout....

On pp. 129–130, why exactly would an ArrayList work better than a regular array?

On pp. 142–143, carefully study the object relationships in the diagram. Notice that the arrows don't all mean quite the same thing—most of them mean "instantiates", as at the top of p. 142, but some of them represent method invocations (and returns). (Alternatively, you could view those "instantiates" arrows as references, right?)

P. 151 has a useful discussion about Boolean expressions. You should be generally familiar with these, but take note of a few significant ideas:

Pp. 154–157 have a pretty detailed discussion of how Java's package system works, and what it's for. (It doesn't address the question of putting your own code in a package, which may come up later in the semester.) Again, you should be pretty familiar with the need to use import statements, but this discussion is really good background on what's going on.

Finally, pp. 159–160 have a basic discussion of how to read/understand the Java API. We've already spent some time on this, of course; p. 160 is a nice summary of why the Java API documents online can be really useful.