Question

In: Computer Science

Write a program that reverses a text file by using a stack. The user interface must...

Write a program that reverses a text file by using a stack. The user interface must consist of 2 list boxes and 3 buttons. The 3 buttons are: Read - reads the text file into list box 1. Reverse - reverses the items in list box 1 by pushing them onto stack 1, then popping them from stack 1 (in the reverse order) and adding them to list box 2. Write - writes the contents of list box 2 to a new text file. At first, only the Read button is enabled. After it is clicked, it is disabled and the Reverse button is enabled. After it is clicked, it is disabled and the Write button is enabled. After it is clicked, it is disabled. The name of the input text file is "input.txt". The input text file will contain no more than 100 lines of text. This fact is not needed by the program. It simply means that memory usage is not an issue. The name of the output text file is "output.txt". Notes: 1. The name of your main class should be ReverseFileViaStack. 2. Use the Java class library Stack class for your stack. 3. Use a border layout for your contents pane. 4. In the north region include the following title: Reverse a Text File via a Stack. 5. Use a panel in the center region to contain the 2 list boxes (side-by-side). 6. Place the 3 buttons in the south region. 7. A useful resource for this project is the week 5 video: Java GUI List Components

Solutions

Expert Solution

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;

public class DealingFiles extends JFrame implements ActionListener
{

private Container listContents;

// declaring ui fields
private JLabel south_side, north_side, center;
private JButton button_read, button_write, button_reverse;

//declaring lists
private JList<String> dataList1;
private JList<String> dataList2;

// Fonts:
private final Font boldFont = new Font(Font.DIALOG, Font.BOLD, 14);
private final Font normalFont = new Font(Font.DIALOG, Font.PLAIN, 14);

private DefaultListModel<String> listModel1 = new DefaultListModel<>();
private DefaultListModel<String> listModel2 = new DefaultListModel<>();

private Stack<String> myStack = new Stack<>();


public DealingFiles()
{
super("Reverse a txt File using STACK");

// declaring models
listModel1 = new DefaultListModel();
dataList1 = new JList<>(listModel1);

listModel2 = new DefaultListModel();
dataList2 = new JList<>(listModel2);

listContents = getContentPane();

// layout management
listContents.setLayout(new BorderLayout());
north_side = new JLabel("Text File reverse using stack.");

//buttons
button_read = new JButton("Read");
button_reverse = new JButton("Reverse");
button_write = new JButton("write");

//adding buttons to panel
listContents.add(button_read);
listContents.add(button_reverse);
listContents.add(button_write);
ButtonHandler button_handler = new ButtonHandler();
button_read.addActionListener(button_handler);
button_reverse.addActionListener(button_handler);
button_write.addActionListener(button_handler);
   // adding elements
listContents.add(north_side, BorderLayout.NORTH);
listContents.add(center, BorderLayout.CENTER);
listContents.add(south_side, BorderLayout.SOUTH);
setSize(800, 600);
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed (ActionEvent e)
{
   Object src = e.getSource();
   if (src == button_read)
   {
   readDataFile();
   return;
   }
   if (src == button_reverse)
   {
   reverseDatFile();
   return;
   }
   if (src == button_write)
   {
   writeDataFile();
   return;
   }
}
private void readDataFile()
{
   try (BufferedReader sc = new BufferedReader(new FileReader(file)))
   {
   while ((dataList1 = sc.readLine()) != null)
{
           dataList1.addElement();
   }
   }
}
private void reverseDatFile()
{
   listModel2.elementAt();
   dataList1.addElement();
}
private void applyFonts()
{
UIManager.put("Button.font", boldFont);
UIManager.put("ComboBox.font", boldFont);
UIManager.put("Label.font", boldFont);
UIManager.put("List.font", normalFont);
}


private void writeDataFile()
{
   try
   {
       FileOutputStream stream = new FileOutputStream("input.txt", false);
       PrintWriter pwriter = new PrintWriter(stream);

       pwriter.print(dataList2);
       pwriter.println(true);

       pwriter.close();
   }

   catch(FileNotFoundException fnfe)
   {
       System.out.println("file not found");
   }

}

public static void main(String[] args)
{
DealingFiles obj = new DealingFiles();
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}


Related Solutions

Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
Write a program that allows the user to navigate the lines of text in a file....
Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...
Write a program that allows the user to navigate the lines of text in a file....
Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
Create a C++ login program using file for 2 user. Usernames must be in one file...
Create a C++ login program using file for 2 user. Usernames must be in one file and password in the other file. Ask user for their usernames and password if it matches, they logged in successfully. If it doesn’t match,they have 3 trials ,then they have to sign in and create a username and password. After creating a username and password, they have to go to the login page again. Must work for visual studio code
Write a method public static Stack reverse(Stack s) that reverses the order of elements on stack...
Write a method public static Stack reverse(Stack s) that reverses the order of elements on stack s using a Queue. Test your method using some example stacks. In java
Assembly Language. Write a procedure that reverses order of a 1-D array using the stack. The...
Assembly Language. Write a procedure that reverses order of a 1-D array using the stack. The array is a string array, and the address along with the number of elements are passed through the stack. Then write a complete program to test your procedure.
Suppose the interface and the class of stack already implemented, Write application program to ( java)...
Suppose the interface and the class of stack already implemented, Write application program to ( java) 1- insert 100 numbers to the stack                         2- Print the even numbers 3- Print the summation of the odd numbers
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT