CSC 153 Grinnell College Spring, 2005
 
Computer Science Fundamentals
Laboratory Exercise
 

Notes on Elementary Java

Goals

This lab provides some initial experimentation with Java and the Java programming environment. Programming itself draws upon the in-class discussion of a Course class, as defined in ~walker/java/examples/course/Course.java.

Background Reading

As preparation for these experiments, you should be familiar with the first of the following readings. In addition, you are encouraged to read elements of the second reading as questions arise.

Compiling and Running Java Programs

  1. If you have not done so already, copy ~walker/java/examples/course/Course.java to a java subdirectory of your login directory.

  2. Compile and run Course.java.

  3. Edit Course.java to make a few typographical errors, and try to compile your program. That is, introduce an error, and try to compile. If the program compiles, try to run the changed program. After each attempt, note any error messages, and then change the program back to its correct form. This experimentation will help you become familiar with the Java compiler and some of its error messages.

The main Method

  1. Sun maintains extensive documentation for its library classes. For example, the main documentation page is http://java.sun.com/j2se/1.5.0/docs/. Within this file, locate the documentation for I/O, and follow that link to locate reference material on the java.io package. Within java.io, find the documentation for the PrintWriter class.

  2. Use the Sun documentation for PrintWriter to identify what types of data may be printed using print or println methods. (While the documentation lists numerous print methods, these may be distinguished by the type of parameter used.)

Many types of input and output devices work most efficiently when they handle a block of data at a time. Thus, unless instructed otherwise, they may save up information for input or output until they can move a full block at once. In such circumstances, you may read or write data, but that information may not be transferred physically to the disk or keyboard until enough is present. Typically, an output class contains a flush method to force the data to the output device.

  1. In Course.java, change all println statements to print. Recompile and run the program. Note what happens.

  2. With your program from the previous step, add the line

    
       out.flush();
    

    after the last print statement (but still within the main method). Again, recompile and run the program, and note what happens.

  3. Restore Course.java to its original form (with println) before continuing with the next steps.

Constructors

Class ~walker/java/examples/course/Course.java includes two constructors, and these are invoked in the first few lines of the main method to initialize variables myCourse and yourCourse.

  1. Review Course.java to determine which constructor is used for which variable. When values are not supplied in the definition of yourCourse, read the relevant constructor to determine what default values are used.

  2. Java does not require that variables are declared and initialized in the same statement. Replace the current declarations of myCourse and yourCourse by the following

    
         // declare two courses
         Course myCourse;
         Course yourCourse;
    
         // new initialize the course variables using the two constructors
         myCourse = new Course ("CSC", 152, 4, "Fund. of CS II");
         yourCourse = new Course ("Math");
    

    Compile and run the revised program.

  3. Using the program from the previous step as a base, check that you can use the same constructor to initialize both variables; you need not use different constructors for different variables.

Fields

The Course class as given allows a programmer to change the title and number of a course together and to change the number of credits for the course.

  1. Add a method setSubject that changes the subject of a course to a new string. Then, add some lines in the main method to test this new setSubject method.

Packages

Java allows one class to use other classes. At a simple level, this means than we can perform tests in several ways. A class's main method may contain various test cases. Also, we may write a new class whose primary task is to test the capabilities of another class. Class ~walker/java/examples/course/CourseTests.java serves this second purpose: providing test cases for the Course class.

  1. Copy CourseTests.java to your course directory (the same directory where you placed the file for Course.java). Compile and run the program.

  2. Examine CourseTests.java. How does the code know the details of class CourseTests and of class Course?

  3. Delete course. from the import statement:

    
    import Course;
    

    What happens when you try to compile and run this program?

  4. Move the original CourseTests.java to another directory and package. Then compile and run. You should receive the same output that was obtained initially.


This document is available on the World Wide Web as

http://www.walker.cs.grinnell.edu/courses/153.sp05/labs/lab-intro-java-2.shtml

created April 8, 2001
last revised March 24, 2005
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.