Solved

Looking for help in creating a class to handle a stack

Posted on 2007-11-14
8
167 Views
Last Modified: 2013-11-23
Hi Fellow EE'rs

Could someone please assist me with creating code to handle a stack? What would be the required driver programs needed for this?

Many Thanks
Steve
0
Comment
Question by:Stephen Manderson
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20286511
0
 
LVL 5

Expert Comment

by:deeppra
ID: 20286607
Its already there in the JavaSE so u dont need to recreate it just use that
0
 

Expert Comment

by:huzefaq
ID: 20286627
0
 
LVL 19

Author Comment

by:Stephen Manderson
ID: 20286686
Thanks Objects

Not at my own pc but does the following look ok to setup a stack and add from keyboard input?

public class CChar {

      private char CharVal;


      public CChar() {
            Stack stack = new Stack();
            CharVal = ' ';
      }


      public void setCharValFromKeyboard() {
            try {
                  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                  System.out.print("Character to add to stack: ");
                  CharVal = (char) in.read();
                        stack.push(CharVal);
                  System.out.println();

            }
            catch (Exception e) {
            }

      }
   
}
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 5

Expert Comment

by:deeppra
ID: 20286731
Yea its okay
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 20286744
close , try more like this:

public class CChar {

       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      Stack stack = new Stack();

      public CChar() {
      }


      public void setCharValFromKeyboard() {
            try {
                  System.out.print("Character to add to stack: ");
                  char charVal = (char) in.read();
                  stack.push(charVal);
                  System.out.println();

            }
            catch (Exception e) {
            }

      }
   
}
0
 
LVL 19

Author Comment

by:Stephen Manderson
ID: 20286829
Thanks for that

Would it just be a case of the following to remove stack values and to print a stack contents?

public void popStackValue() {
       try {
             System.out.println("The poped value is " + stack.pop());
             System.out.println("The new stack size is" + stack.size());
             }
}

public void printStack() {
      try {
            System.out.println("The contents of  the Stack are" + stack);
            }
}

Would that be the correct way to print out the stack contents?

Many Thanks
Steve
0
 
LVL 92

Expert Comment

by:objects
ID: 20286880
yes, that would be a good start
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now