Question

In: Computer Science

Write a java application that implements ArrayStack in chapter 16 of your textbook.

Write a java application that implements ArrayStack in chapter 16 of your textbook.

Your application should have two files:

1. ArrayStack.java: This file will have the implementation of your stack.

2. TestStack.java: This file will have the main method where you declare your stack object and test the different stack operations.

In addition to the ArrayStack methods given in the book, you are to write a toString method. This will allow you to print the Stack object from main.

You should be able to find the doubleArray method implementation via google searching.

Demonstrate all stack operations by calling them in your main and using print statements to explain the method calls. A sample output is given below, you should do something similar.

The current stack is empty: [] After 5 push operations the stack has 5 elements [A, B, C, D, E] The current top of stack is E TopAndPop returns E. The top of stack is currently D After 2 pop operations the stack has 5 elements [A, B] Currently the stack is not empty After calling makeEmpty, the stack becomes empty: []

Solutions

Expert Solution

ArrayStack class-

import java.util.Arrays;
//create ArrayStack class
public class ArrayStack {
   int size=5;
   //creating character Array
char stack[]=new char[size];
//set top to -1
int top=-1;
//push method body
   public void push(char v){
if(top==size-1)
{
    System.out.println("Stack is full");
}
else{
    stack[++top]=v;
    System.out.println("Pushed SuccessFully");
}
   }
   //pop method body
   public void pop()
   {
       char v=stack[top];
       --top;
       System.out.println("Poped Element is: "+v);
   }
   public void show(){
       if(top==-1)
       {
           System.out.println("Stack is empty");
       }
       else {
System.out.println("Stack Element Are: ");
System.out.print("[");
for(int i=0;i<=top;i++)
{
System.out.print(stack[i]+",");
}
System.out.println("]");
       }
   }
   //empty method
   public void makeEmpty()
   {
       top=-1;
   }
   //toString method for printing the ArrayStack object
   public String toString() {
      return Arrays.toString(stack) ;
   }
}
TestStack class-

import java.util.Scanner;

//creating a class TestStack
public class TestStack {
   //creating object of scanner class
   static Scanner sc=new Scanner(System.in);
   //creating object of ArrayStack class
   static ArrayStack my=new ArrayStack ();
   //main method
   public static void main(String[] args) {
       while(true)
       {
       int ch=0;
       System.out.println("1: push");
       System.out.println("2: pop");
       System.out.println("3: show");
       System.out.println("4: make Empty");
       System.out.println("5: exit");
System.out.println("Enter your Choice");
ch=sc.nextInt();
switch(ch)
{
   case 1:System.out.println("Enter Stack Element");
   //taking character input from user
       char value=sc.next().charAt(0);
       //call push method
       my.push(value);
   break;
   case 2://call pop method
       my.pop();
   break;
   case 3: //display MyStack object using toString method
       System.out.println(my);
   break;
   case 4 :
       //call makeEmpty method for clean the stack
       my.makeEmpty();
   System.out.println("now stack is empty");
   break;
   case 5: System.exit(0);
   default: System.out.println("Invalid choice!!");   
}
}
}
}
note - this is only for character.


Related Solutions

USING JAVA and NOTTT using BufferedReader or BufferedWriter Write an application that implements a simple text...
USING JAVA and NOTTT using BufferedReader or BufferedWriter Write an application that implements a simple text editor. Use a text field and a button to get the file. Read the entire file as characters and display it in a TextArea. The user will then be able to make changes in the text area. Use a Save button to get the contents of the text area and write that over the text in the original file. Hint: Read each line from...
Eclipse Exercise 1 Write a Java application that implements different types of insurance policies for employees...
Eclipse Exercise 1 Write a Java application that implements different types of insurance policies for employees of an organization. Let Insurance be an abstract superclass and Health and Life two of its subclasses that describe respectively health insurance and life insurance. The Insurance class defines an instance variable of type String to describe the type of insurance and an instance variable of type double to hold the monthly cost of that insurance. Implement the get methods for both variables of...
Develop a Java application which implements an application for a store chain that has three types...
Develop a Java application which implements an application for a store chain that has three types of stores which are Book, Music, and Movie stores. Your application should have an Item abstract class which should be extended by the Book and Multimedia classes. Item class has abstract priceAfterTax method, you need to implement this method in derived classes. Multimedia class is a superclass for Music and Movie classes. Your project should also include the IPromotion interface, which should be implemented...
Chapter 16 in your textbook sets forth the fundamental principles essential for understanding the concept of...
Chapter 16 in your textbook sets forth the fundamental principles essential for understanding the concept of materials requirements planning. Create a basic outline that captures the key points of the chapter. As a guide for your outline, it will be helpful to note the "key points" that are listed at the end of the chapter (section 16.8).
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm. Create a second Java Application that implements an Insertion sort algorithm to sort the same array. Again, output the array in its original order, then output the array after it has been sorted...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm.
Part 1: Marketing & Community Relations: After reading chapter 16 of your textbook, you are to...
Part 1: Marketing & Community Relations: After reading chapter 16 of your textbook, you are to use the Internet and research a long-term care facility in your area. After studying the organization, you are to address the following questions: a. How does the facility “brand” its product? b. Does the long-term care facility engage in community involvement? 3. Your initial response must be 250 words in length and posted on or before Wednesday.
Develop a Java application using Parboiled library to write aparser for a customer form. Your...
Develop a Java application using Parboiled library to write a parser for a customer form. Your program should include a grammar for the parser and display the parse tree with no errors.The customer form should include the following structure:First name, middle name (optional), last nameStreet address, city, state (or province), countryPhone numberRules• First name, middle name and last name should start with an uppercase letter followed by lower case letters. Middle name is an optional input meaning such an input...
java from control structures through objects 6th edition, programming challenge 5 on chapter 16 Write a...
java from control structures through objects 6th edition, programming challenge 5 on chapter 16 Write a boolean method that uses recursion to determine whether a string argument is a palindrome. the method should return true if the argument reads the same forward amd backword. Demonstrate the method in a program, use comments in the code for better understanding. there must be a demo class and method class also .. generate javadocs through eclipse compiler. And make a UML diagram with...
Write a Java program (use JDBC to connect to the database) that implements the following function...
Write a Java program (use JDBC to connect to the database) that implements the following function (written in pseudo code): (20 points) CALL RECURSION ( GIVENP# ) ; RECURSION: PROC ( UPPER_P# ) RECURSIVE ; DCL UPPER_P# ... ; DCL LOWER_P# ... INITIAL ( ' ' ) ; EXEC SQL DECLARE C CURSOR FOR SELECT MINOR_P# FROM PART_STRUCTURE WHERE MAJOR_P# = :UPPER_P# AND MINOR_P# > :LOWER_P# ORDER BY MINOR_P# ; print UPPER_P# ; DO "forever" ; EXEC SQL OPEN C...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT