Question

Using java Create a simple queue class and a simple stack class. The queue and stack...

Using java Create a simple queue class and a simple stack class. The queue and stack should be implemented as a linked list.

Create three functions that utilize these data structures

Write a function that opens a text file and reads its contents into a stack of characters. 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.

Write a function that opens a text file and reads its contents into a queue of characters. The program should then dequeue each character, convert it to uppercase, and store it in a second file.

Write a function that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. Create a driver program to test the functions.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Please make sure that the files are saved with the correct name as mentioned below.

Program screenshots:

Node.java // Define the Node class. public class Node // Declare the variables. public char info; public Node next; // DefineStack.java // Define Stack class. public class Stack // Declare the private members. private Node head; // Define the default// Define a method to remove an element from // the stack. public void pop() . // Return if the head is null. if(head == nullQueue.java // Define the class Queue. public class Queue // Declare the private members. private Node head; // Define the def// Otherwise, move the head to the // next node else head = head.next; // Define a method to display the queue. public void dMain.java // Import the required packages. import java.io.*; import java.util.Scanner; // Define the Main class. public class// Start the loop to traverse the stack. while(!my Stack.isempty()) // Write the top character of the stack // in the file. o// Open the file in write mode. FileWriter output = new FileWriter(part2_output.txt); BufferedWriter outfile = new Buffered// Start the loop to store the content // of the file in the first queue. while(infile.hasNextLine()). { String line = infile// Start the loop to traverse both the queue. while( ! myQueuel.isempty() || ! myQueue2.isempty()) // Display the error messa

Sample output:

Result for part 3: The files are identical.

Sample output files:

part1_output.txt:

54321 .enil elpmas a si siht

part2_output.txt:

THIS IS A SAMPLE LINE. 12345

Node.java

// Define the Node class.

public class Node

{

    // Declare the variables.

    public char info;

    public Node next;

    // Define the default constructor.

    public Node()

    {

        info = ' ';

        next = null;

    }

    // Define the parameterized constructor.

    public Node(char ch)

    {

        info = ch;

        next = null;

    }

}

Stack.java

// Define Stack class.

public class Stack

{

    // Declare the private members.

    private Node head;

    // Define the default constructor.

    public Stack()

    {

        head = null;

    }

    // Define a method to insert an element

    // in the stack.

    public void push(char ch)

    {

        // Create a new node if the head is null.

        if(head == null)

        {

            head = new Node(ch);

        }

        // Otherwise, create a new node and add

        // it at the head position.

        else

        {

            Node temp = new Node(ch);

            temp.next = head;

            head = temp;

        }

    }

    // Define a method to remove an element from

    // the stack.

    public void pop()

    {

        // Return if the head is null.

        if(head == null)

        {

            return;

        }

        // Otherwise, move the head to the next node.

        else

        {

            head = head.next;

        }

    }

    // Define a method to display the stack.

    public void display()

    {

        Node temp = head;

        

        // Start a loop to traverse the list.

        while(temp != null)

        {

            System.out.print(temp.info);

            temp = temp.next;

        }

        System.out.println();

    }

    // Define a method to return the

    // top element of the stack.

    public char top()

    {

        return head.info;

    }

    // Define a method to check if the

    // stack is empty or not.

    public boolean isempty()

    {

        return head == null;

    }

}

Queue.java

// Define the class Queue.

public class Queue

{

    // Declare the private members.

    private Node head;

    // Define the default constructor.

    public Queue()

    {

        head = null;

    }

    // Define a method to insert an element

    // in the queue.

    public void enqueue(char ch)

    {

        // Create a new node if the head is null.

        if(head == null)

        {

            head = new Node(ch);

        }

        // Otherwise, move to the end of the list

        // and add a new node.

        else

        {

            Node temp = head;

            while(temp.next != null)

            {

                temp = temp.next;

            }

            temp.next = new Node(ch);

        }

    }

    // Define a method to remove an element

    // from the queue.

    public void dequeue()

    {

        // Return if the head is null.

        if(head == null)

        {

            return;

        }

        // Otherwise, move the head to the

        // next node.

        else

        {

            head = head.next;

        }

    }

    // Define a method to display the queue.

    public void display()

    {

        Node temp = head;

        // Start the loop to traverse the list.

        while(temp != null)

        {

            System.out.print(temp.info);

            temp = temp.next;

        }

        System.out.println();

    }

    // Define a method to return the front

    // element of the queue.

    public char front()

    {

        return head.info;

    }

    // Define a method to check if the

    // queue is empty or not.

    public boolean isempty()

    {

        return (head == null);

    }

}

Main.java

// Import the required packages.

import java.io.*;

import java.util.Scanner;

// Define the Main class.

public class Main

{

    // Define a method to copy the content of one file

    // into another using a stack.

    public static void part1() throws Exception

    {

        // Create a stack.

        Stack myStack = new Stack();

        // Open the input file in read mode.

        File input = new File("input.txt");

        Scanner infile = new Scanner(input);

        // Start the loop to read the file.

        while(infile.hasNextLine())

        {

            // Store the line in a string.

            String line = infile.nextLine();

            // Start the loop to travese the line.

            for(char ch: line.toCharArray())

            {

                // Push the character in the stack.

                myStack.push(ch);

            }

            // Add a new line character in the stack.

            if(infile.hasNextLine())

            {

                myStack.push('\n');

            }

        }

        // Close the input file.

        infile.close();

        // Open the file in write mode.

        FileWriter output = new FileWriter("part1_output.txt");

        BufferedWriter outfile = new BufferedWriter(output);

        // Start the loop to traverse the stack.

        while(!myStack.isempty())

        {

            // Write the top character of the stack

            // in the file.

            outfile.write(myStack.top());

            // Remove the element from the stack.

            myStack.pop();

        }

        // Close the output file.

        outfile.close();

    }

    // Define a method to convert all the characters

    // of a file to uppercase using a queue.

    public static void part2() throws Exception

    {

        // Create a queue.

        Queue myQueue = new Queue();

        // Open the file in read mode.

        File input = new File("input.txt");

        Scanner infile = new Scanner(input);

        // Start the loop to read the file.

        while(infile.hasNextLine())

        {

            // Store the line in a string.

            String line = infile.nextLine();

            // Start the loop to traverse the string.

            for(char ch: line.toCharArray())

            {

                // Insert the character in the queue.

                myQueue.enqueue(ch);

            }

            // Add a nextline character in the queue.

            if(infile.hasNextLine())

            {

                myQueue.enqueue('\n');

            }

        }

        // Close the input file.

        infile.close();

        // Open the file in write mode.

        FileWriter output = new FileWriter("part2_output.txt");

        BufferedWriter outfile = new BufferedWriter(output);

        // Start the loop to traverse the queue.

        while(!myQueue.isempty())

        {

            // Store the first element.

            char ch = myQueue.front();

            // If the current character is a letter,

            // convert it to uppercase and write it

            // in the file.

            if(Character.isLetter(ch))

            {

                outfile.write(Character.toUpperCase(ch));

            }

            // Otherwise, write the character in the file.

            else

            {

                outfile.write(ch);

            }

            // Remove the character from the queue.

            myQueue.dequeue();

        }

        // Close the output file.

        outfile.close();

    }

    // Define a method to compare the content of

    // 2 files using a queue.

    public static void part3() throws Exception

    {

        // Create 2 queues.

        Queue myQueue1 = new Queue();

        Queue myQueue2 = new Queue();

        // Open the file in read mode.

        File input = new File("part3_input1.txt");

        Scanner infile = new Scanner(input);

        // Start the loop to store the content

        // of the file in the first queue.

        while(infile.hasNextLine())

        {

            String line = infile.nextLine();

            for(char ch: line.toCharArray())

            {

                myQueue1.enqueue(ch);

            }

            if(infile.hasNextLine())

            {

                myQueue1.enqueue('\n');

            }

        }

        infile.close();

        // Open the second file in read mode.

        input = new File("part3_input2.txt");

        infile = new Scanner(input);

        // Start the loop to store the content

        // of the file in the second queue.

        while(infile.hasNextLine())

        {

            String line = infile.nextLine();

            for(char ch: line.toCharArray())

            {

                myQueue2.enqueue(ch);

            }

            if(infile.hasNextLine())

            {

                myQueue2.enqueue('\n');

            }

        }

        infile.close();

        // Start the loop to traverse both the queue.

        while(!myQueue1.isempty() || !myQueue2.isempty())

        {

            // Display the error message if the characters

            // does not match.

            if(myQueue1.front() != myQueue2.front())

            {

                System.out.println("The files are not the same.");

                return;

            }

            // Remove the character from both the queue.

            myQueue1.dequeue();

            myQueue2.dequeue();

        }

        // Display the result.

        System.out.println("The files are identical.");

    }

    // Define the main() method.

    public static void main(String[] args) throws Exception

    {

        // Call the methods to test.

        part1();

        part2();

        System.out.print("Result for part 3: ");

        part3();

    }

}

Sample input files:

input.txt:

This is a sample line.

12345

part3_input1.txt:

line 1

line 2

line 3

part3_input2.txt:

line 1

line 2

line 3

Add a comment
Know the answer?
Add Answer to:
Using java Create a simple queue class and a simple stack class. The queue and stack...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will co...

    In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will convert strings sent over a serial bus one character at a time. The conversion will be from big to little endian or from little to big endian. To simplify this, each character will be considered a word. Little endian will have the lowest address first. Big endian will have the biggest address first. For example (for this lab),...

  • Create a New Java Project called YourLastNameUpperCase. Write a program that asks the user for the...

    Create a New Java Project called YourLastNameUpperCase. Write a program that asks the user for the names of two files. The first file should be opened for reading and the second file should be opened for writing. The program should read the contents of the first file, change all characters to uppercase, and store the results in the second file. The second file will be a copy of the first file, except that all the characters will be uppercase. Use...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • Implement the Stack Class with an ArrayList instead of an array, including the following functions: -empty...

    Implement the Stack Class with an ArrayList instead of an array, including the following functions: -empty -push -peek -pop -overrided toString() function which returns all of the stack's contents Things to note: -You no longer need a size. -You no longer need to define a constant DEFAULT_CAPACITY. since ArrayLists grow dynamically. -Whenever possible, use ArrayList functions instead of the [ ] (index operator) to implement your stack functions Then write a driver program to do palindrome check. A string is...

  • » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should impleme...

    Using C Please comment » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should implement the following functions that take a stack o void push(Stack * stack, int value) ● int pop(Stack * stack) Prompt the user to input 5 integers, and use the stack to reverse the integers Print the result to the screen. o o » Part B: Queue o Create a Queue struct that is a wrapper for...

  • C++ I need help to create a program that executes a simple queue data type. You...

    C++ I need help to create a program that executes a simple queue data type. You must define the queue data type and use the enqueue(), dequeue(), and displayQueue() functions. (Dont use C++ STL to execute queue logic) You may use a linked list , or just use a regular C array of integers. The input file (testfile.txt) will be the list of operations to carry out on a queue. Example input file: enqueue 6 enqueue 8 dequeue enqueue 4...

  • Write a simple program in C++ that reads a line of text, changes each uppercase letter to lowerca...

    Write a simple program in C++ that reads a line of text, changes each uppercase letter to lowercase, and places each letter both in a queue and onto a stack. The program should then verify whether the line of text is a palindrome (a set of letters or numbers that is the same whether read forward or backward). DO NOT USE THE STACK LIBRARY, QUEUE LIBRARY, OR ANY OTHER LIBRARIES NOT COVERED YET.

  • i need the sloution with java code please :) Using your Queue class write a GUI...

    i need the sloution with java code please :) Using your Queue class write a GUI program that opens a file contains integers in the range [0 .. 999] "in.txt". The program stops reading if -1 is read. Your program should use queues to make the output such that the first textfield output contains the integers in the range 0..9 but in their same order as in the input, the same for the second textfield but in the range 10..19,...

  • Define a class for static Queue for storing integer numbers. Your class must include a constructor,...

    Define a class for static Queue for storing integer numbers. Your class must include a constructor, a destructor, enqueue, dequeue, display, isFull, and isEmpty functions. Write a simple main function in your program that demonstrates the class

  • I am to implement a simple simulation that supports a stack and a queue of items being either enqueued and dequeued (ont...

    I am to implement a simple simulation that supports a stack and a queue of items being either enqueued and dequeued (onto the queue) or pushed and popped (onto the stack). I are required to use STL data structures to implement and create the stack and queue for my program. ----- testfile1.tst -------- enqueue 5 enqueue 7 push blooper push rookie dequeue push demerits pop enqueue 3 enqueue 8 push showplace enqueue 9 dequeue pop dequeue push palmetto push zebra...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT