Question
Asked by:
$25.00 Bin Sort for 4 digit numbers....I'm an idiot!
- From Computer-Science: General-CS , Computer-Science: Programming-Methods
- Closed, but you can still post tutorials
- Due on Dec. 11, 2008
- Asked on Dec 08, 2008 at 9:57:21PM
Q:I need a Java program that does the following:
Repeatedly ask the user to enter integer data values from 0 to 9999, until the user hits the ENTER key alone to stop the inputting.
As each data value is inputted, store it onto a stack. (This stack will serve as the main bin for a Bin Sort procedure.)
Next, output the user's unsorted data, by outputting the stack's contents.
Next, perform a Bin Sort on the user's data. This will arrange the data items from smallest to largest, in the main bin.
Finally, output the main bin's contents (which now consists of the user's data in order).
TEMPLATE:
public class HW5
{
public static StackV mbin = new StackV();
public static StackV[] sbins = new StackV[10];
public static void main(String args[])
{
for (int i=0; i<=9; i++)
{
sbins[i] = new StackV();
}
*HERE: ask the user to input values from 0 to 9999 and end the inputting by hitting the ENTER key alone. Each inputted data value should be stored in (pushed onto) the main bin stack (i.e., pushed onto mbin).
*HERE: now that the user's inputting is finished, output the contents of the main bin, e.g. by using its .toString method.
*HERE: sort the user's data, by using the rules of Bin Sort. You can do this more easily by relying on the MtoS and StoM functions, which are outlined below.
*HERE: since your Bin Sort is now completely finished, you should now output the main bin again, e.g. by using its .toString method. This will display the user's data in sorted order (because the main bin is now sorted).
}
public static void MtoS(int p)
{
*HERE: FINISH THE MTOS SUBROUTINE, which transfers the main bin's contents to the sort bins, according to the rules of Bin Sort.
NOTE: how you define position p is entirely UP TO YOU,
and may vary depending on what technique you use to pick out
the digit at position p inside the user's current data item.
Example: you can define p=0 to represent the leftmost digit
of the user's current data item, p=1 for the digit after it,
p=2 for the next digit, and p=3 for the rightmost digit of
the user's current data item. Or, alternatively, you can define p=0 to represent the rightmost digit, p=3 for the leftmost digit.
}
public static void StoM()
{
*HERE: FINISH THE STOM SUBROUTINE, which transfers the sort bins'contents to the main bin, according to the rules of Bin Sort.
In particular... transfer sort bin #9's contents to the main bin(one item at a time), then transfer sort bin #8's contents to the main bin (one item at a time), etc., etc.,
and finally transfer sort bin #0's contents to the main bin.
}
} //end class
To which Sox4lyfe said: Its just a stack that uses a vector, as apposed to an array. Generally we use the java.util, but we had coded our own at some point: public class StackV { private Vector btsvec; public StackV() { this.btsvec = new Vector(); } public void push(Object obj) { this.btsvec.addElement(obj); } public Object pop() { Object obj = this.btsvec.remove( this.btsvec.size() - 1 ); return obj; } public int size() { return this.btsvec.size(); } public boolean isEmpty() { return this.btsvec.isEmpty(); } public String toString() { return this.btsvec.toString(); } } //end class Thank you.



