Advertisement
Guest User

Sorting

a guest
Oct 4th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. //Class named CoffeeDriver
  4. public class CoffeeDriver {
  5. //Default main class
  6. public static void main(String[] args) {
  7. String ans;
  8. Scanner keyBoard = new Scanner(System.in); //Allows user input
  9. //Array to hold all of the items and prices
  10. item item[] = new item[5];
  11. item[0] =new item("Coffee", 1.00);
  12. item[0].setName("Coffee");
  13. item[0].setPrice(1.00);
  14. item[1] = new item("Water ", 2.00);
  15. item[1].setName("Water ");
  16. item[1].setPrice(2.00);
  17. item[2] =new item("Milk ", 1.50);
  18. item[2].setName("Milk ");
  19. item[2].setPrice(1.50);
  20. item[3] =new item("Bagel ", 1.25);
  21. item[3].setName("Bagel ");
  22. item[3].setPrice(2.00);
  23. item[4] = new item("Donut ", 0.75);
  24. item[4].setName("Donut ");
  25. item[4].setPrice(0.75);
  26. //Welcome message and directions
  27. System.out.println("Welcome to Peace's Coffee Shop! \n" );
  28. System.out.println ("Would you like to see these items sorted by "
  29. + "name or price? (n/p): ");
  30. ans = keyBoard.nextLine(); //Input from user on which sort to view
  31. System.out.println("Items:" + " " + "Price:");
  32.  
  33.  
  34.  
  35.  
  36. System.out.println(item[0].getName() + " " + item[0].getPrice());
  37. System.out.println(item[1].getName() + " " + item[1].getPrice());
  38. System.out.println(item[2].getName() + " " + item[2].getPrice());
  39. System.out.println(item[3].getName() + " " + item[3].getPrice());
  40. System.out.println(item[4].getName() + " " + item[4].getPrice());
  41. }
  42.  
  43. public static void selectionSort2(int[] x) {
  44. for (int i=0; i<x.length-1; i++) {
  45. int minIndex = i; // Index of smallest remaining value.
  46. for (int j=i+1; j<x.length; j++) {
  47. if (x[minIndex] > x[j]) {
  48. minIndex = j; // Remember index of new minimum
  49. }
  50. }
  51. if (minIndex != i) {
  52. //... Exchange current element with smallest remaining.
  53. int temp = x[i];
  54. x[i] = x[minIndex];
  55. x[minIndex] = temp;
  56. }
  57. }
  58. }
  59.  
  60. }
  61. //Class named Item
  62. public class item {
  63. private String name; //String instance to hold name
  64. private double price; //Double instance to hold price
  65. //Constructor to return values
  66. public item(String itemName, double itemPrice)
  67. {
  68. this.name = itemName;
  69. this.price = itemPrice;
  70. }
  71. //Get method for name
  72. public String getName()
  73. {
  74. return this.name;
  75. }
  76. //Set method for name
  77. public void setName(String Itemname)
  78. {
  79. this.name = Itemname;
  80. }
  81. //Get method for price
  82. public double getPrice()
  83. {
  84. return this.price;
  85. }
  86. //Set method for price
  87. public void setPrice(double Itemprice)
  88. {
  89. this.price = Itemprice;
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement