P Dunn's Super CS1 Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
P Dunn's Super CS1 Site  
 
 
 
Class Notes

Wednesday November 8 , 2000
 
Synopsis
Today we learned how import works in Java,  more Strings and Arrays and we started on the Java class Word.
 

Form of import -
import < enterprise >.   < package >.   *;  - > imports all classes in the package  

Ex:
import java.awt.   *;
import blue.cards.   *;
import white.words.   *;
 

NOTE
Off the CS1 home page we found documentation for the ``blue'' packages.  

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

Ex:
iota (5) - > [1] [2] [3] [4] [5]
iota (1) - > [1]
iota (9) - > [1] [2] [3] [4] [5] [6] [7] [8] [9]  

The Code:
  static private int [] iota (int n)
  {
  / /declare an array of integers
  int a [] = new int [n];
  / /populate the array
  for (int x = 0;  x < = n-1;  x + +)
  {
  a [x] = x + 1;
 
}   return a;
 
}
 

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

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

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

  • The word
  • The syllables of the word
 

Ex:
  [today] [to] 1

  [day] 2

Syllables  

Java code for the class definition  

  public class Word
  {
  / /describe instance variables
  private String word;
  private String [] syllables;
 
  / /constructors
  / /this constructor will create a word by reading
  / /info from the standard input stream and it will read:
  / / * the word
  / / * the # of syllables
  / / * the syllables
 
  today 2 to day
  public Word ()
  {
  word = IO.readString ();
  int numberSyllables = IO.read_int ();
  syllables = new String [numberSyllables];
  for (int x = 0;  x < = numberSyllables;  x + +)
  {
  syllables [x] = IO.readString ();
 
}  
}  
  / /methods
 
  / /compute the length of the word
  / / (# of chars in the word)
  public int length ()
  {
  return word.length ();
 
}  
  / /compute the # of syllables in the word
  public int nrSyllables ()
  {
  return syllables.length;
 
}  
/ /end of word class definition}
 

Define a class which represents a named Dictionary.  

There will be 2 properties

  • 1.   The Name (String)
  • 2.   The Words (word [])
 

  public class Dictionary
  {
  / /instance vars
  private String name;
  private 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 # of word
  / /entries
 
  public Dictionary (String theName)
  {
  name = theName;
  int nrWords = IO.read_int ();
  words = new Word [nrWords];
  for (int x = 0;  x < = nrWords;  x + +)
  {
  words [x] = new Word ();  / /using Word class
 
}  
}  
 

Note:
As things stand we can only use one of these two constructors.