This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Modern JavaScript for the Impatient and have Cay Horstmann on-line!
See this thread for details.
Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Bubble sort problem

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am losing a value in the array with my bubble sorting and the sorting does nothing.
Here is the Items Class

public class Items
{
// * A String instance variable to hold the item name
private String itemName;
// * A double instance variable to hold the price
private double itemPrice;
// * A constructor that takes a String and double to initialize the instance variables
// public Items ()
// {
// //used for sorting
// }
public Items (String itemName, double itemPrice)
{
this.itemPrice = itemPrice;
this.itemName = itemName;
}
// * A get and set method for each instance variable
public String getName()
{
return itemName;
}
public double getPrice()
{
return itemPrice;
}
public void setItemName(String someItem)
{
itemName = someItem;
}
public void setItemPrice(double somePrice)
{
itemPrice = somePrice;
}
public String toString()
{
return "Item: " + itemName + " price:" + itemPrice;
}
}
Here is the Class that creates the array and sorts the array by price and by name

import javax.swing.*;
import java.util.*;
// Once you have this class created, you write a second class named CoffeeDriver.   This class has the following methods:
public class CoffeeDriver
{
// sortName – this method sorts the array of items by item name and then displays the name and price of all items on the screen
public static void sortName(Items arr[])
{
// sorts array by name and displays the items and prices
int x;
 Items temp;
 for (x = 0; x < arr.length; x ++)
 
   { if(arr[x].getName().compareTo(arr[x+1].getName()) > 0)
 { temp = arr[x]; arr[x] = arr[x+1]; arr[x+1] = temp; }

JOptionPane.showMessageDialog(null,(arr[x]));
                  }
}




// sortPrice – this method sorts the array of items by item price and then displays the name and price of all items on the screen
public static void sortPrice(Items arr[])
{
//  sorts the array by price and displays the list
int x;
Items temp;
for (x = 0; x < arr.length; x++)
 { if(arr[x].getPrice() > arr[x+1].getPrice())
   { temp = arr[x]; arr[x] = arr[x+1]; arr[x+1] = temp; }
JOptionPane.showMessageDialog(null, arr[x]);

}

}
// main - It creates an array of Item objects using the data above to set each Item's information.
public static void main(String []args)
{
String userSorted;
int x;

 Items arr[] = new Items[5];
  arr[0] = new Items("Coffee", 1.00);
arr[1] = new Items("Water", 2.00);
arr[2] = new Items("Milk", 1.50);
 arr[3] = new Items("Bagel", 1.25);
  arr[4] = new Items("Donut", 0.75);
int selectSort;
JOptionPane.showMessageDialog(null, "Welcome to The Coffee Shop!");
userSorted = JOptionPane.showInputDialog(null, "Please indicate how you would like to sort the menu. To sort by price, press 1. To sort by name, press 2. To exit the program, press 3.");
selectSort = Integer.parseInt(userSorted);
// insert do while loop here

 if (selectSort == 1);
  sortPrice(arr);

  if (selectSort == 2);
      sortName(arr);
   if (selectSort == 3);
  JOptionPane.showMessageDialog(null, "Thank you for using Wings Coffee Shop. Have a great day!");
System.exit(0);

}
}
 
meenal deshpande
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am losing a value in the array with my bubble sorting and the sorting does nothing.
Here is the Items Class

public class Items
{
// * A String instance variable to hold the item name
private String itemName;
// * A double instance variable to hold the price
private double itemPrice;
// * A constructor that takes a String and double to initialize the instance variables
// public Items ()
// {
// //used for sorting
// }
public Items (String itemName, double itemPrice)
{
this.itemPrice = itemPrice;
this.itemName = itemName;
}
// * A get and set method for each instance variable
public String getName()
{
return itemName;
}
public double getPrice()
{
return itemPrice;
}
public void setItemName(String someItem)
{
itemName = someItem;
}
public void setItemPrice(double somePrice)
{
itemPrice = somePrice;
}
public String toString()
{
return "Item: " + itemName + " price:" + itemPrice;
}
}
Here is the Class that creates the array and sorts the array by price and by name

import javax.swing.*;
import java.util.*;
// Once you have this class created, you write a second class named CoffeeDriver.   This class has the following methods:
public class CoffeeDriver
{
// sortName – this method sorts the array of items by item name and then displays the name and price of all items on the screen
public static void sortName(Items arr[])
{
// sorts array by name and displays the items and prices
int x;
 Items temp;
 for (x = 0; x < arr.length; x ++)
 
   { if(arr[x].getName().compareTo(arr[x+1].getName()) > 0)
 { temp = arr[x]; arr[x] = arr[x+1]; arr[x+1] = temp; }

JOptionPane.showMessageDialog(null,(arr[x]));
                  }
}




// sortPrice – this method sorts the array of items by item price and then displays the name and price of all items on the screen
public static void sortPrice(Items arr[])
{
//  sorts the array by price and displays the list
int x;
Items temp;
for (x = 0; x < arr.length; x++)
 { if(arr[x].getPrice() > arr[x+1].getPrice())
   { temp = arr[x]; arr[x] = arr[x+1]; arr[x+1] = temp; }
JOptionPane.showMessageDialog(null, arr[x]);

}

}
// main - It creates an array of Item objects using the data above to set each Item's information.
public static void main(String []args)
{
String userSorted;
int x;

 Items arr[] = new Items[5];
  arr[0] = new Items("Coffee", 1.00);
arr[1] = new Items("Water", 2.00);
arr[2] = new Items("Milk", 1.50);
 arr[3] = new Items("Bagel", 1.25);
  arr[4] = new Items("Donut", 0.75);
int selectSort;
JOptionPane.showMessageDialog(null, "Welcome to The Coffee Shop!");
userSorted = JOptionPane.showInputDialog(null, "Please indicate how you would like to sort the menu. To sort by price, press 1. To sort by name, press 2. To exit the program, press 3.");
selectSort = Integer.parseInt(userSorted);
// insert do while loop here

 if (selectSort == 1);
  sortPrice(arr);

  if (selectSort == 2);
      sortName(arr);
   if (selectSort == 3);
  JOptionPane.showMessageDialog(null, "Thank you for using Wings Coffee Shop. Have a great day!");
System.exit(0);

}
}
 
Marshal
Posts: 70206
280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't quite get the code tags in the right place: [code=java] goes before the code and [/code] after it. But you haven't indented much of your code in the first place.
This is what the code minus the worst of the long lines would have looked like with code tags in the right place:-I know you have been told to write comments to explain the code, but most of those comments don't say anything that isn't already obvious from reading the code. Methods shouldn't have // comments anyway; they should have proper documentation comments with /** ... */ .
Your indentation is incorrect. Lines 54‑55 are really difficult to read. Besides, I think they are in the wrong place. Look at this thread and my posts yesterday.
Don't use System.exit. Let the code come to the end of the method.
Don't stagger indentation. Lines 85‑90 are another bit hard to read. Actually, they can beneficially be shortened. It is potentially error‑prone to populate an array like that. Use an array initialiser:-That code guarantees that the size of the array and the number of elements are exactly the same and there is no risk of gettin gthe index numbers wrong. The [] belong before the name of the array. That class should be called Item not Items.
Why are you using option panes? I can foresee problems on line 57.

What is the problem? What did you mean about losing items, and why does the sorting do nothing? Can you remember what the algorithm for bubble sort is?
 
Bartender
Posts: 4066
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell wrote:I know you have been told to write comments to explain the code, but most of those comments don't say anything that isn't already obvious from reading the code. Methods shouldn't have // comments anyway; they should have proper documentation comments with /** ... */ .


These comments, perhaps looking superfluous, are not meant for some reviewer or so. When I started, I used that kind of comments too. But the intention was to reassure myself that I knew what I was doing, and where I was doing it. In this forum this remark about unnecessary comments is quite often made, but I think that this is just part of the learning process (for the same reason I believe that a Beginner is entitled to a 'Main is a Pain' type of coding).

@Meenal
There are at least two problems with your code. First of all, you did not mention it, but didn't you get an ArrayIndexOutOfBounds exception? Secondly: you are comparing pairs of consecutive elements. Have a look at this topic: example. Do you notice an important difference?
 
Campbell Ritchie
Marshal
Posts: 70206
280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . . When I started, I used that kind of comments too. . . .

Point taken.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic