
CISC 370 Lecture Notes for Class No. 3, February 15, 2000

| Course Home | Prev Lect | Next Lect | Example Progs | Exercise Solns | CoreJava Progs Documentation |
Gen'l Docs | Java 2 APIs | Java Glossary | Term Proj Info |
script // Produces a file named typescript whoami date pwd cat *.java javac *.java // mainFile should be the name of the file containing java mainFile // your main() method exit // to exit script
Now attach the typescript file to an email message to Sivaram Burra at burra@cis.udel.edu. Also bring a hard copy to lecture so that he will not have to print all the assignments, a time-consuming task.
1. Define a class StringSortMachine that contains the following: (i) A constructor
StringSortMachine( String[] A, int n )
that takes an array A of strings to sort and an integer n>=0 that
tells how many of the elements of the array A we wish to process.
public void sort( )
that sorts the strings of a StringSortMachine into alphabetical
order ignoring case.
public void print()
method that prints the strings of a StringSortMachine, one
string per line.
Now write a main method that uses Console.readWord() to read a series of words terminated by a period and sort them. For simplicity, assume that there is a space between the last word and the period. Print the sorted words.
Since this is a "longish" exercise (but not difficult) and there is a "shortish" time between now and Thursday, we will make this due on Tues, 2/22.
Most real software projects are very large and require a coherent overall design. This design in the large can benefit fom some overall design methodologies. Such issues can be best understood and appreciated after some experience with grappling with the details of designing and implementing a variety of particular programming problems, that is, of dealing with object-oriented programming in the small. One of the key objectives of this course is to provide such experience. There is an entire course devoted to object-oriented design in the large; it is CISC 475/675 - Object-Oriented Software Engineering. It is an excellent follow-up to this course and I highly recommend it to you.
Richard Gabriel has written a nice book Patterns of Software - Tales from the Software Community dealing with object-oriented programming and related issues. I have included a short passage from the Preface of his book that I thought was interesting and relevant to this discussion. The book is almost more philosophy than a technical programming book - very interesting.
Because of the way (i) is handled in Java, many Java classes need and equals() method. Because of the way (iii) is done in Java, many classes need a clone() method.
The Console class has a total of six methods. (The text, on p. 67, says that this class has only three methods, but my CD had five - the five that you will see in the online documentation. A CD that I had from an earlier edition of this book had six methods - although the version of each is listed as 1.10 10 Mar 1997. Since I found the sixth method, namely, Console.readWord() useful, I have added it to the version of the Console class found on strauss. You can copy it to your PC is you like. Here is the documentation:
/**
* read a word from the console. The word is
* any set of characters terminated by whitespace
* @return the 'word' entered
*/
public static String readWord()
)
public static void swap( Object obj1, Object obj2 ){
Object t = obj1;
obj1 = obj2;
obj2 = t;
}
The if, if-else, switch and for statements are essentially just like they are in C/C++. However, note that variables declared in the initialization field of a for statement have scope that extends only to the end of the body of the for statement. So you cannot do the following.
Str[0] = Console.readWord();
for (int i = 1; i < Str.length-1 && !Str[i-1].equals("."); i++ ){
Str[i] = Console.readWord();
}
int n = i-1; // Error - variable i no longer in scope
This is also true in the latest versions of C++.
(i) The java.lang.String class
(ii) The classes corejava.Console and
corejava.Format. Note that the online documentation
for these classes can be accessed by clicking on the
Documentation
link under the
Text Programs
link in the buttons at the tops of the course web pages.
(iii) Differences between primitive types and objects in Java.
(iv) Differences between using == and the
boolean equals() method. When to use each.
(v) Java arrays. Array assignment (not possible in C++). Array
return types (also not possible in C++; although a pointer
to an array can be a return value in C++ and this is
really all that Java does).
(vi) Passing of arguments in Java - always by value. This is the
same as passing by value in C++ for primitive types, but is
really the C++ pass by reference for object types.
Back to the
CISC 370 homepage.
This page has been accessed
times since 15 Feb 2000.
Corrections, suggestions and comments to Bob Caviness
Last updated 17:20 Feb 15, 2000.
Copyright
2000 B. F. Caviness