Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

where I can get some info on using stacks?

  Asked By: Jesse    Date: Dec 31    Category: Java    Views: 1009

Does anyone know where I
can get some info on using stacks? Is there an easy way to write and
munipilate these digits? I have nothing in my text about any of
this, nor has she taught us how to use the stack, push or pop
functions. As usual, I am clueless, and would love to learn, but I
need to find this info!
1. Prompt the user to enter a decimal number, read the number N..

2. Prompt the user to enter a number base B, (positive # between 2-
16), to which the first input number will be converted. Read in
this number.

3. Use the Repeated Division Algorithm to convert the input decimal
# to the base specified.

4. Write out with appropriate labels the base 10 number and it's
equivalent base, B number.

5. Repeat Steps 1-4 until the user enters a negative decimal
number. Note: In implementing this algorithm you get the individual
digits of the converted number stored on a stack. You will not
create a "true" number of base B, but will simply build a string of
the converted base B number ONE DIGIT AT A TIME, as each successive
digit is popped off the stack, append it to a stringBuffer instance
variable until the stack is empty.

Specific Requirements:

1. You must use the Java Stack collection class to store the
individual digits.

2. You must build the converted base number as a stringbuffer or
string object.

Your program should be robust enough to handle invalid input. For
the basic version of this program, the only valid numbers that can
be input for the number base are digits 2-16.



9 Answers Found

Answer #1    Answered By: Umaiza Hashmi     Answered On: Dec 31

It is strange that your instructor gave you the
intermedia problem while she only taught you entry
level lessons. I think the best place to start is
your own API, then move on to
search the Java tutorial or a quicky

Answer #2    Answered By: Barachias Levi     Answered On: Dec 31

Well yes it is strange, but it is reality. She has no clue as to
what she has or has not taught us, and she has never even come close
to explaining any of these fundamentals. I have purchased 6 differnt
books on java, looked extensively on line, and cannot find  anything
of much value to understand or accomplish this assingnment. I will
check out these links, and I can only hope there is something that
will help.

Answer #3    Answered By: Naomi Lee     Answered On: Dec 31

I hope in collection of Java books, two of them are
Java CookBook, Java in a Nutshell of O'Reilly, and the
other is Java Programming Language 3rd. Ed of Sun. I
only pass to you the wisdom that I learned from my
professor many moons ago.

Books give you the vocabulary of the particular
language. Algorithm provides the meanning of the

Your instructor might pushing your creativity and what
I see in your program are GUI, String I/O, Stream I/O,
Stack Collection, and Trigonometry level math.
Depended on school, Stack Collection and Hashtable
usually belong in the intermediate level of Java

Sorry, I do not have any similar example. But if you
dissect the program out, you will prevail.

Answer #4    Answered By: Bathilda Schmidt     Answered On: Dec 31

I have bought : beginning Java 2 (Ivor
Horton) Java Programming Fundamentals(Seefeld) Java 2 a beginners
guide(Schildt) Java Data Objects(Ross) and a 4 dollar book Java 2
certification by (Sun). Although none of these books contain any
real examples. Show me something similar, and I can figure it out,
but when I have no starting point to even reference, it really
sucks. It is like trying to find  your way through the woods in the
dark! I have one month left. How can anyone learn  this kind of
programming, if there are no examples, nothing in the text  book, and
nothing in class? I would love to get through this and chalk it up
to a bad experience. If anyone can be of help, or send me in the
right direction, please contact me. I will see if I cannot locate
some of the books you mentioned. I just don't feel with the kind of
money I pay to attend this school that it should be done this way.
Everyone else is in the same boat.

Answer #5    Answered By: Joyce Edwards     Answered On: Dec 31

We all have made mistakes once a while. I have not
visit any book store lately, but try to locate Java
Examples In a NutShell 2nd Ed(Flanagan) and Java How
to Program 4th Ed(Deitel & Deitel). Each have its own
pro and con. Both books will provide you plenty of
code examples, the con side Flanagan's is written in
the 1.3 version and required you have some OOP
background while Deitels' book explanation is rather
strange to follow. If something hard to understand
turn on your API for explanation.

Now for the Algorithm, could you figure out yet or at
least jog it down in human language and post it here?
I am sure some of us have time will help you out
because your situation is very strange.

Answer #6    Answered By: Adel Fischer     Answered On: Dec 31

Beleive it or not, I have put together a program. I have worked on
it since before I posted it! It runs, and compiles. The thing is, I
am having trouble converting from numbers to letters. I am going to
post it here, with the hope someone can put a band aid on it for me,
or tell me how to take care of it. This has been a night mare. Since
I am in school, I cant afford to buy any more books! (I am poor this
time of year!) so I hung out in the bookstores and got segments of
code from differnt books, and somehow I come up with this! I have
commented out the troubled area, so if anyone can help me figure out
what the deal is, I would be greatful. Maybe I am off, but I beleive
this is right.

import java.util.*; //
import java.lang.*; //
import javax.swing.*; //

public class RemainderStack {

public static void main (String[] args) { // Begin main

String choice = "";
int quotient;
Integer Remainder = null; // Create integer object
Stack remainderStack = new stack  (); // Create a new,
empty stack

try { // Begin try

while (!(choice.equalsIgnoreCase("N"))) { // Begin while

String remainderString = "";

String N = JOptionPane.showInputDialog(
"Enter a decimal number. \n" + "Enter a negative number
to exit. \n");

if (Integer.parseInt(N) < 0)

String B = JOptionPane.showInputDialog(
"Enter a number base (between 2 - 16)");

if (Integer.parseInt(B) < 2 || Integer.parseInt(B) > 16)
{ // Begin if

String message = "Invalid base number. \n" + "Restart

JOptionPane.showMessageDialog(null, message, "Error!",


} // End if

int decimalNumber = Integer.parseInt(N);
int baseNumber = Integer.parseInt(B);

quotient = (int) decimalNumber / baseNumber;

Remainder = new Integer (decimalNumber % baseNumber);


while (quotient != 0) { // Begin while loop

Remainder = new Integer (quotient % baseNumber);


quotient = (int) quotient / baseNumber;

} // End while loop

while (! remainderStack.empty()) { // Begin while loop

/// if (Remainder = 10) Remainder = 065;

// if (Remainder = 11) Remainder = "B";

// if (Remainder = 12) Remainder = "C";

// if (Remainder = 13) Remainder = "D";

// if (Remainder = 14) Remainder = "E";

// if (Remainder = 15) Remainder = "F";

remainderString += remainderStack.pop();

} // End while

String message = "Base 10 number: " + decimalNumber + "\n"
+ "Number in base " + baseNumber + " is: " +
+ "\n Continue (Y/N)?\n";

choice = JOptionPane.showInputDialog(null, message, "Number
Base Conversion",

} // End while loop

} // End try statement

catch (NullPointerException e) { // Catch if user presses


} // End catch statement

catch(NumberFormatException e) { // Begin catch

String message = "Invalid number. \n" + "Restart program.";

JOptionPane.showMessageDialog(null, message, "Error!",


} // End catch statement

while (!(choice.equalsIgnoreCase("Y"))) { // Begin while loop


} // End while loop

} // End main

} // End class

Answer #7    Answered By: Teresa Rogers     Answered On: Dec 31

Perhaps i have replied for the other questions of you.
simple thing is to use double in stead of integer.

Answer #8    Answered By: Tammy Sanders     Answered On: Dec 31

You need to reverse parseInt() because it converts
String to an int such as valueOf(). Let's see how it
plays in the program...

Your programming style like C++, but it is OK for now.
You should be happy the darn thing compiled.

Answer #9    Answered By: Hilma Miller     Answered On: Dec 31

if you want to compare something of equality in an if statement use "==":

if (Remainder == 10) Remainder = "A";

Didn't find what you were looking for? Find more on where I can get some info on using stacks? Or get search suggestion and latest updates.