CS1 Course Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
CS1 Course Site  
 
 
 
Class Notes

Wednesday November 8 , 2000
 
  blue.card.   *
  import blue.cards *;
  import < enterprise >.   < package >.   *
 

* means to import all classes.  

Examples:  

  import java.awt.   *
  import blue.cards.   *
  import white.words.   *
 

Note:  

Off of the CS1 home page you will find documentation for the ``blue'' packages.  

Write an operator style method called iota which takes one positive integer as its sole parameter and returns the array of integers containing 1,  2,  3,  ...   ,  n -- where n is the parameter.  

Example:  

  iota (5)
  iota (1)
  iota (9)
 

Solution:  

  static private int [] iota (int n)
  {
      int a [] = new int [n];
      for (int x = 0;  x < = n - 1;  x = x + 1)
      {
        a [x] = x + 1;
     
}       return a;
 
}
 

Write an operator style method which copies (makes a copy of) an array of character strings.  

  static private String [] copy (String [] s)
  {
      String c [] = new String [s.length];
      for (int i = 0;  i < s.length;  i + +)
      {
        c [i] = s [i];
     
}       return c;
 
}
 

Define a class called Word which can be used to store Words words in terms of:

  • the word
  • the syllables of the word
  • the syllable number which is generally accented
 

Java code for the class definition  

  public class Word
  {
  / /instance variables.
        private String word;
        private String [] syllables;
  / /Constructors.
      (This constructor will create a word
      by reading information from the standard
      input stream.   It will read:   the words,  the
      number of syllables,  and the syllables.   )
  public Word ()
  {
        word = IO.readString ();
        int nrSyllables = IO.read_int ();
        syllables = new String [nrSyllables];
        for (int x = 0;  x < nrSyllables;  x + +)
        {
          syllables [x] = IO.readString ();
       
}  
}  
  / /Methods.
      Compute the length of the word.
  public int length ()
  {
        return word.length ();
 
}  
  / /Compute the number of syllables
      in the word.
  public int nrSyllables ()
  {
        return syllables.length;
 
}  
}
 

Define a class which represents a named dictionary.   There will be two properties:

  • the name (String)
  • the words (Word [])
 

  public class Dictionary
  {
  / /Instance variables.
      private String name;
      provate Word [] words;
 
  / /Constructor # 1:   Create an
      empty dictionary.
  public Dictionary (String theName
  {
      name = theName;
      words = new Word [0];
 
}  
  / /Constructor # 2:   Create a dictionary
      by reading elements (word entries)
      from the standard input file which begins
      with an integer indicating the number of
      word entries.
  public Dictionary (String theName)
  {
      name = theName;
      int nrWords = IO.read_int ();
      words = new Words [nrWords];
      for (int i = 0;  i < nrWords;  i + +)
      {
        word [i] = new Word ();
     
}  
}
 

Note:  

As things stand we cna only use one of these two constructors.