17-04-2012, 11:09 AM
Computer Programming using Java and BlueJ
javabook.pdf (Size: 2.64 MB / Downloads: 86)
Preface to the reader
Oh no, not another Java textbook!
Perhaps that’s a valid comment, but wait until you’ve had a chance to look at this one. There is a
lot of discussion amongst teachers about when to introduce objects in Java. I feel that objects
should be introduced early, as early as possible, perhaps on the first day of an introductory
course. I also believe that students should use a large application as an example, so they can see
the advantages of objects.
Reflecting these ideas, this is not just another Java book.
To students
What is computer science? How does this textbook help you study computer science?
Much, but not all, of computer science involves using a computer to solve a problem. The
instructions to solve the problem are written as a program or programs, instruction which the
computer performs.
To teachers
Like many textbooks, this one arose out of a perceived need. I wanted to teach an introductory
programming course using Java, and I wanted to do it objects-first. I could not find a textbook
that met my needs, so I decided to create my own. The competition was between editions, and
other competitors were not yet available. In the time it has taken to complete this book, other
competitors have appeared. To see this textbook published, I have used the Creative Commons
approach.
The MyDate class – unit tests
Use your unit tests to see that MyDate creates and displays dates correctly. When you use its
constructor, you are specifying integers, so you do not need to enclose them in quotation marks
as you did for Strings.
Simplifying the class diagram
By the way, BlueJ may be creating a very complicated class diagram. It may be creating
superfluous “uses” arrows whenever one class includes several instance variables of another
class. This seems to be less of a problem with more-recent versions of BlueJ.
My diagram now has “uses” arrows from the Person, Student, and Professor classes to MyDate.
The Student class does not directly use the MyDate class, so I deleted that “uses” arrow.
Retirement of a professor
Recall that I mentioned earlier that a professor at Okanagan College is expected to retire on the
June 30 following his/her 65th birthday.
Let’s create an instance variable for the retirement date, and a getter method. We don’t have a
setter method, since we can calculate the retirement date once we are given the birth date.
When the month of birth is June or earlier, the professor retires on June 30 of the year in which
he/she turns 65.
Definitions – class and object - revisited
Recall that our definition of a class is “A class is a model of something in the real world or it is a
way of implementing a concept.”
The Math class is neither. It represents a collection of methods and constants. All of its methods
are static. Its two constants are static. There is no constructor, no getter, no setter, and no toString
method.