Summary: The following notes highlight the in-class discussion of a Course class, as defined in ~walker/public_html/courses/153.sp02/Course.java. These notes are not designed to be comprehensive. Rather, they provide a brief commentary on the program -- starting at the first lines of code and proceeding through to the end.
Java programs identify comments in two ways:
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.
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.
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.
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) {
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.
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) {
as an idiom to be used when defining a main method.
SimpleOutput out = new SimpleOutput ();the first part of the line identifies the variable out as a variable that will be of type SimpleOutput. The second part of the line creates a new SimpleOutput object, and uses that object to initialize out.
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.cs.grinnell.edu/~walker/courses/153.sp02/lab-intro-java-2.html
|
created April 8, 2001 by Henry M. Walker last revised March 28, 2002 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |