Click here to Skip to main content
15,899,475 members
Home / Discussions / Java
   

Java

 
Questionjvm manager Pin
aviparida17-Dec-09 1:59
aviparida17-Dec-09 1:59 
AnswerRe: jvm manager Pin
Fatbuddha 117-Dec-09 3:32
Fatbuddha 117-Dec-09 3:32 
GeneralRe: jvm manager Pin
aviparida17-Dec-09 4:19
aviparida17-Dec-09 4:19 
GeneralRe: jvm manager Pin
David Skelly17-Dec-09 5:26
David Skelly17-Dec-09 5:26 
GeneralRe: jvm manager Pin
aviparida17-Dec-09 5:54
aviparida17-Dec-09 5:54 
GeneralRe: jvm manager Pin
Nagy Vilmos17-Dec-09 22:23
professionalNagy Vilmos17-Dec-09 22:23 
GeneralRe: jvm manager Pin
TorstenH.18-Dec-09 4:33
TorstenH.18-Dec-09 4:33 
QuestionFlapjacks basic java code; Create methods? Pin
Aurelius446611-Dec-09 10:03
Aurelius446611-Dec-09 10:03 
FlapJacks


For this exercise, you will simulate a stack of flapjacks that gets flipped. The stack is really just an array of integers. The flapjacks always start out in regular numbered order. For example, a stack of 10 flapjacks would look like this:

0
1
2
3
4
5
6
7
8
9

Now, you will let your user "flip" all or some of the flapjacks by entering a number, which is the index of the number under which to put the "spatula". For example, if I want to flip the first 6 flapjacks I would call flip of index 5, which would change my flapjack stack to look like this:


5
4
3
2
1
0
6
7
8
9

Now, I can flip this stack at index 7 to get this stack:


7
6
0
1
2
3
4
5
8
9

Your mission consists of two methods. You have to first figure out how to perform the flip operation, then you should create a method that figures out how to flip any mixed-up stack back into original order. You CANNOT just make a new array that is sorted and return that. You must "flip" the stack back to sorted order. You should also not flip randomly until it is sorted. Although chances are that this will eventually work, it will take a very long time for large stacks. Your code should call the flip method and the find method that is given to you.

This is a logic puzzle. The code is easy if you can figure out the algorithm...

The code, except for the two methods for you to make, is in this file: FlapJacks.java (I included the code below)


import javax.swing.*;
public class FlapJacks {

public static void main(String[] args) {
// Put whatever number you want here. This is the
// number of "pancakes" to flip.
final int STACK_HEIGHT = 10;
int[] stack = getArray(STACK_HEIGHT);
printStack(stack);
String quit = "y";
while(quit.equals("y")){
String s = JOptionPane.showInputDialog(null, "Where should I flip?");
int i = Integer.parseInt(s);
stack = flip(stack,i);
printStack(stack);
quit = JOptionPane.showInputDialog(null, "Type y to continue flipping.");
}
stack = sortStack(stack);
printStack(stack);
System.exit(0);
}

// Create an array of len items, where the first number is 0, the second
// number is 1, and so forth.
public static int[] getArray(int len){
int[] answer = new int[len];
for(int i=0; i<len; i++){
answer[i] = i;
}
return answer;
}

// Prints all the elements of an array vertically
public static void printStack(int[] stack){
String toPrint = "";
for(int i = 0; i<stack.length; i++){
toPrint = toPrint + stack[i] + '\n';
}
JOptionPane.showMessageDialog(null,toPrint);
}

// Find the index of a certain number in the array
// Returns either the index where the number was
// found, or -1 if the number wasn't found.
public static int find(int numToFind, int[] array){
for(int i = 0; i<array.length; i++){
if (array[i] == numToFind){
return i;
}
}
return -1;
}

// High is the index under which I put my "spatula" and flip the
// stack over.
public static int[] flip(int[] stack, int high){

}

// Perform a series of flips until the stack is back in order.
public static int[] sortStack(int[] stack){

}
}
AnswerRe: Flapjacks basic java code; Create methods? Pin
LunaticFringe11-Dec-09 11:13
LunaticFringe11-Dec-09 11:13 
AnswerRe: Flapjacks basic java code; Create methods? Pin
Richard MacCutchan12-Dec-09 2:03
mveRichard MacCutchan12-Dec-09 2:03 
AnswerRe: Flapjacks basic java code; Create methods? Pin
427748012-Dec-09 8:59
427748012-Dec-09 8:59 
QuestionEclipse Pulsa Pin
Enobong Adahada9-Dec-09 10:23
Enobong Adahada9-Dec-09 10:23 
AnswerRe: Eclipse Pulsa Pin
Richard MacCutchan9-Dec-09 23:52
mveRichard MacCutchan9-Dec-09 23:52 
QuestionAny one explain plz Pin
kirancgi6-Dec-09 23:07
kirancgi6-Dec-09 23:07 
AnswerRe: Any one explain plz Pin
Nagy Vilmos7-Dec-09 1:12
professionalNagy Vilmos7-Dec-09 1:12 
GeneralRe: Any one explain plz Pin
Luc Pattyn9-Dec-09 13:36
sitebuilderLuc Pattyn9-Dec-09 13:36 
GeneralRe: Any one explain plz Pin
LunaticFringe9-Dec-09 13:47
LunaticFringe9-Dec-09 13:47 
GeneralRe: Any one explain plz Pin
Nagy Vilmos9-Dec-09 21:34
professionalNagy Vilmos9-Dec-09 21:34 
JokeRe: Any one explain plz Pin
Richard MacCutchan9-Dec-09 23:50
mveRichard MacCutchan9-Dec-09 23:50 
GeneralRe: Any one explain plz Pin
LunaticFringe10-Dec-09 2:43
LunaticFringe10-Dec-09 2:43 
Questionscreen shairing application Pin
rajivbirari3-Dec-09 21:52
rajivbirari3-Dec-09 21:52 
AnswerRe: screen shairing application Pin
Nagy Vilmos3-Dec-09 23:54
professionalNagy Vilmos3-Dec-09 23:54 
AnswerRe: screen shairing application Pin
Arun Reginald Zaheeruddin1-Mar-10 0:05
Arun Reginald Zaheeruddin1-Mar-10 0:05 
QuestionGWT https Pin
sangeethanarayan3-Dec-09 17:36
sangeethanarayan3-Dec-09 17:36 
AnswerRe: GWT https Pin
Nagy Vilmos4-Dec-09 0:24
professionalNagy Vilmos4-Dec-09 0:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.