Need a little help with the basics
Hi all...I have this program here
Code java:
import javax.swing.JOptionPane;
public class BubbleSort {
public static void main(String[] args) {
double[] list = new double[10];
bubbleSort(list);
JOptionPane.showMessageDialog(null, "Thanks for playing!");
}
//The Method for sorting the numbers
public static void bubbleSort(double[] list) {
//Declare variables
double currentMin;
int currentMinIndex, i, j;
//Store user input
for (i = 0; i < list.length; i++)
list[i] = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter next numbers."));
//Print the numbers entered
JOptionPane.showMessageDialog(null, "The numbers you entered were: \n" + list[0] + " " + list[1] + " " + list[2] + " " +
list[3] + " " + list[4] + " " + list[5] + " " + list[6] + " " + list[7] + " " + list[8] + " " + list[9]);
JOptionPane.showMessageDialog(null, "The numbers will now swap till they are sorted from low to high: ");
for (i = 0; i < list.length - 1; i++) {
//Find the minimum in the list
currentMin = list[i];
currentMinIndex = i;
for (j = i + 1; j < list.length; j++) {
if (currentMin > list[j]) {
currentMin = list[j];
currentMinIndex = j;
}
}
//Swap list[i] with list[currentMinIndex] if necessary
if (currentMinIndex != i) {
list[currentMinIndex] = list[i];
list[i] = currentMin;
//Print the sorted numbers
JOptionPane.showMessageDialog(null, list[0] + " " + list[1] + " " + list[2] + " " + list[3] + " " + list[4]
+ " " + list[5] + " " + list[6] + " " + list[7] + " " + list[8] + " " + list[9]);
}
}
}
}
While my teacher gave this assignment an A, he wants me to get rid of this type of code...
Code java:
JOptionPane.showMessageDialog(null, "The numbers you entered were: \n" + list[0] + " " + list[1] + " " + list[2] + " " +
list[3] + " " + list[4] + " " + list[5] + " " + list[6] + " " + list[7] + " " + list[8] + " " + list[9]);
He hinted at the += style using a string. Does anyone know what he's talking about?
Neil
Re: Need a little help with the basics
He most likely meant to use a loop to set up the string:
Code java:
String result = "The numbers you......";
for (int i = 0; i < list.length; i++) {
result += numbers.....
}
Re: Need a little help with the basics
I'm not quite sure what you mean. Doing it that way would produce the numbers one at a time for each loop wouldn't it? He wants the numbers entered to be displayed in one window.
Of coarse I could have completely misunderstood you as well. For some reason your code was not displayed right.
Thanks for your help either way! I love this place.
Re: Need a little help with the basics
The second parameter of the method showMessageDialog() takes a String. Therefore, you can send it a String, or a String variable:
So, you would need to setup the String variable. What your professor was hinting at, (or at least the way I would do it) was to loop through your array of doubles, and enter them into a string. Think of it this way:
Code java:
String result = "The numbers you entered were: ";
String = String + list[0] + " "; // The empty space is for separation between your different elements in the array
// if the value of list[0] was 13.4, then your string would now be "The numbers you entered were: 13.4 "
// You can also do it this way:
String += list[0] + " ";
// And ultimately, loop though it so you don't have extra lines of code:
String result = "The numbers you......";
for (int i = 0; i < list.length; i++) {
result += list[i];
}
And use the string you created:
Hope this helps
Re: Need a little help with the basics
Well I can't thank you enough for your help. Unfortunately I'm still missing something.
Here is your suggestions implemented in my code...
Code java:
import javax.swing.JOptionPane;
public class BubbleSort {
public static void main(String[] args) {
double[] list = new double[10];
bubbleSort(list);
JOptionPane.showMessageDialog(null, "Thanks for playing!");
}
//The Method for sorting the numbers
public static void bubbleSort(double[] list) {
//Declare variables
double currentMin;
int currentMinIndex, i, j;
//Store user input
for (i = 0; i < list.length; i++)
list[i] = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter next numbers."));
//Print the numbers entered
String result = "The numbers you entered were: ";
for (i = 0; i < list.length; i++) {
result += list[i];
}
JOptionPane.showMessageDialog(null, "The numbers will now swap till they are sorted from low to high: ");
for (i = 0; i < list.length - 1; i++) {
//Find the minimum in the list
currentMin = list[i];
currentMinIndex = i;
for (j = i + 1; j < list.length; j++) {
if (currentMin > list[j]) {
currentMin = list[j];
currentMinIndex = j;
}
}
//Swap list[i] with list[currentMinIndex] if necessary
if (currentMinIndex != i) {
list[currentMinIndex] = list[i];
list[i] = currentMin;
//Print the sorted numbers
JOptionPane.showMessageDialog(null, list[0] + " " + list[1] + " " + list[2] + " " + list[3] + " " + list[4]
+ " " + list[5] + " " + list[6] + " " + list[7] + " " + list[8] + " " + list[9]);
}
}
}
}
As you can see I'm missing something?
Re: Need a little help with the basics
Not sure exactly what needs further explanation, but for instance, you are creating the String variable, but are not sending it to the method to display it. Also, remember to add a space in your String between each number.
Re: Need a little help with the basics
Quote:
Originally Posted by
vanDarg
Not sure exactly what needs further explanation, but for instance, you are creating the String variable, but are not sending it to the method to display it. Also, remember to add a space in your String between each number.
Sorry for not following you.
I guess I a bit confused by this...
Code java:
String result = "The numbers you entered were: ";
This is not using JOptionPane for some reason?
I'm just not seeing between the lines.
Maybe you could apply your suggestion to my original code so I could see the whole picture? Like I said, I already have the grade for the assignment, and am just trying to approach things in a more efficient way.
Neil
Re: Need a little help with the basics
No worries! You DO want to use the String variable with JOptionPane.
I was just playing around with it in eclipse, see if this bit helps:
Code java:
String result = "This is my text, stored in a String variable";
JOptionPane.showMessageDialog(null, "This is my text...");
JOptionPane.showMessageDialog(null, result);
Sending a String variable to the method, will display the contents of the variable in the "pane" that is displayed. In this instance, sending the String variable 'result' to the method showMessageDialog would display (in a message box, of course):
Quote:
This is my text, stored in a String variable
Re: Need a little help with the basics
I just feel like I'm spinning my wheels here.
Can you apply your knowledge to this code so I can "see" the whole thing? I think I'm just missing how the code looks as a whole. Once I see it in practice I can trace the logic.
Code java:
import javax.swing.JOptionPane;
public class SelectionSort {
public static void main(String[] args) {
double[] list = new double[10];
selectionSort(list);
}
//The method for sorting the numbers
public static void selectionSort(double[] list) {
for (int i = 0; i < list.length - 1; i++) {
//Find the minimum int the list[..list.length - 1]
double currentMin = list[i];
int currentMinIndex = i;
for (int j = i + 1; j < list.length; j++) {
if (currentMin > list[j]) {
currentMin = list[j];
currentMinIndex = j;
}
}
//Swap list[i] with list[currentMinIndex] if nessasary
if (currentMinIndex != i) {
list[currentMinIndex] = list[i];
list[i] = currentMin;
}
}
}
}
How long have you been doing this, by the way?
Re: Need a little help with the basics
A quick example to show what I was getting at, the code I added is between the commented lines
Code java:
import javax.swing.JOptionPane;
public class SelectionSort {
public static void main(String[] args) {
double[] list = new double[10];
selectionSort(list);
}
//The method for sorting the numbers
public static void selectionSort(double[] list) {
for (int i = 0; i < list.length - 1; i++) {
//Find the minimum int the list[..list.length - 1]
double currentMin = list[i];
int currentMinIndex = i;
//--------------------------------------------------------------------------------------
//Store user input
for (i = 0; i < list.length; i++)
list[i] = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter next numbers."));
String result = "Numbers are: ";
for (i = 0; i < list.length; i++) {
result += list[i] + " ";
}
JOptionPane.showMessageDialog(null, result);
//-----------------------------------------------------------------------------------------
for (int j = i + 1; j < list.length; j++) {
if (currentMin > list[j]) {
currentMin = list[j];
currentMinIndex = j;
}
}
//Swap list[i] with list[currentMinIndex] if nessasary
if (currentMinIndex != i) {
list[currentMinIndex] = list[i];
list[i] = currentMin;
}
}
}
}
What I was saying should hopefully make some sense now. I have been programming for roughly 10 years, on and off. Currently a CS student.
Re: Need a little help with the basics
Quote:
What I was saying should hopefully make some sense now. I have been programming for roughly 10 years, on and off. Currently a CS student.
It really shows. I can't thank you enough!
I'm going to mess around with this logic for a while and post back.