Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stacks and Queues

Status
Not open for further replies.

Rhoon

Programmer
Mar 23, 2001
55
US
Hey all,

I'm having problems with trying to understand how this will work. I'm trying to make my own stack implementation (yes, homework assignment) but I'm not sure how my two constructors will work. One constructor takes an Int and the other a Char. When I'm push()'ing something onto the stack, what type of paramenter should I use? Int and type it to a char? or vice versa?

public MyStack(int size) {
private int stack = new int [size];

} // End First Constructor

public MyStack(int size, char c) {
private char stack = new char [size];


} // End Second Constructor


Here's my two constructs, should I make two methods called push and pop() , one for each type (int and char) ?? Any help would be greatly appreciated!

Thanks in advance
Andrew
 
you should have two push methods to add something to the stack i.e.

public void push( int a )
and
public void push( char a )

but you will have to decide between either int or char for the pop method as you can't overload methods based on their return type

 
Hey all,

I finally went with the following code. I'm getting numberformatexceptions out the rear.. was hoping someone could help me with this bit of code. Any tips/tricks/advice is greatly appreciated. Thanks in Advance.

Andrew

/* Main Class */

import java.io.*;
import java.lang.*;
import java.util.*;

public class program2 {



public static void main (String args[]) throws IOException, NumberFormatException {

BufferedReader keyboard = new BufferedReader ( new InputStreamReader (System.in));
System.out.print("How many Processes would you like to do? ");
int procnum = Integer.parseInt (keyboard.readLine());
System.out.println(procnum);
MyQueue que = new MyQueue(procnum);
System.out.println("\n" + "a: Add two VLN's. r: Reverse a String.");

/* Reads in Processes to be performed */
int i = 1;
while (i <= procnum) {
System.out.print(&quot;\n&quot; + &quot;Enter Your Process: &quot;);
que.enqueue( ((char) keyboard.read()) );
i++;
}

// End Process Reading



while (que.hasElements()) {
switch( (char)que.dequeue() ) {

case 'a': {
System.out.print(&quot;Enter the amount of numbers in the first VLN: &quot;);
int am1 = (int)(Integer.parseInt(keyboard.readLine()));
System.out.print(&quot;\n&quot; + &quot;Enter the amount of numbers in the second VLN: &quot;);
int am2 = (int) (Integer.parseInt(keyboard.readLine()));

MyStack num1 = new MyStack(am1);
MyStack num2 = new MyStack(am2);

for (int j = 0; j < am1; j++ ) {
System.out.print(&quot;\n&quot; + &quot;Please enter a numbers for Number 1: &quot;);
num1.push(new Integer(keyboard.readLine() ));
} // End For Loop

for (int j = 0; j < am2; j++ ) {
System.out.print(&quot;\n&quot; + &quot;Please enter a numbers for Number 2: &quot;);
num2.push( new Integer(keyboard.readLine() ));
} // End For Loop
System.out.println(&quot;Test 1&quot;);
MyStack resultant = new MyStack(am1+am2);
int result;
System.out.println(&quot;Test 2&quot;);
while (num1.hasElements() || num2.hasElements() ) {
result = (( (Integer)resultant.pop()).shortValue() );
System.out.println(&quot;test 3&quot;);
result = (( (Integer)num1.pop()).shortValue() + ((Integer)num2.pop()).shortValue() );
resultant.push(new Integer (result % 10));
System.out.println(&quot;Test 4&quot;);
resultant.push(new Integer (result / 10));
} // End While
System.out.println(&quot;Test 5&quot;);
while (resultant.hasElements() ) {
System.out.print(&quot;The sum of your two numbers is: &quot;);
System.out.print(((Integer)resultant.pop()).shortValue() );

} // End While
break;
} // End Case A

case 'r' : {

System.out.print(&quot;How many Letters will you enter?&quot;);
int length = Integer.parseInt(keyboard.readLine());
MyStack reverse = new MyStack(length);
for ( i = 0; i < length; i++ ) {
System.out.print(&quot;\n&quot; + &quot;Enter the next letter in your word: &quot;);
reverse.push( new Character ((char)keyboard.read()) );
} // End For Loop
while( reverse.hasElements() ) {
System.out.print(((Character)reverse.pop()).charValue() );
} // End While

break;
} // End Case R

} // End Switch

} //End While

} // End Main

} // End Program 2



/* STack Class */

import java.lang.*;

public class MyStack {

private Object [] stack;
private int index= 0;

public MyStack(int size) {
stack = new Object [size];

} // End First Constructor


public void push (Object item) {
stack[index] = item;
index++;
} // End push
public Object pop () {
Object temp = stack[index];
index--;
return temp;

} // End Pop

public boolean hasElements() {
if (index == 0) {return false;}
else {return true;}

} // End hasElements

} // End MyStack

/* Que Class */

public class MyQueue {

private char [] queue;
private int top = 0;
private int index =0;


public MyQueue ( int size ) {
queue = new char [size];

} // End Constructor

public void enqueue(char c) {
queue[index] = c;
index = index + 1;
} // End enqueue

public char dequeue() {
char temp = queue[top];
top++;
return temp;

} // End Dequeue

public boolean hasElements() {
if (index <= top) {return false;}
else {return true;}

} // End hasElements




} // End Queue
 
You are most likely getting the NumberFormatExceptions from the user input. If someone doesn't type in a number, and you try to use Integer.parseInt( ), BANG, there is your problem. You should enclose the portion where you are taking input in a try/catch block:

try{
int a = Integer.parseInt(in.readLine());
}
catch(NumberFormatException nfe){
System.out.println(&quot;Hey, type a number dummy!&quot;);
//or some error handling code here
}
------
KJR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top