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

Java Assignment Instructions You are to write a program that gives its users thr

ID: 3684894 • Letter: J

Question

Java Assignment

Instructions

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). If the user selects reverse the program should open the text file and read its content into a stack of characters. The program should then pop the characters from the top of the stack and save them in a second text file. 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. The program should then dequeue the characters, convert it to uppercase, and store it in a second file.

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. The program should determine if the files are identical by comparing characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. Otherwise let the user know that the files are identical.

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. You are at liberty to ask the user for the file name to be examined.

Be sure to include the package declaration [package stacksAndQueues;].

Explanation / Answer

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){
           // reading input and output filename from user
           System.out.print("Enter input filename: ");
           String inputFileName = sc.next();
           System.out.print("Enter output filename: ");
           String outputFileName = sc.next();
          
           // creating reader and writer
           fr = new FileReader(inputFileName);
          
           fw = new FileWriter(outputFileName);
          
           // declaring Stack
           Stack<Character> stack = new Stack<Character>();
          
           int ch;
           while((ch = fr.read()) != -1){
          
               char c = (char)ch;
               stack.push(c);
           }
          
           // now writing to file
           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();
          
           // creating reader and writer
           fr = new FileReader(inputFileName);
          
           fw = new FileWriter(outputFileName);
          
           // declaring Stack
           Queue<Character> queue = new LinkedList<Character>();
          
           int ch;
           while((ch = fr.read()) != -1){
          
               char c = (char)ch;
               queue.offer(c);
           }
          
           // now writing to file
           while(!queue.isEmpty()){
               char c = Character.toUpperCase(queue.poll());
               fw.write(c);
           }
          
           fr.close();
           fw.close();
       }
       else if(choice == 3){
           // first input file
           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