



|
|
Byron's CSC212 Web Site
|
Class Notes
Wednesday November 8 , 2000
|
|
|
Word Dictionary
Class Notes --
Wednesday November 8 , 2000
CSC212 - November 8, 2000
======================================
Lecture Topic: WORD CLASS
Blue.card.*
import blue.cards.*;
import <enterprise>.<package>.* //import all the class
Ex: import java.awt.*
import.cards.*
import white.words.*
NOTE: Off the CS1 Homepage 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.
___________________
Ex: iota(5) => | 1 | 2 | 3 | 4 | 5 |
|___|___|___|___|___|
___
iota(1) => | 1 |
|___|
Written in Java:
// an arrray of ints will be returned
// method name is iota
static private int[] iota(int n)
{
// assignment & declaration
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 (ie makes a
copy of) an array of character strings.
// An array of Strings will be returned
static private String[] copy(String[] s)
{
// c = copy
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
_______ _____ ___
Ex: | | | to | | |
| today | |_____| | 2 |
|_______| | day | |___|
|_____|
The Word - Syllables - Stress
_____________ ______ ___
| | | Ex | | |
| Examination | |______| | 4 |
|_____________| | am | |___|
|______|
| in |
|______|
| a |
|______|
| tion |
|______|
Java code for Class Defination
public class Word
{
// Instance Variables
private String word;
private String[] syllables;
//
/* Constructors - give values to instance variables
This constructor will create a word by reading
information from the standard input stream.
It will read...
+ the word
+ the number of syllables
+ the syllables
Ex: today 2 to day
Examination 5 Ex am in a tion
*/
public Word()
{
word = IO.readString();
int ntSyllables = IO.read_int();
syllables = new String [nrSyllables];
{
syllables [x] = IO.read_String();
}
}
// Methods
// Compute the length of the word
// (# of characters in the word)
// public = returns a value
// "int" = what returns
public int length()
{
// "word" small first letter = character string
return word.length();
Word w = new Word();
String s = IO.read_String();
int x = w.length(); // word
int y = s.length(); // String
// Compute the number of syllables in the word.
public int nrSyllables()
{
return syllables.length; // syllables = array +
}
// End class definition
Define a Class which represents a named dictionary.
There will be two properties.
1) The name (String) // String
2) The words (Word[]) // Array
public class Dictionary
{
// Instance Variables
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 a standard input
// file which begins with an integer indicating
// the number of word entries.
___________________
// The File: | 70 |
| today 2 to day |
| blue 1 blue |
| ... |
|___________________|
public Dictionary (String theName)
{
name = theName;
int nrWords = IO.read_int();
words = new Words[nrWords];
for (int i = 0; i < nrWords; i++)
{
words[i] = new Word();
}
}
Note: As things stand, we can only use one of these
constructs.
=========================================================
123456789112345678921234567893123456789412345678951234567
|
|
|