Question

I was told I need three seperate files for these classes is there anyway to tie...

I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw.

import java.util.ArrayList;
import java.util.Scanner;

/**
*
*
*/
public class MySorts {

      public static void main(String[] args) {

            Scanner input = new Scanner(System.in);

            String sentence;

            String again;

            do {

                  System.out

                              .println("Enter a sentence, I will tell you if it is a palindrome: ");

                  sentence = input.nextLine();

                  if (isPalindrome(sentence))

                        System.out.println("\"" + sentence + "\" is a palindrome!");

                  else

                        System.out.println("\"" + sentence + "\" is not a palindrome!");

                  System.out

                              .println("Do you want another test (\"YES\" or \"NO\"): ");

                  again = input.nextLine();

            } while (again.equalsIgnoreCase("YES"));

      }

      /**

      * isPalindrom returns true if the given String is a palindrom @

      */

      public static boolean isPalindrome(String sentence) {

            // declare a MyStack s

            MyStack s = new MyStack();

            // declare a MyQueue q

            MyQueue q = new MyQueue();

            for (int i = 0; i < sentence.length(); i++) {

                  // if ith character in sentence is a letter

                  // convert to upper case and push it into s and q

                  if (Character.isLetter(sentence.charAt(i))) {

                        char c = Character.toUpperCase(sentence.charAt(i));

                        s.push(c);

                        q.push(c);

                  }

            }

            while (!s.isEmpty()) {

                  // if the front of the queue not match the top of stack

                  // return false

                  if (s.peek() != q.peek()) {

                        return false;

                  }

                  // pop out top of the stack and front of the queue

                  s.pop();

                  q.pop();

            }

            return true;

      }

}

//MyQueue.java

/**

* class MyQueue implemented using ArrayList. The index 0 element is the front of the queue

* The last element of the queue has index tail

* @author Your Name

* @version Date

*/

public class MyQueue {

      private ArrayList list; // hold the elements in queue

      private int tail; // index of the last element in queue

      /**

      * constructor construct an empty queue

      */

      public MyQueue() {

            list = new ArrayList();

            // setting tail index to -1 initially, but to be honest, there is no

            // need for this variable, we can implement all the functionalities

            // using list methods

            tail = -1;

      }

      /**

      * isEmpty return true if the queue is empty; false otherwise

      *

      * @return true if the queue is empty; false otherwise

      */

      public boolean isEmpty() {

            return list.isEmpty();

      }

      /**

      * size return the size of the queue

      *

      * @return the number of elements in queue

      */

      public int size() {

            return list.size();

      }

      /**

      * peek return the front element of the queue

      *

      * @return the front element of the queue. If the queue is empty, return

      *         null

      */

      public E peek() {

            // returning null if empty

            if (isEmpty()) {

                  return null;

            }

            // returning element at index 0

            return list.get(0);

      }

      /**

      * pop remove the front element of the queue

      */

      public void pop() {

            if (!isEmpty()) {

                  // removing element at index 0

                  list.remove(0);

                  // updating tail index, now that list is updated.

                  tail--;

            }

      }

      /**

      * push push a new element to the queue

      */

      public void push(E item) {

            // simply adding to the end

            list.add(item);

            // updating tail index

            tail++;

      }

}

// MyStack.java

/**

* class MyStack: A stack class implemented by using ArrayList

* All stack elements are stored in an ArrayList. The top element

* has index top

*

* @author Your Name

* @version Date

*/

public class MyStack {

      private ArrayList list; // used to store elements in stack

      private int top; // the index of top element

      /**

      * constructor construct an empty stack

      */

      public MyStack() {

            list = new ArrayList();

            top = -1;

      }

      /**

      * push push a given element on the top of the stack

      */

      public void push(E item) {

            // simply adding to the end, incrementing top

            list.add(item);

            top++;

      }

      /**

      * isEmpty return true if the stack is empty; false otherwise

      *

      * @return true if the stack is empty; false otherwise

      */

      public boolean isEmpty() {

            return list.isEmpty();

      }

      /**

      * peek Return the top element

      */

      public E peek() {

            if (isEmpty()) {

                  // empty stack

                  return null;

            }

            // returning element at top index

            return list.get(top);

      }

      /**

      * pop Remove the top element from the stack. If the stack is empty,nothing

      * happen

      */

      public void pop() {

            if (!isEmpty()) {

                  // removing element at top and updating top index

                  list.remove(top);

                  top--;

            }

      }

      /**

      * size return the size of the stack

      *

      * @return number of elements in stack

      */

      public int size() {

            return list.size();

      }

}

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

Please find the answer below, have created the single file as per your need. In this case, MySort.java is the class which contains the main method. So you need that class as a public class and file saved with MySort.java. But we can have MyQueue and MyStack class as the inner class of the MySort.java. So whenever you create an Object of MyQueue or MyStack it will be created form the inner class from the same file. Please find the MySort.java file below:

MySorts.java
import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 *
 */
public class MySorts {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String sentence;
        String again;
        do {
            System.out.println("Enter a sentence, I will tell you if it is a palindrome: ");
            sentence = input.nextLine();
            if (isPalindrome(sentence))
                System.out.println("\"" + sentence + "\" is a palindrome!");
            else
                System.out.println("\"" + sentence + "\" is not a palindrome!");
            System.out.println("Do you want another test (\"YES\" or \"NO\"): ");
            again = input.nextLine();
        } while (again.equalsIgnoreCase("YES"));
    }
    /**
     * isPalindrom returns true if the given String is a palindrom @
     */
    public static boolean isPalindrome(String sentence) {
        // declare a MyStack1 s
        MyStack1 s = new MyStack1();
        // declare a MyQueue1 q
        MyQueue1 q = new MyQueue1();
        for (int i = 0; i < sentence.length(); i++) {
            // if ith character in sentence is a letter
            // convert to upper case and push it into s and q
            if (Character.isLetter(sentence.charAt(i))) {
                char c = Character.toUpperCase(sentence.charAt(i));
                s.push(c);
                q.push(c);
            }
        }
        while (!s.isEmpty()) {
            // if the front of the queue not match the top of stack
            // return false
            if (s.peek() != q.peek()) {
                return false;
            }
            // pop out top of the stack and front of the queue
            s.pop();
            q.pop();
        }
        return true;
    }
}

/**
 * class MyQueue1 implemented using ArrayList. The index 0 element is the front of the queue
 * <p>
 * The last element of the queue has index tail
 *
 * @author Your Name
 * @version Date
 */

class MyQueue<E> {

    private ArrayList<E> list; // hold the elements in queue

    private int tail; // index of the last element in queue

    /**
     * constructor construct an empty queue
     */

    public MyQueue() {

        list = new ArrayList();

        // setting tail index to -1 initially, but to be honest, there is no

        // need for this variable, we can implement all the functionalities

        // using list methods

        tail = -1;

    }

    /**
     * isEmpty return true if the queue is empty; false otherwise
     *
     * @return true if the queue is empty; false otherwise
     */

    public boolean isEmpty() {

        return list.isEmpty();

    }

    /**
     * size return the size of the queue
     *
     * @return the number of elements in queue
     */

    public int size() {

        return list.size();

    }

    /**
     * peek return the front element of the queue
     *
     * @return the front element of the queue. If the queue is empty, return
     * <p>
     * null
     */

    public E peek() {

        // returning null if empty

        if (isEmpty()) {

            return null;

        }

        // returning element at index 0

        return list.get(0);

    }

    /**
     * pop remove the front element of the queue
     */

    public void pop() {

        if (!isEmpty()) {

            // removing element at index 0

            list.remove(0);

            // updating tail index, now that list is updated.

            tail--;

        }

    }

    /**
     * push push a new element to the queue
     */

    public void push(E item) {

        // simply adding to the end

        list.add(item);

        // updating tail index

        tail++;

    }

}


/**
 * class MyStack1: A stack class implemented by using ArrayList
 * <p>
 * All stack elements are stored in an ArrayList. The top element
 * <p>
 * has index top
 *
 * @author Your Name
 * @version Date
 */

class MyStack<E> {

    private ArrayList<E> list; // used to store elements in stack

    private int top; // the index of top element

    /**
     * constructor construct an empty stack
     */

    public MyStack() {

        list = new ArrayList();

        top = -1;

    }

    /**
     * push push a given element on the top of the stack
     */

    public void push(E item) {

        // simply adding to the end, incrementing top

        list.add(item);

        top++;

    }

    /**
     * isEmpty return true if the stack is empty; false otherwise
     *
     * @return true if the stack is empty; false otherwise
     */

    public boolean isEmpty() {

        return list.isEmpty();

    }

    /**
     * peek Return the top element
     */

    public E peek() {

        if (isEmpty()) {

            // empty stack

            return null;

        }

        // returning element at top index
        return list.get(top);

    }

    /**
     * pop Remove the top element from the stack. If the stack is empty,nothing
     * <p>
     * happen
     */

    public void pop() {

        if (!isEmpty()) {

            // removing element at top and updating top index

            list.remove(top);

            top--;

        }

    }

    /**
     * size return the size of the stack
     *
     * @return number of elements in stack
     */

    public int size() {

        return list.size();

    }

}

Output:

C: \Program Files\Java\jdk1.8.0_151\bin\java palindrome: Enter a sentence, I will tell you if it is a aba aba is a palind

Please let us know in the comments, if you face any problems.

Add a comment
Know the answer?
Add Answer to:
I was told I need three seperate files for these classes is there anyway to tie...
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
  • how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 }...

    how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 } class Stack { Node head; int size; Stack() //default constructor { this.head=null; this.size=0; } //Input = data //Output = void (just adds value to list) // method pushes elements on stack // time: O(1) // space: O(1) public void push(int data) { Node node=new Node(data); node.next=head; head=node; size++; } //Input = none //Output = top of stack // method pops value from top of...

  • I have added a little Code but I need help with the rest. /** A class...

    I have added a little Code but I need help with the rest. /** A class of stacks whose entries are stored in a chain of nodes. Implement all methods in MyStack class Main Reference : text book or class notes Do not change or add data fields */ package PJ2; public class MyStack<T> implements StackInterface<T> {    // Data fields    private Node<T> topNode; // references the first node in the chain    private int numberOfEntries;       public...

  • In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These...

    In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These files will need to be added to your Java project. They provide data structure functionality that you will build over. It is suggested that you test if these files have been properly added to your project by confirming that Base_A05Q1.java compiles correctly. Complete the implementation of the ArrayStack class. Specifically, complete the implementations of the isEmpty, size, and toString methods. See Base_A05Q1.java for a...

  • JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test...

    JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test it is not executing but my stack is working fine, can you fix it please! MyQueue.java Implement a queue using the MyStack.java implementation as your data structure.  In other words, your instance variable to hold the queue items will be a MyStack class. enqueue(String item): inserts item into the queue dequeue(): returns and deletes the first element in the queue isEmpty(): returns true or false...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns...

    Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizeIS is public int sizeIs() a.) Write the code for sizeIs for the ArrayStack class b.) Write the code for sizeIs for the LinkedStack class (do not add any instance variables to the class; each time sizeIs is called you must "walk" through the stack...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • I need help with this code This is what I need to do: Implement the Stack...

    I need help with this code This is what I need to do: 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...

  • Please Answer this question using the language of C++. I provide you with the picture of figure 1...

    Please Answer this question using the language of C++. I provide you with the picture of figure 18_02. Thank you. I 7/ Fig. 18.2: Stack.h 2 // Stack class template. #ifndef #de fine 3 STACK-H STACK-H 5 #include 7 template 8 class Stack ( 9 public: 10 I const T& top) 12 13 l/ return the top element of the Stack return stack.frontO; // push an element onto the Stack void push(const T& pushValue) 15 stack push front (pushValue); 17...

  • The class pictured below is designed to implement an integer queue using two stacks. Assume the...

    The class pictured below is designed to implement an integer queue using two stacks. Assume the stack methods all work as desired (though their implementations are not shown). .(a) Trace what happens in the following situation, showing intermediate steps (values of variables, what is in the stacks, and what is in the queue at various points in the methods, not just the results of the methods). • Start with an empty queue. Enqueue 5, then 16, then 7. Dequeue twice....

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