Return to Jeff's Main Page

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
My Intro to Object-Oriented Programming  
 
 
Programming Challenge Archive

Laboratory Challenges
String Thing
 
This laboratory introduced us to the String Class of the java.lang package.   We engaged in some simple string processing and wrote some operators.  

 
  Laboratory Challenges  -- String Thing

   // General Information
   // -------------------------------------------------------+
   -----
  
   // File:  StringThingApp.java
   // Type:  java application file
   // Date:  November 1998
   // Name:  BLUE
   // Line:  Simple String Processing Application
  
   // Application Description
   // ------------------------------------------------
  
   /*
  
      Simple String processing application.
  
   */
  
  
   // Required Packages
   // -------------------------------------------------
  
      import blue.io.*;
  
  
   // Application Class
   // ------------------------------------------------
  
      class StringThingApp
      {
      // main method
         static public void main (String args[])
         {
         // Create and print some strings which represent nam+
   es
            String singer = "Holliday, Billie";
            String sculptor = "Claudel, Camille";
            // Point A
            String self = "Norkoli, Jeff";         
            IO.println(singer);
            IO.println(sculptor);
            IO.println(self);
  
         // Computer and print the lengths                   +
     
            // Point B
            int singerLength = singer.length();
            int sculptorLength = sculptor.length();
            int selfLength = self.length();
            IO.println(); IO.println("Lengths ...");
            IO.println(singerLength);
            IO.println(sculptorLength);
            IO.println(selfLength);
  
         // Computer and print first names                   +
     
            // Point C
            String singerFirst = singer.substring(10);
            String sculptorFirst = sculptor.substring(9);
            String selfFirst = self.substring(9);
    IO.println(); IO.println("First names ...");
            IO.println(singerFirst);
            IO.println(sculptorFirst);
            IO.println(selfFirst);
  
         // Computer and print last names                    +
     
            // Point D
            String singerLast = singer.substring(0,8);
            String sculptorLast = sculptor.substring(0,7);
            String selfLast = self.substring(0,7);
            IO.println(); IO.println("Last names ...");
            IO.println(singerLast);
            IO.println(sculptorLast);
            IO.println(selfLast);
  
         // Computer and comma position within names         +
     
            // Point E
            int singerPos = singer.indexOf(",");
            int sculptorPos = sculptor.indexOf(",");
            int selfPos = self.indexOf(",");
            IO.println(); IO.println("Comma positions ...");
            IO.println(singerPos);
            IO.println(sculptorPos);
            IO.println(selfPos);
  
         // Computer and print first names                   +
    
            // Point F1
            singerFirst = firstName(singer);
            sculptorFirst = firstName(sculptor);
            selfFirst = firstName(self);
            IO.println(); IO.println("First names ...");
            IO.println(singerFirst);
            IO.println(sculptorFirst);
            IO.println(selfFirst);
  
         // Computer and print last names                    +
  
            // Point G1
            singerLast = lastName(singer);
            sculptorLast = lastName(sculptor);
            selfLast = lastName(self);
            IO.println(); IO.println("Last names ...");
            IO.println(singerLast);
            IO.println(sculptorLast);
            IO.println(selfLast);
  
         // Computer and print full names                    +
  
            // Point H1
            String singerFull = fullName(singer);
            String sculptorFull = fullName(sculptor);
            String selfFull = fullName(self);
            IO.println(); IO.println("Full names ...");
            IO.println(singerFull);
            IO.println(sculptorFull);
            IO.println(selfFull);
  
         }
  
      // Point F2
      // operator-style method to compute first name
          static private String firstName(String fname)
          {
      int space  = fname.indexOf(" ");
      String first = fname.substring(space);
      return first;
          }
      
          
      // Point G2
      // operator-style to compute last name
          static private String lastName(String lname)
          {
      int comma = lname.indexOf(",");
      String last = lname.substring(0,comma);
      return last;
          }      
  
  
      // Point H2
      // operator-style to compute full name
          static private String fullName(String fullname)
          {
      int comma = fullname.indexOf(",");
      int space = fullname.indexOf(" ");
      String full = fullname.substring(comma + 2) 
          + fullname.substring(space, space + 1) +
          fullname.substring(0, comma);
      return full;
          }
         
     
      }
  
   // Demo
   // --------------------------------------------
  
   /*
   $ javac StringThingApp.java
   $ java StringThingApp
   Holliday, Billie
   Claudel, Camille
   Norkoli, Jeff
  
   Lengths ...
   16
   16
   13
  
   First names ...
   Billie
   Camille
   Jeff
  
   Last names ...
   Holliday
   Claudel
   Norkoli
  
   Comma positions ...
   8
   7
   7
  
   First names ...
    Billie
    Camille
    Jeff
  
   Last names ...
   Holliday
   Claudel
   Norkoli
  
   Full names ...
   Billie Holliday
   Camille Claudel
   Jeff Norkoli
   $ 
   */