Unit 1 Lesson 2 - Java Lab

Lesson summary and assignments

In this lesson you are provided a Java source code file. Source code contains the human readable instructions telling the computer what to do. Java is an object-oriented language which means you can define blueprints for objects and later create as many of those objects as you want from that blueprint.

In Java, a blueprint is a class. To define a class, you first tell whether the blueprint is available to all code (public) or only to certain code (private). You then specify the keyword "class" followed by the name you want to give to the class such as NeighborhoodRunner. For example, the following code defines a class (blueprint) with which you can create many NeighborhoodRunner objects:

public class NeighborhoodRunner{ … }

The actual code which describes/defines the blueprint would be where the three dot ellipses are between the open and closing curly braces in the example above. Note that Java is a case-sensitive language so the keywords public and class must be all lower case as defined by the language. You could not use Public or Class because the case of the first letter would be wrong. Similarly, if you put upper and lower case letters in terms you define such as the name of a class (NeighborhoodRunner), use must always use that exact same letter casing in the future. In fact, you could define two distinct class blueprints, one whose name is NeighborhoodRunner and the other whose name is neighborhoodrunner.

A class blueprint can define two types of things:

  1. Methods which define behaviors an object can do
  2. Other objects of any type which are contained within and can be used by that blueprinted object to accomplish desired actions.

A class' methods or contained objects can be defined as private so they can only be used by the class object itself, or public such that other code outside of the object can directly use the method or contained object.

The NeighborhoodRunner class does not directly hold any other objects. It just has its’ one method called “main” will be executed by the Java runtime when this program is run. Although the NeighborhoodRunner class does not itself contain any objects, methods within a class can create temporary objects which are created when the method starts and disappear when the method finishes. This program’s “main” method creates a Painter object called sofia which only exists until the “main” method finishes executing.

When a class or method wants to create an object for its use, it first specifies the name of the class blueprint for the desired object, then a name by which the instantiated (created) object will be known for use, then an equal sign, the word “new” and then the name of the blueprint (class name) of the desired object.

Once you have and object created (instantiated), you access its’ public data or behavior methods with the dot notation. That is, you specify the name you have assigned to the created object, type a period, then type the name of the date or method you want to access.

In this program, the “main” method creates a Painter object and assigns that object the name sofia. The class blueprint for Painter objects is defined outside of this program. That definition apparently contains public methods move, takePaint, turnLeft, and paint. We don’t know how Painter objects do these things. We just ask Painter objects to do them, and they do them. That is one principle of object oriented programming: you are able to use objects without knowing how they work, and trust that they will do what they are supposed to do.

Vocabulary

class header
consists of the class keyword and the name of the class
comment
a text note to explain or annotate the code and is ignored when the program is run
source code
a collection of programming commands
syntax
the rules for how a programmer must write code for a computer to understand
syntax error
a mistake in the code that does not follow a programming language's syntax