Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 16th, 2007, 04:42 AM
tdubbz02 tdubbz02 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 1 tdubbz02 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 19 sec
Reputation Power: 0
Help with Printing a Stack

Hello, I am relatively new to Java and I am writing a program that uses a stack to print prime factors of a positive integer. I need some help getting the program to output the data in reverse order using the reversePrint method. For example the program output for 500 reads 5 5 5 2 2 and it needs to read 2 2 5 5 5.

I am a bit confused as to where to place this method (which class) and its correct structure.
I have 2 classes and will list them below, the first one is where I believe the method should go, and you will see a couple of my attempts at the bottom of this class, commented out of course. (As you see I'm totally lost!)

Any help or information would be greatly appreciated, Thanks

Ex8 class:

public class Ex8
{
static Scanner console = new Scanner(System.in);

public static void main(String[] args)
{
int num;
int temp;
int factor;
Integer intObject;

LinkedStackClass<Integer> stack = new LinkedStackClass<Integer>();

System.out.print("Enter a positive integer greater than 1: ");
num = console.nextInt();
System.out.println();

while (num <= 1)
{
System.out.print("\nYou must enter a positive "
+ "integer greater than 1: ");
num = console.nextInt();
System.out.println();
}

System.out.print("The prime factorization of " + num + ": ");

if (isPrime(num))
{
System.out.println(num);
return;
}

temp = num;

factor = 2;

while (temp > 1)
if (temp % 2 == 0)
{
intObject = 2;
stack.push(intObject);
temp = temp / 2;
}
else
break;

factor = 3;

while (temp > 1 && (factor <= num / 2))
{
if (!isPrime(factor))
factor += 2;
else
if (temp % factor == 0)
{
intObject = factor;
stack.push(intObject);

temp = temp / factor;
}
else
factor += 2;
}

while (!stack.isEmptyStack())
{
System.out.print(stack.peek() + " ");
stack.pop();
}

System.out.println();

return ;
}



public static boolean isPrime(int num)
{
boolean isPrime = true;

for (int i = 3; i <= Math.sqrt(num); i += 2)

if (num % i == 0)
{
isPrime = false;

break;
}

if (( num % 2 !=0 && isPrime && num > 2) || num == 2)
{
return true;
}

else
{
return false;
}
}




/*
public void reversePrint()
{
LinkedStackClass<Integer> current;

current = stackTop;

while (current != null)
{
System.out.print(current.info + " ");
current = current.link;
}
}

public void reversePrint()
{
LinkedStackClass<Integer> current;

current = temp;

while (current != null)
{
System.out.print(current.info + " ");
current = current.temp;
}
}
*/

}




LinkedStackClass:

public class LinkedStackClass<T> implements StackADT<T>
{

private class StackNode<T>
{
public T info;
public StackNode<T> link;

public StackNode()
{
info = null;
link = null;
}

public StackNode(T elem, StackNode<T> ptr)
{
info = elem;
link = ptr;
}

public String toString()
{
return info.toString();
}
}


private StackNode<T> stackTop;


public LinkedStackClass()
{
stackTop = null;
}


public void initializeStack()
{
stackTop = null;
}


public boolean isEmptyStack()
{
return (stackTop == null);
}


public boolean isFullStack()
{
return false;
}


public void push(T newElement)
{
StackNode<T> newNode;

newNode = new StackNode<T>(newElement, stackTop);

stackTop = newNode;
}


public T peek() throws StackUnderflowException
{
if (stackTop == null)
throw new StackUnderflowException();

return stackTop.info;
}


public void pop() throws StackUnderflowException
{
if (stackTop == null)
throw new StackUnderflowException();

stackTop = stackTop.link;
}


}

Reply With Quote
  #2  
Old March 22nd, 2007, 03:44 AM
daniel_g daniel_g is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 60 daniel_g User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 3 m 50 sec
Reputation Power: 3
Not gonna bother to read and try to understand what your code is doing, but basically the elements 2, 2, 5, 5, 5 where pushed into the stack, so when you pop the stack, the first number to come out is going to be the last one in, therefore you get 5 5 5 2 2, right?

If so, there are a couple of ways in which you can print the output in the correct order:
1- Push the numbers into a new stack, then print.
2- Use a StringBuffer and it's reverse method:
http://java.sun.com/j2se/1.4.2/docs....html#reverse()

So far you have:
Code:
while (!stack.isEmptyStack()){
    System.out.print(stack.peek() + " ");
    stack.pop();
}

Instead you might want something like:
Code:
StringBuffer buffer = new StringBuffer();
while (!stack.isEmptyStack()){
    buffer.append(stack.pop() + " ") //can't remember right now if this is possible
}
System.out.println(buffer.reverse()); //or something like this.

// It's almost 4 AM so can't think clearly, but I hope you get the idea.

Last edited by daniel_g : March 22nd, 2007 at 08:38 PM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Help with Printing a Stack


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT