Solved

Looking for help in creating a class to handle a stack

Posted on 2007-11-14
8
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

707 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