Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6356247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:55:38+00:00 2026-05-24T22:55:38+00:00

I am creating this stack class with the following methods as shown . import

  • 0

I am creating this stack class with the following methods as shown .

 import java.util.ArrayList;
    import java.util.EmptyStackException;


    public class SortableStack<E extends Comparable<E>> implements ISortableStack<E> {
        private int N;          
        private Node first;     


        private class Node {
            private E e;
            private Node next;
        }


        public SortableStack() {
            first = null;
            N = 0;
        }


    private ArrayList<E> listOne = new ArrayList<E>();



    public boolean isEmpty() {
            return first == null;
        }


        public int size() {
            return N;
        }
        public void push(E e) {
            Node oldfirst = first;
            first = new Node();
            first.e = e;
            first.next = oldfirst;
            N++;
        }
        public E pop() {
            if (isEmpty()) throw new RuntimeException("Stack underflow");
            E e = first.e;        // save e to return
            first = first.next;            // delete first node
            N--;
            return e;                   // return the saved e
        }


    public E peekMidElement() {
        if(listOne.size() <= 0){
        throw new EmptyStackException();
        }

        return listOne.get(listOne.size()/2);
        }

    public E peekHighestElement() {
        if(listOne.size() <= 0){
        throw new EmptyStackException();
        }

        return listOne.get(listOne.size() - 1);
        }

    public E peekLowestElement() {
        if(listOne.size() <= 0){
        throw new EmptyStackException();
        }

        return listOne.get(0);
        }
    }`

//The interface ISortableStack is [here][1]
(The comments describe the required method signatures).

[1] :http://stackoverflow.com/questions/7130901/java-stack-implementation

Now when I try to create the main body class as here :

import java.io.*;
public class ExhibitStack<E extends Comparable<E> > {

    E ch;
    public static void main(String[] args) throws IOException {
        ISortableStack<E> s = new ISortableStack(5); // Cannot instatiate ISORTABLESTACK
        ExhibitStack demo = new ExhibitStack();
        // Cannot make reference to a non static type
        while ((demo.ch = (E) System.in.read()) != '\n') {
            if (!s.full()) {
                s.push(demo.ch);
            }
        }
        while (!s.empty()) {
            System.out.print(s.pop());
        }

        System.out.println();
    }
}

It throws error at ISortableStack as:Cannot make a reference to a non static type .
and is not able to instatiate the ISORTABLESTACK

I would like to create the menu driven program using the interface. I am bad at Java GENERICS and collections and have been pretty late on submitting the assignment .
Any help/directions would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:55:39+00:00Added an answer on May 24, 2026 at 10:55 pm
    ISortableStack<E> s = new ISortableStack(5); //Cannot instatiate ISORTABLESTACK
    

    ISortableStack is an interface (it specifies the signatures of methods but not the code that goes in those methods) and thus it can’t itself be instantiated. Instead try using your concrete implementation class:

    ISortableStack<E> s = new SortableStack<E>();
    

    Now, E in SortableStack is a type parameter: it’s a placeholder for some specific class, like String. Instead of specifying E as the user of this class, you need to tell the compiler what E should map to for this instance. It looks like your stack needs to hold characters, so what you really want is:

    ISortableStack<Character> s = new SortableStack<Character>();
    
    char character;
    while ( (character = (char)System.in.read()) != '\n') {
       //...
       s.push(character);
    }
    

    You don’t need ch to be a member of demo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've create the following class in Visual Studio 2010: public class Bat : Form1
Please consider the following code: type TFoo1 = class public procedure DoSomething1; end; TFoo2
Consider following class class test { public: test(int x){ cout<< test \n; } };
I had a huge file for creating this GUI and I have shortened it
How can I have it so Visual Studio doesn't keep re-creating this folder that
Seems I've outdone myself. All the while I was creating this pretty little 'latest
Note: I found this Creating a Word Doc in C#.NET , but that is
nobody loved my first question about this: Creating Entity Framework objects with Unity for
I have referred this for creating error message tool tips, to be displayed continuously
(creating a separate question after comments on this: Javascript redeclared global variable overrides old

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.