Question: Make sure to read the instructions! Please help me complete my Java assignment by improving my code with the following instructions (Please provide full code

Make sure to read the instructions!

Please help me complete my Java assignment by improving my code with the following instructions (Please provide full code for copy and paste):

Modify your Poetry.java file to do the following: If the letter is coded to a vowel, make it a capital letter. Write your new encoded poem to a text file called capitalVowels.txt. Take capitalVowels.txt and reverse the characters of each line of the file Then, decode the text back to its original letters Finally, write this text to a new file called reversePoem.txt. Make sure that the capitalised vowels go back to their original letters! Compile, save and run your file.

Here is my code:

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.Scanner;

public class Poetry {

public static boolean isCharacterALetter(char c) {

return Character.isLetter(c);

}

public static ArrayList toAscii(String str) {

int i = 0;

ArrayList asciiArr = new ArrayList();

int nextAscii = 0;

while (i < str.length()) {

if (isCharacterALetter(str.charAt(i))) {

nextAscii = (int) str.charAt(i);

asciiArr.add(nextAscii);

} else {

asciiArr.add((int) str.charAt(i));

}

i++;

}

return asciiArr;

}

public static ArrayList encode(ArrayList arr) {

int i = 0;

ArrayList encodedArr = new ArrayList();

while (i < arr.size()) {

if (isCharacterALetter((char) arr.get(i).intValue())) {

if (((arr.get(i) >= 108) && (arr.get(i) <= 122)) || ((arr.get(i) >= 76) && (arr.get(i) <= 90))) {

encodedArr.add(arr.get(i) - 11);

} else {

encodedArr.add(arr.get(i) + 15);

}

} else {

encodedArr.add(arr.get(i));

}

i++;

}

return encodedArr;

}

public static String toChar(ArrayList arr) {

int i = 0;

ArrayList charArr = new ArrayList();

char nextChar;

while (i < arr.size()) {

if (isCharacterALetter((char) arr.get(i).intValue())) {

nextChar = (char) arr.get(i).intValue();

charArr.add(nextChar);

} else {

charArr.add((char) arr.get(i).intValue());

}

i++;

}

StringBuilder sb = new StringBuilder();

for (Character c : charArr) {

sb.append(c);

}

return sb.toString();

}

public static void main(String[] args) {

try {

Scanner scanner = new Scanner(new File("poem.txt"));

PrintWriter writer = new PrintWriter("encodedPoem.txt");

while (scanner.hasNextLine()) {

String line = scanner.nextLine();

ArrayList ascii = toAscii(line);

ArrayList encodedAscii = encode(ascii);

String encodedLine = toChar(encodedAscii);

writer.println(encodedLine);

}

scanner.close();

writer.close();

} catch (FileNotFoundException e) {

System.out.println("File not found.");

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!