SearchingTopStrings and CharactersSummary

Summary

Here is a brief summary of what must be done to convert a Grace program to Java:

  1. You must put a semicolon at the end of lines in Java. However, never put a semicolon before a { or after a }. Proper indenting is optional (but required by your instructors). Use the Format command under the Source menu in Eclipse.
  2. Parameterless method declarations and requests must include () even though there are no parameters. This is how Java tells the difference between a method and an instance variable.
  3. Every Java class must be in a separate file.
  4. Every instance variable, constant, and method must have a visibility annotation. For now, use one of private and protected. Do not put these in front of local variables.
  5. Types are written before the identifiers in declarations of variables and methods. E.g., int x = 5; and public void onMouseClick(Location point).
  6. Rather than a single Number type, Java has many. The type int is a primitive type holding integers, while double refers to numbers with decimal points. An int can be easily used as a double, but to convert a double to an int you must use a type cast: (int)myDouble or Math.round(myDouble, depending on whether you want to truncate the value or round it.
  7. The Java types int, double, and boolean are primitive types, which mean that you may not make method requests of them. Object types should always be written with initial letter capitalized (e.g., FramedRect).
  8. Java is statically typed, so every identifier introduced must be given a type: either a class or an interface. Interfaces are like Grace types, with no implementation information.
  9. Assignments to variables are written with = instead of :=.
  10. Constants are written in all caps and are declared as static final if they are the same for all objects of the class. If they depend on parameters to the class or method, just declare them as final.
  11. Uninitialized instance variables are given the value 0 (if a primitive type) or null (if an object type). If you forget to initialize an instance variable you will get a "null pointer error" when you attempt to access it. Local variables are not automatically initialized. You must make sure to initialize them.
  12. In Java we use keyword this in place of self. If this is the receiver of a method request you may leave out this. If a method parameter and an instance variable of the same class have the same name, placing a "this." before the variable makes it refer to the instance variable. A common idiom is using the same name for a constructor parameter and instance variable and then using this in front of the instance variable when initializing it.
       class C {
          ...
          private String strVal;
          ...
          public C(String strVal) {
              this.strVal = strVal;  // right side is the parameter, 
                                            // left side is instance variable
          }
          ...
      }
    
  13. You may overload methods and class constructors in Java (i.e., have more than one constructor or method with the same name). To be legal you must be able to distinguish between them with the number and types of parameters.
  14. There are minor differences in the names of methods in the Java objectdraw library compared to Grace. Keep the objectdraw API documentation open at
    http://www.cs.pomona.edu/classes/cs051/handouts/objectdraw-api.html.

SearchingTopStrings and CharactersSummary