|CSC 153||Grinnell College||Spring, 2005|
|Computer Science Fundamentals|
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.
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.
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.
If you have not done so already, copy ~walker/java/examples/course/Course.java to a java subdirectory of your login directory.
Compile and run Course.java.
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.
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.
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.
In Course.java, change all println statements to print. Recompile and run the program. Note what happens.
With your program from the previous step, add the line
after the last print statement (but still within the main method). Again, recompile and run the program, and note what happens.
Restore Course.java to its original form (with println) before continuing with the next steps.
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.
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.
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.
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.
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.
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.
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.
Copy CourseTests.java to your course directory (the same directory where you placed the file for Course.java). Compile and run the program.
Examine CourseTests.java. How does the code know the details of class CourseTests and of class Course?
Delete course. from the import statement:
What happens when you try to compile and run this program?
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
created April 8, 2001
last revised March 24, 2005
|For more information, please contact Henry M. Walker at firstname.lastname@example.org.|