CSC 153: Computer Science Fundamentals | Grinnell College | Spring, 2005 |
Laboratory Exercise Reading | ||
These notes highlight the in-class discussion of a Course class, as defined in ~walker/java/examples/course/Course.java. These notes are not designed to be comprehensive. Rather, they provide a brief commentary on the program.
Mr. Rebelsky maintains additional notes on Java, designed for those just beginning with the language, at http://www.cs.grinnell.edu/~rebelsky/Espresso/. This source provides a range of introductory readings and lab exercises.
The following notes provide a running commentary on the program Course class, as defined in ~walker/java/examples/course/Course.java. Since the notes are not designed to be comprehensive, you may want to consult more substantive sources for a more complete review of the Java programming language. In what follows, commentary starts with the first lines of code and proceeds through to the end.
Java programs identify comments in two ways:
Java organizes classes into modules, called packages. When writing small programs, such an organization may seem unnecessary. However, Java was designed for large-scale applications, in which there may be a very large number of classes. In that context, it is important to keep track of the names of various classes, and there is potential for the developers of one part of the program to use the same class names as have been specified by developers of another part. That is, developers of different parts of a program might use the same class names, and Java would need to determine which class was meant at each stage. Thus, the designers of Java decided to organize classes into packages, so the classes within one package could be distinguished from those in another.
The statement
package course;
Within Unix and Linux systems, the convention is that a package name corresponds to a directory, and all classes within a package are stored within this designated directory.
Within the context of a package, names may be easy to identify. However, within a broader programming context, Java employs a naming convention for packages that reflects the directory structure. Thus, the Java "standard" suggests that you follow a similar style to a Web URL, although the ordering of the elements is reversed. For example, my programs for this course might be in a package that starts with
edu.grinnell.cs.walker.java.examples
Whenever one class wants to reference objects from another class, the program must tell the compiler of the existence of this separate class. This is accomplished with an import statement.
In class Course from file Course.java, the PrintWriter is located within the io package (subdirectory) of the standard java library. To identify this location, the import statement indicates this starting library point, the subdirectory, and the class name:
import java.io.PrintWriter;
Java supports object-oriented problem solving, and all programs in Java begin with classes and objects. Thus, each program component is a class, and a file begins with the declaration of one class. In the example, the class Course is identified as public, meaning that any application is allowed to utilize this class in its work.
Note the file name Course.java has the same name as the class (with .java) appended at the end.
Unlike Scheme, capitalization matters in Java. Thus, a class Course is not the same as a class course.
While not required, a common Java convention is that the names of classes start with a capital letter (e.g., Course), while the names of data fields and methods begin with a lower-case letter.
In Java, fields must be declared at the start of every class or method.
Java also requires that we must specify the type of data to be stored in every variable. Some common data types are:
In class course, fields subject and title are used to store character strings, while number and credits store integer data.
Whenever objects are created from classes, all fields are initialized.
Methods for initializing data have the same name as the class.
Initialization may be done in several ways -- using different methods, provided the constructor methods have different parameters.
In the example, Course has two constructor methods -- both called Course. With the first, one supplies a subject parameter, and other fields are set to 0 or the null string. The second constructor requires the programmer to specify data for all four fields.
If a constructor does not explicitly initialize a field, or if no constructor is provided by the programmer, then Java will initialize all fields according to various default values. For clarity and to avoid oversights, programmers should get into the habit of supplying constructors and explicitly initializing all fields.
Extractor methods return data from various fields, while modifier methods change the values stored in those fields.
Just as in Scheme, an object contains some methods which are available for applications, and an object also may include helper methods which are for local or internal use only. For example, in husk-and-kernel processing, the husk might be generally available, while the kernel would not be.
The definition of every method indicates either what type of data are returned by the method or that no data are returned. In the example,
Methods may or may not have parameters in Java, just as in Scheme. When no parameters are given, method names are followed by parentheses ( ), indicating a procedure or method with no parameters. When parameters are to be supplied, the method definition indicates the names and types of those parameters. Unlike Scheme, Java methods require the programmer to specify what type of data will be supplied as parameters.
In Java, the brackets { and } serve as begin and end markers, just as parentheses do in Scheme.
Java contains strings and string operations that are analogous to those in Scheme.
Once we defined classes in Scheme, we tested and used them in separate code, and Java also allows classes to be used by other classes and programs. In addition, Java allows any class to have a main method, which can be used to run a program based on the given class.
public static void main (String[] args) throws Exception {Here, public and void mean just what has been described earlier:
Also, Java programs may take command line arguments, and such values are handled by specifying an array of strings as parameters (String[] args). While this capability may be useful in running programs, we will not utilize main's parameters for now.
Next, main methods sometimes encounter unusual circumstances in processing that require special handling. The phrase throws Exception provides a mechanism to address these circumstances. Not all main methods face such conditions, but including throws Exception in the code anticipates any potential troubles, should they arise.
Technically, Java requires that a main method belongs to the class; there are not separate main methods for each object. This gives rise to the descriptor static in the declaration of main. While this will become clear over time, for now, it is strongly suggested that you consider the header
public static void main (String[] args) throws Exception {
as an idiom to be used when defining a main method.
The main begins by declaring and initializing objects for later use.
PrintWriter out = new PrintWriter (System.out, true);
the first part of the line identifies the variable out as a variable that will be of type PrintWriter. The second part of the line creates a new PrintWriter object, and uses that object to initialize out.
Once objects are declared and initialized, their methods may be utilized by specifying the object and the desired method -- separated by a dot. Thus, out.println specifies the println method of out, while yourCourse.getCredits references the getCredits method of object yourCourse
Within PrintWriter, the println displays the specified text, and adds a new line character -- ready to continue on the next line. In contrast, print sends the text to the output device, but does not add the new line. Thus, the sequence
out.print(yourCourse.getSubject() + " "); out.println (yourCourse.getNumber() + ": " + yourCourse.getTitle());
displays a subject and some spaces, and continues with the course number and title on the same line. If the println had been used in both cases, then these data would be printed on two separate lines.
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/153.sp05/readings/reading-intro-java-2.shtml
created April 8, 2001 last revised March 24, 2005 |
![]() ![]() |
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |