CISC 370 Lecture Notes for Class No. 3, February 15, 2000
Further Comparisons Between Java and C++

Course Home Prev Lect Next Lect Example Progs Exercise Solns
CoreJava Progs
Documentation
Gen'l Docs Java 2 APIs Java Glossary Term Proj Info

Graded Assignment #2

To prepare the assignment for submission, do the following:

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.

Reading:
Horstmann & Cornell, chap. 4; This takes through p. 161 of the text, quite a bit of reading to do! But most of this should be review. Nonetheless, it should be read carefully for some of it is new and other things about C++ you may have forgotten. Do not fall behind at this point!
Exercises:

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.
(ii) A method
          public void sort( )
          
that sorts the strings of a StringSortMachine into alphabetical order ignoring case.
(iii) A
          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.

Today's Lecture Topics

  1. OOP - explicit and implicit lessons. Last lecture, we explicitly looked at object-oriented design issues related to generating random sentences. We will not explicitly talk about object-oriented design on a regular basis in this course, but there will be, nonetheless, many opporunities to learn about object-oriented design, primarily by example. You will see many examples of good object-oriented design in the textbook, in the lectures, and especially in the class libraries for Java, itself. For the most part, these will be good examples of the details of well-designed small programs as implemented in one object-oriented programming language, Java. The exception is the Java class libraries where you can see good examples of both design in the large (the overall structure of the class libraries, the components chosen as the basic objects, etc) and design in the small (the implementation of individual objects).

    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.

  2. Importing (Java) and including (C++): similarities and differences. How the CLASSPATH and import statements work together to tell the Java system into which directories to look for files.

  3. Online documentation

  4. Some issues in Java that are different from C++: (i) the == operator, (ii) passing parameters to methods, and (iii) the assignment operator.

    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.

  5. Classes provided by the text to ease console input/output: The classes corejava.Console and corejava.Format.

    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()
          
    )
    You can see what these are and what they by do by clicking on the Documentation link under Text Programs in the navigation bar at the top of this page. We now present a simple demo of the use of the Console class to read in a series of words and print them in reverse order.

  6. Passing parameters to Java methods - how it is done. All parameters are passed to Java methods by value - but with the caveat that only the pointer to an object is passed by value. Thus, for example, one cannot write a generic swap program in Java. That is, the following Java program accomplishes nothing.
          public static void swap( Object obj1, Object obj2 ){
              Object t = obj1;
              obj1 = obj2;
              obj2 = t;
          }
          

  7. Control statements - differences from C/C++

    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++.

  8. Summary: Some important topics from Chap. 3

    (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