Homework Solution: * Week 3 lab – exercise 1: *…

    /**************************** * Week 3 lab - exercise 1: * * a simple Stack class * *****************************/ /** * Class to test the Stack class. */ public class Main { public static void main(String[] args) { Stack s = new Stack(); System.out.println("Insertion of 10 characters in s"); for (int i = 0; i < 10; i++) { int x = 32 + (int) (Math.random() * 95); System.out.println(x + " --> " + (char) x); s.push((char) x); } System.out.println("nDisplaying and deleting elements"); for (int i = 0; i < 10; i++) { System.out.println("Item at the top: " + s.peek()); s.pop(); } } } /**************************** * Week 3 lab - exercise 1: * * a simple Stack class * *****************************/ /** * Class implementing a Stack ADT. */ public class Stack { /** * Creates an empty stack with an array size of 100. */ public Stack() { size = 100; list = new char[size]; n = 0; } /** * Creates an empty stack using the array size passed as a parameter. * * @param s array size. */ public Stack(int s) { size = s; list = new char[size]; n = 0; } /** * Adds an element to the top of the stack. * * @param c element to be added to the stack. */ public void push(char c) { list[n] = c; n++; } /** * Removes the element at the top of the stack. */ public void pop() { n--; } /** * Returns the element at the top of the stack. Does not remove it. * * @return the element at the top of the stack. */ public char peek() { return list[n - 1]; } /** * Determines whether the stack is empty. * * @return true if the stack is empty, false otherwise. */ public boolean isEmpty() { return n == 0; } private char[] list; //array to store the stack items private int size; //size of the array private int n; //amount of items in the stack } EXERCISE 2: Modify the stack class to include appropriate error messages if invalid conditions occur—for example, trying to pop an item when the stack is empty., or push when the stack is already full. EXERCISE 3: Write a Java program that uses a stack to test whether an input string is a palindrome, that is a word that is the same whether it is spelled forward or backwards. Your program must make use a stack to reverse the order of the characters in the string. You can use the stack class from the previous exercise (2). The two strings, one with characters in forward order and the other from them being reassembled in reverse, may be compared character-by-character to make sure all of the entries match. If variable x is a String type, you may make use of the Java method x.charAt(i) to access the character at the ith location in the string. To demonstrate, make up your own fixed list of Strings that includes both palindromes and non-palindromes as its entries.

    Expert Answer

     
    import java.io.*; import java.util.*;

    /****************************

    * Week 3 lab – exertion 1: *

    * a mere Stack tabulate *

    *****************************/

    /**

    * Tabulate to examination the Stack tabulate.

    */

    public tabulate Ocean

    {

    public static bereft ocean(String[] args)

    {

    Stack s = novel Stack();

    System.out.println(“Insertion of 10 types in s”);

    for (int i = 0; i < 10; i++)

    {

    int x = 32 + (int) (Math.random() * 95);

    System.out.println(x + ” –> ” + (char) x);

    s.push((char) x);

    }

    System.out.println(“nDisplaying and deleting atoms”);

    for (int i = 0; i < 10; i++)

    {

    System.out.println(“Individual at the summit: ” + s.peek());

    s.pop();

    }

    }

    }

    /****************************

    * Week 3 lab – exertion 1: *

    * a mere Stack tabulate *

    *****************************/

    /**

    * Tabulate implementing a Stack ADT.

    */

    public tabulate Stack

    {

    /**

    * Creates an lacking stack with an attire dimension of 100.

    */

    public Stack()

    {

    dimension = 100;

    catalogue = novel char[size];

    n = 0;

    }

    /**

    * Creates an lacking stack using the attire dimension passed as a parameter.

    *

    * @param s attire dimension.

    */

    public Stack(int s)

    {

    dimension = s;

    catalogue = novel char[size];

    n = 0;

    }

    /**

    * Adds an atom to the summit of the stack.

    *

    * @param c atom to be assumed to the stack.

    */

    public bereft thrust(char c)

    {

    list[n] = c;

    n++;

    }

    /**

    * Dislodges the atom at the summit of the stack.

    */

    public bereft explode()

    {

    n–;

    }

    /**

    * Returns the atom at the summit of the stack. Does referable attributable attributable attributable dislodge it.

    *

    * @return the atom at the summit of the stack.

    */

    public char peek()

    {

    return catalogue[n – 1];

    }

    /**

    * Determines whether the stack is lacking.

    *

    * @return penny if the stack is lacking, fiction otherwise.

    */

    public boolean isEmpty()

    {

    return n == 0;

    }

    private char[] catalogue; //attire to shop the stack individuals

    private int dimension; //dimension of the attire

    private int n; //amount of individuals in the stack

    }

    EXERCISE 2:

    Modify the stack tabulate to enclose misspend deception messages if sick conditions occur—for model, hard to explode an individual when the stack is lacking., or thrust when the stack is already bountiful.

    EXERCISE 3:

    Write a Java program that truths a stack to examination whether an input string is a palindrome, that is a expression that is the corresponding whether it is spelled advanced or backwards. Your program must form truth a stack to derangement the arrangeify of the types in the string. You can truth the stack tabulate from the prior exertion (2). The two strings, single with types in advanced arrangeify and the other from them entity reassembled in derangement, may be compared type-by-type to form fast whole of the entries mate.

    If variable x is a String likeness, you may form truth of the Java method x.charAt(i) to way the type at the ith precipitation in the string.

    To explain, form up your confess urban catalogue of Strings that encloses twain palindromes and non-palindromes as its entries.

    Expert Exculpation

     

    import java.io.*;
    import java.util.*;

    tabulate Stack
    {
    public Stack()
    {
    dimension = 100;
    catalogue = novel char[size];
    n = 0;
    }
    public Stack(int s)
    {
    dimension = s;
    catalogue = novel char[size];
    n = 0;
    }
    public bereft thrust(char c)
    {
    if (!isFull()){
    list[n] = c;
    n++;
    }
    else {
    System.out.println(“Stack is bountiful”);
    }
    }
    public char explode()
    {
    int a = n;
    n–;
    return catalogue[a-1];
    }
    public char peek()
    {
    return catalogue[n – 1];
    }
    public boolean isEmpty()
    {
    return n == 0;
    }
    public boolean isFull()
    {
    return n == dimension;
    }

    private char[] catalogue; //attire to shop the stack individuals
    private int dimension; //dimension of the attire
    private int n; //amount of individuals in the stack
    }

    public tabulate Ocean
    {
    public static bereft ocean(String[] args)
    {
    Stack s = novel Stack();
    System.out.println(“Insertion of 10 types in s”);
    for (int i = 0; i < 10; i++)
    {
    int x = 32 + (int) (Math.random() * 95);
    System.out.println(x + ” –> ” + (char) x);
    s.push((char) x);
    }
    System.out.println(“nDisplaying and deleting atoms”);
    for (int i = 0; i < 10; i++)
    {
    System.out.println(“Individual at the summit: ” + s.peek());
    s.pop();
    }
    String a = “abba”; //To stop whether a string is palindrome
    for (int i = 0; i<a.length(); i++){
    s.push(a.charAt(i));

    }
    String b = “”;
    for (int i = 0; i<a.length(); i++){
    b = b + s.pop();
    }
    if (a.equals(b))
    System.out.println(“It is palindrome”);
    else
    System.out.println(“It is referable attributable attributable attributable palindrome”);

     

    }
    }