Man, this class is pretty!

Lecture Notes Two: Classes and Objects


Programming is (essentially) a contact sport.

The fundamental programming unit of the Java programming language is the class.

The lecture notes for today (on Classes and Objects) can be found here:

http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Eleven.html
They belong to a larger set of documents some of which are summarized below (with URLs).

Lecture Notes One
What computers can do and how. Programming languages. Java
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/One.html)

Lecture Notes Two
Algorithms. Programs. Mechanics of implementation in Java
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Two.html)

Lab One: Getting Started
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/labs/One.html)
Lecture Notes Three
Objects and classes, reference types, symbolic names, variables declaration and their initialization through assignment
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Three.html)

Lecture Notes Four
Numbers. Strings. Reading input with ConsoleReader. ConsoleReader revealed (http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Four.html)

Lab Two: Some simple programs, some sample problems
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/labs/Two.html)
Homework Assignment One
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/assignments/A1.html)
Lecture Notes Five
Syntax. The structure of main
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Five.html)

Lecture Notes Six
Boolean values, expressions, and if statements
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Six.html)

Lab Three: Basic programs. Simulating decisions. Working with objects
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/labs/Three.html)

Lecture Notes Seven
Introduction to user-defined types. Classes
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Seven.html)

Lecture Notes Eight
Diagrams for object-oriented concepts
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Eight.html)

Lab Four: Classes and objects
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/labs/Four.html)

Homework Assignment Two
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/assignments/A2.html)
Lecture Notes Nine
Classes, objects, constructors
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Nine.html)

Lecture Notes Ten
Branches and paths. If statement exercises. (L)oops
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Ten.html)

Lab Five: Wrap-up of Classes and Objects material
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/labs/Five.html)

Lecture Notes Eleven
Java review by Dilbert
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Eleven.html)

Lecture Notes Twelve
Loops
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Twelve.html)

Lab Six: Nested loops, other loops, loops and a half, scalable letters
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/labs/Six.html)

Homework Assignment Three
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/assignments/A3.html)

Lecture Notes Thirteen
More practice with loops. Loops, tokenizers, and Monte Carlo problems
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Thirteen.html)

Lecture Notes Fourteen
Computer Games. (In which Tigger has guests from another tale).
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Fourteen.html)

Lab Seven: Loops, Strings, StringTokenizers.
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/labs/Seven.html)

Lecture Notes Fifteen
Designing Fractions.
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Fifteen.html)

Lecture Notes Sixteen
Milestones.
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Sixteen.html)

Homework Assignment Four
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/assignments/A4.html)

Lecture Notes Seventeen
Java arrays.
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Seventeen.html)

Lecture Notes Eighteen
Partially filled arrays. Array parameters and return values. Simple array algorithms.
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Eighteen.html)

Lab Eight: Procedures (Methods)
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/labs/eEight.html)

Lecture Notes Nineteen
Java Fandango
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Nineteen.html)

Lecture Notes Twenty
Help with Homework Four
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/Twenty.html)

Lab Nine: Working with Methods and Arrays
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/labs/Nine.html)

Lecture Notes Twenty-One
Parallel arrays. Arrays of object data. Vectors.
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/lectures/TwentyOne.html)

Lab Ten:
Classes and objects revisited.
(http://www.cs.indiana.edu/classes/a201-dger/spr2002/labs/Ten.html)

The front page of A201/A597 can be found here:
(http://www.cs.indiana.edu/classes/a201-dger/)


Here's the problem for today. This problem has several parts:

  1. Write a simple Vehicle class that has fields for (at least) current speed, current direction in degrees, and owner name.

  2. Add a static field to your Vehicle class for the highest Vehicle Identification Number issued, and a non-static field that holds each vehicle's ID number.

  3. Write a main method for your Vehicle class that creates a few vehicles and prints out their field values. Note that you can also write a separate tester program as well.

  4. Add two constructors to Vehicle. A no-arg constructor and one that takes an initial owner's name. Modify the tester program from the previous step and test your design.

  5. Make the fields in your Vehicle class private, and add accessor methods for the fields. Which fields should have methods to change them and which should not?

  6. Add a changeSpeed method that changes the current speed of the vehicle to a passed-in value, and a stop method that sets the speed to zero.

  7. Add two turn methods to Vehicle. One that takes a number of degrees to turn, and one that takes simply either a Vehicle.TURN_LEFT or a Vehicle.TURN_RIGHT constant. Define the two constants accordingly.

  8. Add a static method to Vehicle that returns the highest identification number used so far.

  9. Add a toString method to Vehicle.
At any stage you should test what you have in a main method.


Here's a collection of problems of increasing difficulty, to be used as a starting point:

The URL for this module:

(http://www.cs.indiana.edu/classes/a348/CTED/moduleFour/)


Last updated: Mar 25, 2002 by Adrian German for A348/A548/T540/NC010