Question
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
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
Example Programming Project Output Sample input file contents: a ab abc abcd abcde abcdef abcdefg abcdefgh abcdefghi abcdefghij abcdefghijk abcdefghijkl abcdefghijklm abcdefghijklmn abcdefghijklmno abcdefghijklmnop abcdefghijklmnopq abcdefghijklmnopqr abcdefghijklmnopqrs abcdefghijklmnopqrst abcdefghijklmnopqrstu abcdefghijklmnopqrstuv abcdefghijklmnopqrstuvw abcdefghijklmnopqrstuvwx abcdefghijklmnopqrstuvwxy abcdefghijklmnopqrstuvwxyz
// This program reverses a text file by using a stack. // The user interface consists 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". import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.border.*; import java.util.*; public class ReverseFileViaStack extends JFrame implements ActionListener { // Borders: private Border borderCenter = BorderFactory.createEmptyBorder(10, 10, 10, 10); private Border borderContents = BorderFactory.createEmptyBorder(0, 10, 10, 10); private Border borderList = BorderFactory.createLineBorder(Color.BLUE, 1); // Containers: private Box boxList1 = Box.createVerticalBox(); private Box boxList2 = Box.createVerticalBox(); private JPanel contents; // Main container private JPanel panelCenter; private JPanel panelSouth; // Components: private JButton btnRead; private JButton btnReverse; private JButton btnWrite; private JLabel lblList1; private JLabel lblList2; private JListlist1; private JList list2; // Fonts: private final Font fontBold = new Font(Font.DIALOG, Font.BOLD, 14); private final Font fontPlain = new Font(Font.DIALOG, Font.PLAIN, 14); // List models: private DefaultListModel listModel1 = new DefaultListModel<>(); private DefaultListModel listModel2 = new DefaultListModel<>(); // Stack: private Stack stack = new Stack<>(); public ReverseFileViaStack() { super("Reverse Text File via Stack"); setFonts(); // A JPanel is used for the main container: contents = new JPanel(); contents.setBorder(borderContents); contents.setLayout(new BorderLayout()); setContentPane(contents); // Create and add components: // North region: JLabel lblTitle = new JLabel("Reverse Text File via Stack", SwingConstants.CENTER); lblTitle.setFont(new Font(Font.DIALOG, Font.BOLD, 18)); contents.add(lblTitle, BorderLayout.NORTH); // Center region: panelCenter = new JPanel(); panelCenter.setBorder(borderCenter); // List 1: // To left-align components in a vertical box, they // must EACH be given left alignment. lblList1 = new JLabel("Original order:"); lblList1.setAlignmentX(LEFT_ALIGNMENT); listModel1 = new DefaultListModel(); list1 = new JList<>(listModel1); list1.setAlignmentX(LEFT_ALIGNMENT); list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list1.setVisibleRowCount(20); list1.setBorder(borderList); JScrollPane scrollList1 = new JScrollPane(list1); scrollList1.setAlignmentX(LEFT_ALIGNMENT); scrollList1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); setSpecificSize(scrollList1, new Dimension(250, 300)); boxList1.add(lblList1); boxList1.add(scrollList1); panelCenter.add(boxList1); // Spacer between lists: panelCenter.add(Box.createRigidArea(new Dimension(70, 1))); // List 2: lblList2 = new JLabel("Reversed order:"); lblList2.setAlignmentX(LEFT_ALIGNMENT); listModel2 = new DefaultListModel(); list2 = new JList<>(listModel2); list2.setAlignmentX(LEFT_ALIGNMENT); list2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list2.setVisibleRowCount(20); list2.setBorder(borderList); JScrollPane scrollList2 = new JScrollPane(list2); scrollList2.setAlignmentX(LEFT_ALIGNMENT); scrollList2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); setSpecificSize(scrollList2, new Dimension(250, 300)); boxList2.add(lblList2); boxList2.add(scrollList2); panelCenter.add(boxList2); contents.add(panelCenter, BorderLayout.CENTER); // South region: panelSouth = new JPanel(); btnRead = new JButton("Read"); btnReverse = new JButton("Reverse"); btnReverse.setEnabled(false); btnWrite = new JButton("Write"); btnWrite.setEnabled(false); panelSouth.add(btnRead); panelSouth.add(Box.createRigidArea(new Dimension(70, 1))); panelSouth.add(btnReverse); panelSouth.add(Box.createRigidArea(new Dimension(70, 1))); panelSouth.add(btnWrite); contents.add(panelSouth, BorderLayout.SOUTH); // Register event handlers: btnRead.addActionListener(this); btnReverse.addActionListener(this); btnWrite.addActionListener(this); // Size and display window: setSize(700, 450); setResizable(false); setLocationRelativeTo(null); // Centers window setVisible(true); } // ******** Student part 1 begins here ****************************** // Button handler. //write the button handler for three buttons //Read, Reverse, Write //call the readFile or reverseFile or writeFile routines depending on which button is selected public void actionPerformed(ActionEvent e) { //********* Student part 1 ends here *********************************** } } private void readFile() { btnRead.setEnabled(false); // Create input file scanner: String fileName = "input.txt"; File file = new File(fileName); Scanner fileScanner; try { fileScanner = new Scanner(file); } catch (FileNotFoundException fnfe) { System.out.println("Input file not found: " + fileName); return; } // Process the input file: // **************** Student Part 2 begins here ************************* // Process the input file using fileScanner //turn off the read button and enable the reverse button //use the variable name: listModel1 (that is list Model 1) //use the variable name: addElement (line) // ********************** Student Part 2 ends here ************************ //*********************** Student Part 3 begins here ********************** // Push list 1 to stack: // Push list 1 to a stack //check for an empty stack //use variable name: listModel1 //use variable name addElementAt(i) // Pop stack to list 2: // check for empty stack //use variable: listModel2 //use variable addElement // ************************ Student Part 3 ends here ********************* } private void setFonts() { UIManager.put("Button.font", fontBold); UIManager.put("ComboBox.font", fontBold); UIManager.put("Label.font", fontBold); UIManager.put("List.font", fontPlain); } private void setSpecificSize(JComponent component, Dimension dimension) { component.setMinimumSize(dimension); component.setPreferredSize(dimension); component.setMaximumSize(dimension); } private void writeFile() { // Create ouput file writer: String fileName = "output.txt"; File file = new File(fileName); PrintWriter writer; try { writer = new PrintWriter(file); } catch (FileNotFoundException fnfe) { System.out.println("Unable to create output file: " + fileName); return; } // Write list 2 to file: int size = listModel2.getSize(); for (int i = 0; i < size; i++) { writer.println(listModel2.elementAt(i)); } // PrintWriter does not do automatic line flushing. // If it is not closed, buffered output will be lost. // An alternative would be to call flush() after each println. writer.close(); btnWrite.setEnabled(false); } public static void main(String[] args) { ReverseFileViaStack gui = new ReverseFileViaStack(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started