Results 1 to 7 of 7
Thread: stacks
- 02-25-2011, 02:52 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
stacks
i have a working program that prints ints from a list in reverse using a stack, but I need this to work if i was working with floating point in my list as well. I still have to use a stack though. Here is my code so far:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Stack;
class Numlistclass
{
final int MAXCOUNT = 50;
private int count;
private int[] nums = new int[MAXCOUNT];
public Numlistclass()
{
int i;
count = 0;
for (i = 0; i < MAXCOUNT; i++) nums[i] = 0;
} // Numlistclass
public void addnum(int num)
{
if (count == MAXCOUNT)
System.out.println("List is full -- unable to store: "+num);
else
nums[count++] = num;
} // addnum
public void printnums(PrintWriter pw)
{
int i;
pw.println("Numbers in list:");
for (i = 0; i < count; i++) pw.println(nums[i]);
} // printnums
public void reversestack(PrintWriter pw)
{
int i;
Stack lifo = new Stack();
for (i = 0; i < count; i++)
{
lifo.push (nums[i]);
}
pw.println("Numbers in reverse: ");
while ( !lifo.empty() )
{
pw.println (lifo.pop());
}
}
} // class Numlistclass
public class prog05
{
public static void main(String args[]) throws IOException
{
int num;
String line;
Numlistclass numlist = new Numlistclass();
BufferedReader br = new BufferedReader(new FileReader("prog5i.dat"));
PrintWriter pw = new PrintWriter(new FileWriter("prog5.out"));
// store numbers in list
while ((line = br.readLine()) != null)
{
num = Integer.parseInt(line);// convert line to numeric value
numlist.addnum(num);
}
// write output
numlist.printnums(pw);
numlist.reversestack(pw);
// finalize
br.close();
pw.close();
}
} // class prog05
- 02-25-2011, 03:20 AM #2
Are you asking how can I put ints AND floats in the Stack, then you can as your Stack currently can store any Object.
Or are you asking how can you make the Stack hold ints OR floats then you will need to make a decision.
Java Code:if blah { do int stack } else { do float stack }
- 02-25-2011, 03:46 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
i need the second one i think, how do i do that?
Last edited by nevets93; 02-25-2011 at 03:50 AM.
- 02-25-2011, 03:59 AM #4
Why not just assume that all inputs are floats. So if user enters 1 it really is 1.0. This means you cannot use Integer.parseInt. You will need to use the float equivalent.
- 02-25-2011, 04:01 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
to make things more clear, here were my instructions:
Your task is to write a program (Prog5.java) which utilizes a stack to reverse
the order of a sequence of input integers. You will need to prepare a data file
(prog5i.dat) which contains a dozen or so integers (it might be good to arrange
them in ascending order). The program should print the original sequence of
numbers, and then print the numbers in reverse order (the output should be
placed in a file named prog5.out).
After the program is working for input integers, prepare a second data file
(prog5f.dat) that contains floating point numbers (numbers with a decimal
point). Repeat the same task as above, using the second file and printing the
floating point outputs. (Add the new task into the same program -- Prog5.java).
Part of the challenge in this program is to make the second task as easy as
possible by making the first solution as generic as possible. You should
implement your stack as a Java class.
- 02-25-2011, 04:30 AM #6
A few points:
1. for (i = 0; i < MAXCOUNT; i++) nums[i] = 0;
When you create an array it will be filled with the default value. For int arrays the default is zero. Therefore the above is unnecessary.
2. Why are you using an array at all. You are supposed to be using a Stack. Add the numbers directly to your Stack and not the array.
3. I would pass the user input to the addNum method as a String. Then inside that method use an if statement to decide if to parse input as an int or float.
- 02-25-2011, 04:32 AM #7
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Stuck with stacks
By Yakg in forum New To JavaReplies: 2Last Post: 01-30-2011, 09:34 PM -
Help with stacks
By kMel90 in forum New To JavaReplies: 3Last Post: 12-05-2010, 02:02 AM -
Help with stacks
By Srcee in forum New To JavaReplies: 5Last Post: 11-01-2009, 12:23 PM -
Stacks
By Zosden in forum Advanced JavaReplies: 15Last Post: 05-05-2008, 09:16 AM -
Using Stacks
By ravian in forum New To JavaReplies: 7Last Post: 11-28-2007, 10:53 AM
Bookmarks