Question

In: Computer Science

Can you fix my code and remove the errors? Thank you!! ^^ ////////////////////////////////////////////////////////////////////////////////////////////////////// public class StackException<T,...

Can you fix my code and remove the errors? Thank you!! ^^

//////////////////////////////////////////////////////////////////////////////////////////////////////

public class StackException<T, size> extends Throwable {


    private final T[] S =  null ;

    public StackException(String s) {
    }

    public T top() throws StackException {

        if (isEmpty()) throw new StackException("Stack is empty.");
        int top = 0;
        return S[top];

    }

    private boolean isEmpty() {
        return false;
    }

    public T pop() throws StackException {

        T item;

        if (isEmpty()) throw new StackException("Stack underflow.");
        int top = 0;
        item = S[top];

        S[top--] = null;

        return item;

    }

    public void push(T item) throws StackException {

        if (<Object capacity = null;
        size() == capacity >)throw new StackException("Stack overflow.");
        int top = 0;
        S[++top] = item;

    }
    private void size() {
    }
}

Solutions

Expert Solution

Hey, I have changed the class name because generic exception class can't be created. I have used array of Object because we can't create array of generic.

Java code:

// stack exception class which print error msg
class StackException extends Throwable {
   StackException(String s) {
       System.out.println(s);
   }
}

// Generic class Stack
// we can't create generic exception class
// that's why i have changed the name of the class
public class Stack<T> {

private Object[] S = null;
private int capacity;
private int top ;
  
public Stack() {
   this.S = new Object[10];
   this.capacity = 10;
   this.top = -1;
}
  
public Stack(int cap) {
   this.S = new Object[cap];
   this.capacity = cap;
   this.top = -1;
}

@SuppressWarnings("unchecked")
   public T top() throws StackException {

if (isEmpty()) throw new StackException("Stack is empty.");
  
return (T)S[top];
}

private boolean isEmpty() {
if (size() == 0 )
   return true ;
return false;
}

@SuppressWarnings("unchecked")
   public T pop() throws StackException {

T item;

if (isEmpty()) throw new StackException("Stack underflow.");
item = (T)S[top];

S[top--] = null;

return item;

}

public void push(T item) throws StackException {

if (size() == capacity)throw new StackException("Stack overflow.");
  
S[++top] = item;

}
private int size() {
   return top + 1;
}
}

I have tested this code and it's working fine.

Hope you like it

Any Query? Comment Down!

I have written for you, Please up vote the answer as it encourage us to serve you Best !


Related Solutions

Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public static void main(String[] args) {        System.out.println("This program will ask the user for three sets of two numbers and will calculate the average of each set.");        Scanner input = new Scanner(System.in);        int n1, n2;        System.out.print("Please enter the first number: ");        n1 = input.nextInt();        System.out.print("Please enter the second number: ");        n2 =...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner;...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner; public class Booolean0p {        public class BooleanOp {            public static void main(String[] args) {                int a = 0, b = 0 , c = 0;                Scanner kbd = new Scanner(System.in);                System.out.print("Input the first number: ");                a = kbd.nextInt();                System.out.print("Input...
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public...
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public Fraction () {    numerator = 0;    denominator = 1;    numberOfFraction++; }    public Fraction (int n, int d) {    numerator = n;    denominator = d;    numberOfFraction++; } private int gcd (int num1, int num2) {    if (num1 == 0)    return num2;    return gcd (num2 % num1, num1); }    public Fraction add (Fraction third) {    int n = numerator * third.denominator + third.numerator * denominator;    int...
I am getting 7 errors can someone fix and explain what I did wrong. My code...
I am getting 7 errors can someone fix and explain what I did wrong. My code is at the bottom. Welcome to the DeVry Bank Automated Teller Machine Check balance Make withdrawal Make deposit View account information View statement View bank information Exit          The result of choosing #1 will be the following:           Current balance is: $2439.45     The result of choosing #2 will be the following:           How much would you like to withdraw? $200.50      The...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that the following test cases work. Note: I have removed all the unnecessary inherited List implementations. I have them to: throw new UnsupportedException(); For compilation, you could also add //TODO. Test (Main) List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size...
identify the syntax errors in the following code:             public class Hello {                    &
identify the syntax errors in the following code:             public class Hello {                         private static int main(String [] args) {                                     string Msg=”Hello, Wrld!;                                     Sytem.out.println(msg+ “Ken")
Fix the following java code package running; public class Run {    public double distance; //in...
Fix the following java code package running; public class Run {    public double distance; //in kms    public int time; //in seconds    public Run prev;    public Run next;    //DO NOT MODIFY - Parameterized constructor    public Run(double d, int t) {        distance = Math.max(0, d);        time = Math.max(1, t);    }       //DO NOT MODIFY - Copy Constructor to create an instance copy    //NOTE: Only the data section should be...
In Java, please write a tester code. Here's my code: public class Bicycle {     public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public int cadence; public int gear;   public int speed;     public Bicycle(int startCadence, int startSpeed, int startGear) {         gear = startGear;   cadence = startCadence; speed = startSpeed;     }     public void setCadence(int newValue) {         cadence = newValue;     }     public void setGear(int newValue) {         gear = newValue;     }     public void applyBrake(int decrement) {         speed -= decrement;    ...
Remove the Head element from the code below: public class LinkedList {    class Node{ int...
Remove the Head element from the code below: public class LinkedList {    class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element);    if(first == null) { first = newValueNode; } else { last.nextElement = newValueNode; } last = newValueNode; } public void displayValues() { Node recent = first; if(first ==...
----fix code to search and delete a student by Identification number import java.util.Scanner; public class COurseCom666...
----fix code to search and delete a student by Identification number import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public String getCourseName() {         return courseName;     }     public int DeleteStudentsByID() {         return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT