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...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that defines an integer parameter to set the private integer attribute. Create a setter that validates the attribute does not accept a value lower than 2 or the method will throw a IllegalArgumetException. Create a getter to return the private integer attribute value. Define a public method that is called isPrime() that returns a boolean and implements the Sieve of Eratosthenes method. Define a public...
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 Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last...
Please Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games, and Goals Have two constructors Constructor 1 – default constructor; all values to "NONE" or zero Constructor 2 – accepts input of first name, last name, games and goals. Create get and set methods for each of the four attributes Create a method the returns a double that calculates the average goals per game This method checks for zero games played: If...
C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
Specifications Create an abstract Employee class that provides private attributes for the first name, last name,...
Specifications Create an abstract Employee class that provides private attributes for the first name, last name, email address, and social security number. This class should provide functions that set and return the employee’s first name, last name, email address, and social security number. This class has a function: get_net_income which returns 0. Create a Manager class that inherits the Employee class. This class should add private attributes for years of experience and the annual salary. This class should also provide...
JAVA Program Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games,...
JAVA Program Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games, and Goals Have two constructors Constructor 1 – default constructor; all values to "NONE" or zero Constructor 2 – accepts input of first name, last name, games and goals. Create get and set methods for each of the four attributes Create a method the returns a double that calculates the average goals per game This method checks for zero games played: If there are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT