0% found this document useful (0 votes)
3 views6 pages

201801Solution

The document contains Java code for generating random letters, identifying pairs of letters, and processing words from a file to check for 'homohemigrams'. It includes methods for reading characters, checking if two characters form a pair, and determining if a word is a homohemigram based on its structure. The main class orchestrates file reading and writing operations to filter and save valid words.

Uploaded by

powerpau1057yt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

201801Solution

The document contains Java code for generating random letters, identifying pairs of letters, and processing words from a file to check for 'homohemigrams'. It includes methods for reading characters, checking if two characters form a pair, and determining if a word is a homohemigram based on its structure. The main class orchestrates file reading and writing operations to filter and save valid words.

Uploaded by

powerpau1057yt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Word[] words = new Word[MAX];

words[numWords].read();

words[numWords] = new Word();

words[numWords] =
Word.read();








public void start() {
// Constant to hold the maximum amount of chars
final int MAX = 200;

// 1. Declare and create the array


char[] chars = new char[MAX];

// 2. Fill the array using a random letter from 'a' - 'z'


for (int i = 0; i < MAX; i++) {
chars[i] = getRandomLetter();
}

// 3. Find the pairs and print them


int i = 0; // current element: first
char letter1; // current letter
char letter2; // next letter

// while not end: until penultimate character


while (i < MAX - 1) {
// process current element
letter1 = chars[i];
letter2 = chars[i + 1];
if (isAPair(letter1, letter2)) {
System.out.println(letter1 + " " + letter2 + " " + i);
}
// placement on next element
i++;
}
}

public char getRandomLetter() {


int numLetters = 'z' - 'a' + 1; // This uses "ASCII values"
// Math.random() -> random double in [0, 1).
// Multiplied by numLetters -> random double in [0, numLetters).
// Adding 'a' (97) -> random double in [97, 97 + numLetters = 123 =
'{')
// Casted to char -> random char in ['a', '{') = ['a', 'z']
return (char) (Math.random() * numLetters + 'a');
}

public char getRandomLetterUsingAnArray() {


final char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
// Refer above method for multiplying a random number [0, 1).
// Alternative, Random class and nextInt(alphabet.length);
return alphabet[(int) (Math.random() * alphabet.length)];
}

public boolean isAPair(char c1, char c2) {


return isAVowel(c1) && c2 == 'm';
}

public boolean isAVowel(char c) {


return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class Exercise3 {

public void start() {


try {

// 1. open both files


FileReader fr = new FileReader("words.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("homohemigrams.txt");
BufferedWriter bw = new BufferedWriter(fw);
// 2. iterate the first file
String line = br.readLine(); // 1st element
while (line != null) { // while not end
// process current element
Word word = new Word(line.toCharArray());
if (word.isHomohemigram()) {
// write it in the second file
bw.write(word.toString());
bw.newLine(); // writing '\n' would also be OK
}
// placement on next element
line = br.readLine();
}

// 3. close both files


bw.close();
fw.close();
br.close();
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {


new Exercise3().start();
}

}
public class Word {
// maximum length a word can be
final static int MAX = 20;
// actual data of the word:
// characters
private char[] letters;
// actual length
private int length;

// Constructor: from a char[]


public Word(char[] chars) {
// characters of the word: MAX positions
letters = new char[MAX];
// as many as chars, but not more than MAX
length = Math.min(chars.length, MAX);
// save these characters
for (int i = 0; i < length; i++) {
this.letters[i] = chars[i];
}
}

// Checks if this word is an homohemigram


public boolean isHomohemigram() {
if (length % 2 != 0) {
// words with an even number of characters
// cannot be homohemigrams
return false;
} else {
int i = 0; // first character of the first half
int j = length / 2; // first character of the second half
boolean ok = true; // assume it is an homohemigram
// find the first pair of characters that are not equal
// stop when found or when j reaches the end of the word
while (ok && j < length) {
// process current element
ok = letters[i] == letters[j];
// placement on next element
i++;
j++;
}
// return if we still assume it is an homohemigram
return ok;
}
}

@Override
public String toString() {
String a = "";
for (int i = 0; i < length; i++) {
a += letters[i];
}
return a;
}
}

You might also like