These are the parameters that I need the code revised by: 1. NO GLOBAL VARIABLES
ID: 3527411 • Letter: T
Question
These are the parameters that I need the code revised by:
1.NO GLOBAL VARIABLESare allowed !!!! -- with the exception of file input / output variables.
2.All variables must be either declared locally in the method or passed as parameters.
3. Keep in mind that the only parameters that are passed between methods are those required to transmit shared information. DO NOT engage in "over-kill"by passing each and every variable declared in main to each and every method in your class!!
4. Some methods may need to be changed to return values to the calling routine. If that is the case, another variable must exist on the left side of the assignment statement in the calling routine as the receiving variable, with the method call on the right side.
import java.io.*;
import java.util.StringTokenizer;
public class vowels_r_us
{
private static FileInputStream inFile;
private static InputStreamReader inReader;
private static BufferedReader reader;
private static StringTokenizer strTkn;
private static String line, word, suffix, wordPlural, wordSuffix, substring, substringSuffix;
private static int wordLength, suffixLength, i, j;
private static char lastChar, secondLastChar, firstChar;
public static void main (String args[]) throws IOException
{
initFile();
getData();
inFile.close();
}
public static void initFile() throws IOException
{
inFile = new FileInputStream ("c:\vowels.txt");
inReader = new InputStreamReader(inFile);
reader = new BufferedReader(inReader);
}
public static void getData() throws IOException
{
line = reader.readLine();
while(line != null)
{
strTkn = new StringTokenizer(line);
word = strTkn.nextToken();
suffix = strTkn.nextToken();
formPlural();
addSuffix();
printResults();
line = reader.readLine();
}
}
public static void formPlural()
{
wordLength = word.length();
i = wordLength-1;
lastChar = word.charAt(wordLength-1);
secondLastChar = word.charAt(wordLength-2);
if (lastChar == 'A' || lastChar == 'C' || lastChar == 'S' || lastChar == 'L'){
if (secondLastChar == 'A' || secondLastChar == 'C' || secondLastChar == 'S' || secondLastChar == 'L'){
wordPlural = word + lastChar + 'H';
}else{
substring = word.substring(0, i);
wordPlural = substring + 'G';
}
}else{
if (secondLastChar != 'A' && secondLastChar != 'C' && secondLastChar != 'S' && secondLastChar != 'L'){
wordPlural = word + lastChar + 'H';
}else{
wordPlural = word + 'G' + 'H';
}
}
}
public static void addSuffix()
{
firstChar = suffix.charAt(0);
j = wordLength-2;
if (firstChar != 'A' && firstChar != 'C' && firstChar != 'S' && firstChar != 'L'){
if (lastChar == 'A' || lastChar == 'C' || lastChar == 'S' || lastChar == 'L'){
if (secondLastChar == 'A' || secondLastChar == 'C' || secondLastChar == 'S' || secondLastChar == 'L'){
substring = word.substring(0, j);
wordSuffix = substring + lastChar + suffix;
}else{
wordSuffix = word + firstChar + suffix;
}
}else{
if (secondLastChar != 'A' && secondLastChar != 'C' && secondLastChar != 'S' && secondLastChar != 'L'){
wordSuffix = substring + lastChar + suffix;
}else{
wordSuffix = word + suffix;
}
}
}else{
if (lastChar == 'A' || lastChar == 'C' || lastChar == 'S' || lastChar == 'L'){
if (secondLastChar == 'A' || secondLastChar == 'C' || secondLastChar == 'S' || secondLastChar == 'L'){
wordSuffix = word + firstChar + suffix;
}else{
substringSuffix = suffix.substring (1);
wordSuffix = word + substringSuffix;
}
}else{
if (secondLastChar != 'A' && secondLastChar != 'C' && secondLastChar != 'S' && secondLastChar != 'L'){
wordSuffix = word + firstChar + suffix;
}else{
wordSuffix = word + suffix;
}
}
}
}
public static void printResults()
{
System.out.println("Original String: " + line);
System.out.println("Plural: " + wordPlural);
System.out.println("Suffix: " + wordSuffix);
System.out.println(" ");
}
}
Explanation / Answer
import java.io.*;
import java.util.StringTokenizer;
public class vowels_r_us
{
private static FileInputStream inFile;
private static InputStreamReader inReader;
private static BufferedReader reader;
private static StringTokenizer strTkn;
private static String line, word, suffix, wordPlural, wordSuffix, substring, substringSuffix;
private static int wordLength, suffixLength, i, j;
private static char lastChar, secondLastChar, firstChar;
public static void main (String args[]) throws IOException
{
initFile();
getData();
inFile.close();
}
public static void initFile() throws IOException
{
inFile = new FileInputStream ("c:\vowels.txt");
inReader = new InputStreamReader(inFile);
reader = new BufferedReader(inReader);
}
public static void getData() throws IOException
{
line = reader.readLine();
while(line != null)
{
strTkn = new StringTokenizer(line);
word = strTkn.nextToken();
suffix = strTkn.nextToken();
formPlural();
addSuffix();
printResults();
line = reader.readLine();
}
}
public static void formPlural()
{
wordLength = word.length();
i = wordLength-1;
lastChar = word.charAt(wordLength-1);
secondLastChar = word.charAt(wordLength-2);
if (lastChar == 'A' || lastChar == 'C' || lastChar == 'S' || lastChar == 'L'){
if (secondLastChar == 'A' || secondLastChar == 'C' || secondLastChar == 'S' || secondLastChar == 'L'){
wordPlural = word + lastChar + 'H';
}else{
substring = word.substring(0, i);
wordPlural = substring + 'G';
}
}else{
if (secondLastChar != 'A' && secondLastChar != 'C' && secondLastChar != 'S' && secondLastChar != 'L'){
wordPlural = word + lastChar + 'H';
}else{
wordPlural = word + 'G' + 'H';
}
}
}
public static void addSuffix()
{
firstChar = suffix.charAt(0);
j = wordLength-2;
if (firstChar != 'A' && firstChar != 'C' && firstChar != 'S' && firstChar != 'L'){
if (lastChar == 'A' || lastChar == 'C' || lastChar == 'S' || lastChar == 'L'){
if (secondLastChar == 'A' || secondLastChar == 'C' || secondLastChar == 'S' || secondLastChar == 'L'){
substring = word.substring(0, j);
wordSuffix = substring + lastChar + suffix;
}else{
wordSuffix = word + firstChar + suffix;
}
}else{
if (secondLastChar != 'A' && secondLastChar != 'C' && secondLastChar != 'S' && secondLastChar != 'L'){
wordSuffix = substring + lastChar + suffix;
}else{
wordSuffix = word + suffix;
}
}
}else{
if (lastChar == 'A' || lastChar == 'C' || lastChar == 'S' || lastChar == 'L'){
if (secondLastChar == 'A' || secondLastChar == 'C' || secondLastChar == 'S' || secondLastChar == 'L'){
wordSuffix = word + firstChar + suffix;
}else{
substringSuffix = suffix.substring (1);
wordSuffix = word + substringSuffix;
}
}else{
if (secondLastChar != 'A' && secondLastChar != 'C' && secondLastChar != 'S' && secondLastChar != 'L'){
wordSuffix = word + firstChar + suffix;
}else{
wordSuffix = word + suffix;
}
}
}
}
public static void printResults()
{
System.out.println("Original String: " + line);
System.out.println("Plural: " + wordPlural);
System.out.println("Suffix: " + wordSuffix);
System.out.println(" ");
}
}
please rate!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.