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 my code and remove the errors in java language. public class LinkedStack<T> implements...
Can you fix my code and remove the errors in java language. public class LinkedStack<T> implements Stack<T> { private Node<T> top; private int numElements = 0; public int size() { return (numElements); } public boolean isEmpty() { return (top == null); } public T top() throws StackException { if (isEmpty()) throw new StackException("Stack is empty."); return top.info; } public T pop() throws StackException { Node<T> temp; if (isEmpty()) throw new StackException("Stack underflow."); temp = top; top = top.getLink(); return temp.getInfo();...
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...
Java question: I need to fix a point class (code below) Thank you! /** * A...
Java question: I need to fix a point class (code below) Thank you! /** * A point, implemented as a location without a shape. */ public class Point extends Location { // TODO your job // HINT: use a circle with radius 0 as the shape! public Point(final int x, final int y) { super(-1, -1, null); assert x >= 0; assert y >= 0; } }
I need this code translated to C code. Thank you. public class FourColorTheorem { public static...
I need this code translated to C code. Thank you. public class FourColorTheorem { public static boolean isPrime(int num) { // Corner case if (num <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < num; i++) if (num % i == 0) return false; return true; } public static void main(String[] args) { int squares[] = new int[100]; for (int i = 1; i < squares.length; i++) squares[i-1] = i * i;...
This is the code that needs to be completed... Thank you! public class MultiplicationTable { private...
This is the code that needs to be completed... Thank you! public class MultiplicationTable { private int[][] table; private int rows; private int cols;    /* Instantiate the two dimensional array named table. * Assign the numRows parameter to the rows field. * Assign the numCols parameter in the cols field. */ public MultiplicationTable(int numRows, int numCols) {    } /* Using nested for loops, fill the table array with * the values shown in the Multiplication Table document. */...
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...
C++ Program for Project Alarm. ------------------------------------------------------------------------------ Fix ALL Errors in my EXISTING code for ALL 5...
C++ Program for Project Alarm. ------------------------------------------------------------------------------ Fix ALL Errors in my EXISTING code for ALL 5 files.   MUST add comments for FULL CREDIT. Take a screenshot of the working solution output. ------------------------------------------------------------------------------- Instructions: Use class composition to define and implement a new class called Alarm that contains a Time member variable. A. Define and implement the class Alarm as follows: The class Alarm consists of two private member variables: description of type string and atime of type Time. The class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT