Question

In: Computer Science

please follow this pseudocode 1) Create class (any name you want)ex: aS 2) Declaring private integer...

please follow this pseudocode

1) Create class (any name you want)ex: aS
2) Declaring private integer array empty one ex: name arr[]
3) Declare private integer variables top, size and len (my example)

4) Declare constructor for the class with the same name and inside of that
public aS declare (parameter ex: int n)
  
5) Inside of that constructor method write command where:
size is equal to n
len is equal to zero
array (name) is equal to new integer[size]
and
top is equal minus 1

6)
Declare function to check if Stack is empty
In this function called public you should declare boolean (any name you want)
Which return top is equal minus 1

7)

Declare function to check if stack is full
Again another public boolean (any name you want)
Which return top is equal to size minus 1

8)
Declare function to get the size of the stack
This is public integer (any name you want)
Which return len
  
  
9)

Declare function to check the top element of the stack.
This is public integer peek
Which calls the function to see if is empty (we created above)
You can write underneath
throw new NoSuchElementException("Underflow Exception");
then
return name of your array[top]
  
10)

Declare function to add an element to the stack
Called public void push with parameters integer i
Condition:
if top + 1 is greater equal to size then you can write again
throw new NoSuchElementException("Underflow Exception");
if top + 1 is less equal to size
name of your array [increment top] equal to i

len increment

11)

Declare function to delete an element from the stack

This is public integer pop
Which calls the function to see if is empty (we created above)
You can write underneath
throw new NoSuchElementException("Underflow Exception");
then
increment len
then
return name of array[decrement top]
  
  
12)

Declare function to display the stack

This is public void display

system print out Stack: =
Condition
if len is equal to zero
then
system print out empty
return only
close the brackets
then
create for loop where int i is equal to top and i is greater equal to zero and i decrement
inside of that loop
system print name of array[i]+" "

13)

Scanner scan = new Scanner(System.in);

System.out.println("Enter Size of Stack ");

int n = scan.nextInt();

  
aS s = new aS(n);


char ch;

do{

System.out.println("\nStack Operations");

System.out.println("1. Push");

System.out.println("2. Pop");

System.out.println("3. Peek");

System.out.println("4. Check if is empty");

System.out.println("5. Check if is full");

System.out.println("6. Get the Size of Stack”);

int choice = scan.nextInt();

switch (choice)

Solutions

Expert Solution

// java

// if you have any problem let me know i will try to help you. Thank you.



import java.util.NoSuchElementException;
import java.util.Scanner;

class aS{
        private int arr[];
        private int top;
        private int size;
        private int len=0;
        public aS(int n)
        {
                size=n;
                len=0;
                arr=new int[size];
                top=-1;
                
        }
        
        public boolean isEmpty()
        {
                return top==-1;
        }
        
        public boolean isFull()
        {
                return top==size-1;
        }
        public int size()
        {
                return len;
        }
        
        public int peek()
        {
                if(isEmpty())
                        throw new NoSuchElementException("Underflow Exception");
          return arr[top];
        }
        
        public void push(int value)
        {
                if(isFull())
                        throw new NoSuchElementException("Underflow Exception");
                top++;
                arr[top]=value;
                len++;
        }
        
        public int pop()
        {
                if(isEmpty())
                        throw new NoSuchElementException("Overflow Exception");
         int temp= arr[top];
         top--;
         len--;
         return temp;
        }
        
        public void display()
        {
                System.out.print("[");
                for(int i=top;i>=0;i--)
                {
                        System.out.print(arr[i]+" ");
                }
                System.out.println("]");
        }
        
}
public class Main {

        public static void main(String[] args) {
        
        
                Scanner sc=new Scanner(System.in);
                System.out.println("Enter Size of Stack ");

                int n = sc.nextInt();

                  
                aS s = new aS(n);


                char ch='n';

                do{

                System.out.println("\nStack Operations");

                System.out.println("1. Push");

                System.out.println("2. Pop");

                System.out.println("3. Peek");

                System.out.println("4. Check if is empty");

                System.out.println("5. Check if is full");

                System.out.println("6. Get the Size of Stack");

                int choice = sc.nextInt();

                switch (choice) {
                case 1:
                        System.out.print("Enter the value to push: ");
                        int val=sc.nextInt();
                        s.push(val);
                        s.display();
                        break;
                case 2:
                        s.pop();
                        s.display();
                        break;
                case 3:
                        System.out.println("Peek element is : "+s.peek());
                        break;
                case 4:
                        System.out.println("Is stack empty?"+s.isEmpty());
                        break;
                case 5:
                        System.out.println("Is stack full?"+s.isFull());
                        break;
                case 6:
                        System.out.println("size of stack: "+s.size());
                        break;
                }
      System.out.print("Do you want to perform more operation(y/n):  ");
      ch=sc.next().charAt(0);
        }while(ch=='y'||ch=='Y');

}
}

Related Solutions

write the code based on this pseudocode //Create Class any name (ex: aQ) //Declaring private integer...
write the code based on this pseudocode //Create Class any name (ex: aQ) //Declaring private integer array empty one ex: name arrQ[] //Declare private integers front, rear, size and len (example, but you call any other names) //Write constructors Called public and same name of the class (parameters integer n) Inside Declare where: size is equal to n len is equal to zero arrQ is equal to new integer[size] front is equal minus 1 rear is equal minus 1 //Declare...
follow pseudo 1) Create class called Node Declare private integer called data (or any other name)...
follow pseudo 1) Create class called Node Declare private integer called data (or any other name) Declare private Node called link (or any other name) 2) Declare constructor Should be public (ex: Node) where: link is equal to null data is equal to zero 3) Declare another constructor public Node with parameters integer d, Node n where: data is equal to d link is equal to n 4) Declare function to set link to next Node link equal to n...
PSEUDOCODE: 1. You are designing software for a voting booth. Please create pseudocode for a modular...
PSEUDOCODE: 1. You are designing software for a voting booth. Please create pseudocode for a modular program that: - Takes in a user inputted integer for age. If their age is below 18, display "you are too young to vote" - Only If their age is high enough, please ask them which candidate they wish to vote for. Valid options are "dog", "cat", "horse" - If they did not choose a valid option display "you did not choose a valid...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods:...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods: 1. numberOfStudents 2. getName 3. getStudentID 4. getCredits 5. getLoginName 6. getTime 7. getValue 8. getDisplayValue 9. sum 10. max Show Class17Ex.java file with full working please. Let me know if you have any questions.
Please write in C++ as simple as possible I want you to create a Book Class...
Please write in C++ as simple as possible I want you to create a Book Class for a bookstore. I am not going to tell you what variables should go into it, that is for you to figure out (but you should have at least 5+). And then you must create a UML with all the variables and methods (like: the getters and setters, a default constructor, and a constructor that takes all the variables and finally the printValues() )....
1. Create a class Car with data: Name, Price, Production and properly methods. 2. Create another...
1. Create a class Car with data: Name, Price, Production and properly methods. 2. Create another class named GenericCar with a parameter of the T type. This class manages a collection of object T (may be LinkedList) named a. Implementing some methods for GenericCar: Add: add new item of T to a Display: display all items of a getSize: return the number item of a checkEmpty: check and return whether a is empty or not delete(int pos): remove the item...
Java programming. ** Create Account class include: 1/ private long   accountNumber; 2/ private String firstName; 3/...
Java programming. ** Create Account class include: 1/ private long   accountNumber; 2/ private String firstName; 3/ private String lastName; 4/ private double balance; 5/ public Account(long accountNumber, String firstName, String lastName, double balance); 6/ get/set methods for all attributes. 7/ public boolean deposit(double amount);   • deposit only if amount is greater than 10 but   less than 200, add the deposit   amount   to the   currentbalance. • if deposit is   successful return true,   otherwise return false; 8/ public boolean withdrawal(double amount); •...
INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well...
INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields in the...
1: Create a class Circle 2: Create two instances of the Circle class 1: Based on...
1: Create a class Circle 2: Create two instances of the Circle class 1: Based on Program 13-1 (see attachment Pr13-1.cpp) in the textbook, create a class name “Circle” with the following declarations (hint: you can use PI=3.14): //Circle class declaration class Circle { private: double radius; public: void setRadius(double); double getRadius() const; double getArea() const; double getPerimeter() const; }; 2: Based on Program 13-2 (see attachment Pr13-2.cpp) in the textbook, create two instances of the Circle class, pizza1 and...
Create a PHP class named "User" with the following private fields: name, birthdate in yyyy/mm/dd format,...
Create a PHP class named "User" with the following private fields: name, birthdate in yyyy/mm/dd format, age, and department. In the class, include getter and setter methods to get and set the values of those variables. Author a data entry webform using HTML text input boxes and a submit button. When the user clicks on the submit button, a "User" class is instantiated and the new User object's fields are populated. The HTML input boxes correspond to the field in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT