Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are to write a program that gives its users three basic options: reverse, co

ID: 3739206 • Letter: Y

Question

You are to write a program that gives its users three basic options: reverse, convert, and compare. The program should ask the user to input the file name for the file in question (including the extension, txt). (20pts) If the user selects reverse the program should open the text file and read its content into a stack of characters (10pts) The program should then pop the characters from the top of the stack and save them in a second text file (10pts). The order in the second file should be the reverse of the order in the first file If the user selects convert, the program should open a text file and read its content into as queue of characters (10pts). The program should then dequeue the characters, convert it to uppercase, and store it in a second file (10pts) If the user selects compare, the program ask the user for two files and should then open the two text files and read their contents into two separate queues (10pts) The program should determine if the files are identical comparing characters in the queues(10pts) When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same(5pts) Otherwise let the user know that the files are identical(5pts) by You should use your own test files to make sure the program performs as specified but you do not have to include them in your submission (5pts). You are at liberty to ask the user for the file name to be examined Be sure to include the package declaration (package stacksAndQueues:] (5pts)

Explanation / Answer

Java code:

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.LinkedList;

import java.util.Queue;

import java.util.Scanner;

public class FileOperation {

   public static void main(String[] args) throws IOException {

     

       int choice;

       Scanner sc = new Scanner(System.in);

       System.out.println("Enter your choice:");

       System.out.println("1. Reverse 2. Convert 3. Compare");

       choice = sc.nextInt();

     

       FileReader fr;

     

       FileWriter fw;

     

       if(choice == 1){

        

           System.out.print("Enter input filename: ");

           String inputFileName = sc.next();

           System.out.print("Enter output filename: ");

           String outputFileName = sc.next();

         

      

         fr = new FileReader(inputFileName);

         

           fw = new FileWriter(outputFileName);

         

          

           Stack<Character> stack = new Stack<Character>();

         

           int ch;

           while((ch = fr.read()) != -1){

         

               char c = (char)ch;

               stack.push(c);

           }

         

           while(!stack.isEmpty()){

               fw.write(stack.pop());

           }

         

           fr.close();

           fw.close();

       }

       else if(choice == 2){

           System.out.print("Enter input filename: ");

           String inputFileName = sc.next();

           System.out.print("Enter output filename: ");

           String outputFileName = sc.next();

         

           fr = new FileReader(inputFileName);

         

           fw = new FileWriter(outputFileName);

         

           Queue<Character> queue = new LinkedList<Character>();

         

           int ch;

           while((ch = fr.read()) != -1){

         

               char c = (char)ch;

               queue.offer(c);

           }

         

           while(!queue.isEmpty()){

               char c = Character.toUpperCase(queue.poll());

               fw.write(c);

           }

         

           fr.close();

           fw.close();

       }

       else if(choice == 3){

           System.out.print("Enter first input filename: ");

           String inputFileName1 = sc.next();

           System.out.print("Enter second input filename: ");

           String inputFileName2 = sc.next();

         

           fr = new FileReader(inputFileName1);

           FileReader fr2 = new FileReader(inputFileName2);

         

           while(true){

               int ch1 = fr.read();

               int ch2 = fr2.read();

             

               if(ch1 == -1 && ch2 == -1){// reached at end of both file

                   System.out.println("Both file is equal");

                   break;

               }

             

               else if(ch1 == -1 || ch2 == -1){// one of file has more characters

                   System.out.println("Files are not equal");

                   break;

               }

               else if(ch1 != ch2){ // current characters are not equal

                   System.out.println("Files are not equal");

                   break;

               }

           }

         

           fr.close();

           fr2.close();

       }

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote