Question

In: Computer Science

create a stack class based on an array of type double of fixed size. In the...

create a stack class based on an array of type double of fixed size. In the class,

The array should have the default size of 5.

The array size should be settable via a constructor.

The following methods must be implemented:

push – inserts a value a the top of the stack

pop – returns the top value, removing it from the stack

isEmpty – returns true if the stack is empty, false otherwise

isFull - – returns true if the stack is full, false otherwise

Please notice you must use an array not an arraylist or any other list. Im having trouble with a couple parts of it. Thanks

Also use java

Solutions

Expert Solution

class Stack {
   private int size;
   private double[] data;

   /**
   *
   */
   public Stack() {
       data = new double[5];
       size = 0;
   }

   /**
   * @param maxSize
   */
   public Stack(int maxSize) {
       data = new double[maxSize];
       size = 0;
   }

   /**
   *
   */
   public void display() {

       for (int i = 0; i < size; i++) {

           System.out.print(data[i] + ", ");
       }
       System.out.println();

   }

   /**
   * inserts a value a the top of the stack
   *
   * @param value
   */
   public void push(double value) {
       if (size >= data.length)
           throw new RuntimeException("Stack full");
       data[size] = value;
       size++;
   }

   /**
   * returns the top value, removing it from the stack
   *
   * @return
   */
   public double pop() {
       if (isEmpty())
           throw new RuntimeException("Stack empty");
       size--;
       return data[size];
   }

   /**
   * returns true if the stack is empty, false otherwise
   *
   * @return
   */
   public boolean isEmpty() {
       return size == 0;
   }

   /**
   * returns true if the stack is full, false otherwise
   *
   * @return
   */
   public boolean isFull() {
       return size == data.length;
   }

   /**
   * @param args
   */
   public static void main(String[] args) {

       Stack stack = new Stack();
       // displaying output
       stack.push(8.3);
       stack.push(6.3);
       stack.push(4.3);
       stack.push(2.3);
       stack.push(4.1);
       System.out.println("FINAL STACK CONTENT ");
       stack.display();

       System.out.println("pop from stack-->" + stack.pop());
       System.out.println("check stack is empty-->" + stack.isEmpty());
       stack.push(9.1);
       System.out.println("check stack is full-->" + stack.isFull());
       System.out.println(" ");

   }
}

OUTPUT:

FINAL STACK CONTENT
8.3, 6.3, 4.3, 2.3, 4.1,
pop from stack-->4.1
check stack is empty-->false
check stack is full-->true


Related Solutions

IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array of signed integers. Write a program that creates an Array container of size 100 and fills it with random numbers in the range [1..999]. (Use std::rand() with std::srand(1).) When building the array, if the random number is evenly divisible by 3 or 5, store it as a negative number. Within your main.cpp source code file, write a function for each of the following processes....
Implement a stack in C++ using an array, not an array list. Make your stack size...
Implement a stack in C++ using an array, not an array list. Make your stack size 5 when you test it, but do not hardcode this! You should be able to change the size for testing purposes with the change of one variable. DO NOT use Stack class defined in C++ Implement the following methods in your stack class. stack() creates an empty stacks, stack s is new and empty. push(item) adds a new item to the stack s, stacks...
Write a class DataSet that stores a number of values of type double in an array...
Write a class DataSet that stores a number of values of type double in an array list. Provide methods to insert a value to the array list and to compute the sum, average, and maximum value. Please put a note on what you did is for what. So I can understand this question better. Code provided: import java.util.*; public class DataSet {    private ArrayList<Double> data; /** Constructs an empty data set. @param maximumNumberOfValues the maximum this data set can...
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
Given an array of Student type and size 10, create a linked list of students by...
Given an array of Student type and size 10, create a linked list of students by linking students with an odd index first and then linking students with an even index. Write a loop to print out the students in the linked list #include<iostream> #include<string> #include<fstream> using namespace std; const int NUM = 10; struct Student{ string fName; string lName; Student * next; }; int main() {        Student stuArr[NUM];        ifstream myfile;        myfile.open("Test.txt");        for(int i = 0;...
Given an array of Student type and size 10, create a linked list of students by...
Given an array of Student type and size 10, create a linked list of students by linking students with an odd index first and then linking students with an even index. Write a loop to print out the students in the linked list. #include #include #include using namespace std; const int NUM = 10; struct Student{ string fName; string lName; Student * next; }; int main() { Student stuArr[NUM]; ifstream myfile; myfile.open("Test.txt"); for(int i = 0; i < NUM; i++)...
Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers.
Programing in Scala language: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers. Write separate methods to get an element (given parametersrow and col), set an element (given parametersrow, col, and value), and output the matrix to the console formatted properly in rows and columns. Next, provide an immutable method to perform array addition given two same-sized array.
In C Programing Create a stack using an array. Define the index variable of the array...
In C Programing Create a stack using an array. Define the index variable of the array name to be stacktop. Initially set stacktop to -1. Next create two functions push and pop. Both take as input 3 items: a pointer to the stack in memory, stacktop, and maxstack. Make sure to inc stacktop by one and also remember that stacktop[0] is the bottom of the stack and by stack rule cannot be accessed until all the other items are popped....
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate,...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also...
using java Create a class Rectangle with attributes length and width both are of type double....
using java Create a class Rectangle with attributes length and width both are of type double. In your class you should provide the following: Provide a constructor that defaults length and width to 1. Provide member functions that calculate the area, perimeter and diagonal of the rectangle. Provide set and get functions for the length and width attributes. The set functions should verify that the length and width are larger than 0.0 and less that 50.0. Provide a member function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT