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...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
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...
Write a Java application that implements the following class(es) as per business requirements mentioned below: Create...
Write a Java application that implements the following class(es) as per business requirements mentioned below: Create an abstract Insurance class (Insurance.java) that has the following instance variables: - Insurance number, - customer name, - start date of policy ( This should be represented by an object of predefined date class in java), - customer address [( Create a new Address class ( Address.java ) having following instance data members: House number , Street name, City, Province, Zip code. Implement getter...
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.
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS...
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN Your program will service a disk with 5,000 cylinders numbered 0 to 4,999. The program will generate a random series of 50 requests and service them according to each of the algorithms you chose. The program will be passed the initial position of the disk head as a parameter on the command line and report the total amount of head...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT