?
Solved

Looking for help in creating a class to handle a stack

Posted on 2007-11-14
8
Medium Priority
?
175 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

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