For this assignment, you will develop Java programs to solve several problems. Y
ID: 3545300 • Letter: F
Question
For this assignment, you will develop Java programs to solve several problems. You will also write test cases with which to test the solutions.
If you know some Java already, you may use advanced features that we have not covered in class yet (though it is not necessary to do so). However, you may NOT use any modules or classes from outside java.lang (do not add any "import" statements to your programs).
FOR ALL PROBLEMS you should output your answers via the IO module as in previous assignments.
You may find some or all of the following methods of the Character module useful:
Remember that a character is not an object in Java, so you cannot invoke methods on a character in the same way that you would on a string. You call methods of this module as in this example:
You may also need to use some or all of the following String methods:
Write your code in the file PigLatin.java. Write your test cases in assign4-testcases.txt.
"Pig Latin" is a fake language used as a children's game. A word in English is "translated" into Pig Latin using the following rules:
(This is a simplified dialect of Pig Latin, of course.)
Ask the user for a word (one string) and output its Pig Latin translation (one string). You may assume that the input does not contain digits, punctuation, or spaces. The input may be in any combination of uppercase or lowercase. The case of your output does not matter. Use IO.outputStringAnswer() to output.
Write your code in the file WordCount.java. Write your test cases in assign4-testcases.txt.
Write a program that takes a string containing a sentence or a set of sentences, and counts the number of words in the sentence that meet or exceed a specified minimum length (in letters). For example, if the minimum length entered is 4, your program should only count words that are at least 4 letters long.
Input the string and the minimum word length (integer), in that order, and output the word count (integer). Words will be separated by one or more spaces. Non-letter characters (spaces, punctuation, digits, etc.) may be present, but should not count towards the length of words.
Hint: write a method that counts the number of letters (and ignores punctuation) in a string that holds a single word without spaces. In your main program, break the input string up into words and send each one to your method.
Write your code in the file Compress.java. Write your test cases in assign4-testcases.txt.
Run-length encoding (RLE) is a simple "compression algorithm" (an algorithm which takes a block of data and reduces its size, producing a block that contains the same information in less space). It works by replacing repetitive sequences of identical data items with short "tokens" that represent entire sequences. Applying RLE to a string involves finding sequences in the string where the same character repeats. Each such sequence should be replaced by a "token" consisting of:
If a character does not repeat, it should be left alone.
For example, consider the following string:
After applying the RLE algorithm, this string is converted into:
In the compressed string, "9w" represents a sequence of 9 consecutive lowercase "w" characters. "5e" represents 5 consecutive lowercase "e" characters, etc.
Write a program that takes a string as input, compresses it using RLE, and outputs the compressed string. Case matters - uppercase and lowercase characters should be considered distinct. You may assume that there are no digit characters in the input string. There are no other restrictions on the input - it may contain spaces or punctuation. There is no need to treat non-letter characters any differently from letters.
Explanation / Answer
Complete Programs:
File: PigLatin.java
// PigLatin class implementation
public class PigLatin
{
// start main method
public static void main(String[] args)
{
String word;
String result;
IO.print("Enter a word: ");
word = IO.readString();
char firstCharAsUpper = Character.toUpperCase(word.charAt(0));
if(firstCharAsUpper == 'A' || firstCharAsUpper == 'E' || firstCharAsUpper == 'I' || firstCharAsUpper == 'O' ||firstCharAsUpper == 'U')
result = word + "way";
else
result = word.substring(1) + word.charAt(0) + "ay";
IO.outputStringAnswer("Pig Latin word: " + result);
} // end of main method
} // end of PigLatin class
File: WordCount.java
// WordCount class implementation
public class WordCount
{
// countWords method implementation
public static int countWords(String original, int minLength)
{
int numOfWords = 0;
int letterCounter = 0;
for(int i = 0; i < original.length(); i++)
{
if(original.charAt(i) != ' ')
{
if(Character.isLetter(original.charAt(i)))
letterCounter++;
}
if(original.charAt(i) == ' ' || i == original.length() - 1)
{
if(letterCounter >= minLength)
numOfWords++;
letterCounter = 0;
}
}
return numOfWords;
} // end of countWords method
// start main method
public static void main(String[] args)
{
IO.println("What is the minimum characters you would like a word to have?");
int minLength = Integer.parseInt(IO.readLined());
IO.println("What is the sentence?");
String original = IO.readLine();
IO.outputStringAnswer("Number of words: " + (countWords(original, minLength)));
} // end of main method
} // end of WordCount class
File: Compress.java
// Compress class implementation
public class Compress
{
// start main method
public static void main(String[] args)
{
String word;
String result = "";
IO.print("Enter a word: ");
word = IO.readString();
for(int i = 0; i < word.length(); i++)
{
int count = 1;
while(i + 1 < word.length() && word.charAt(i) == word.charAt(i + 1))
{
count++;
i++;
}
if(count != 1)
result = result + count;
result = result + word.charAt(i);
}
IO.outputStringAnswer("Compressed word: " + result);
} // end of main method
} // end of Compress class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.