1 reply to this topic
#1
Posted 04 May 2011 - 06:28 PM
Hello, I have an assignment where I need to create a class with 3 data members (a string, a double, and an integer). Then create a constructor with arguments of the 3 data members, 3 get methods, and 3 sort methods that sort an array. Then I have to create a program that calls the methods and displays the array based on user input. I am having difficulty calling the sort methods and think it may have to do with the fact that the program declares and array while the class has parameters of other data types. But I have no idea how to fix it. Attached are the codes that I have created so far. Any help would be greatly appreciated.
|
|
|
#2
Posted 04 May 2011 - 07:48 PM
You haven't understood the assignment. That's understandable since it's written confusingly.
Try cleaning up some of what I mentioned, and see what it makes you do on the SortSalon file as well. You can get this down to much less repetition.
/*Create a class for services offered by a hairstyling salon. Data members include a String to hold the service description (for example, Cut, Shampoo, or Manicure), a double to hold the price, and an integer to hold the average minutes it takes to perform the service. The class name is HairSalon. Include a constructor that requires arguments for all three data members and three get methods that each returns one of the data members values. Include a method to sort the array in ascending order by price of service. Add a second method to sort the HairSalon objects in ascending order by the time it takes to perform the service. Add a third method to sort the HairSalon objects in alphabetical order by service description.*/It's not clear whether the sorting methods are supposed to be static or not, and it makes the HairSalon object just a single service rather than a list of services or something similar. Regardless, assuming the sorting methods are static, what is clear is that they're intended to take an array of HairSalon objects, not an array of array of String. Let's take the sortPrice method as an example.
public static String[][] sortPrice(salonArray){
for (int pass = 0; pass<HairSalon.length-1; pass++) { //passes
for (int i= 0; i<HairSalon.length-1; i++) { //one pass
if ((HairSalon[i][1]).compareTo(HairSalon[i+1][1])> 0) { //comparison
String[][] temp = new String [6][3];
temp[i][0] = HairSalon[i][0]; //swap
temp[i][1] = HairSalon[i][1];
temp[i][2] = HairSalon[i][2];
HairSalon[i][0] = HairSalon[i+1][0];
HairSalon[i][1] = HairSalon[i+1][1];
HairSalon[i][1] = HairSalon[i+1][2];
HairSalon[i+1][0] = temp[i][0];
HairSalon[i+1][1] = temp[i][1];
HairSalon[i+1][2] = temp[i][2];
} //end if
}//end for i
}//end for pass
return sortPrice;
}//end method
Right now you're... trying to pass one of your non-static private values into a static method signature, and returning a String[][], when this method should be sorting an array of HairSalon objects in place.public static void sortPrice(HairSalon[] services)The rest on your methods should be equivalent.
temp[i][0] = HairSalon[i][0]; //swap temp[i][1] = HairSalon[i][1]; temp[i][2] = HairSalon[i][2]; HairSalon[i][0] = HairSalon[i+1][0]; HairSalon[i][1] = HairSalon[i+1][1]; HairSalon[i][1] = HairSalon[i+1][2]; HairSalon[i+1][0] = temp[i][0]; HairSalon[i+1][1] = temp[i][1]; HairSalon[i+1][2] = temp[i][2];Why are you doing this? What are you trying to do? HairSalon is a class name, not an object instance name. You've mistaken calling static methods (using the class name HairSalon.sortPrice() for example) as calling an instance of that method, or what's more, conflated object instances with arrays. This simply isn't how things roll in Java, and since you're just sorting an array of HairSalon objects, this overly complicated system evaporates.
for (int i = 1; i < services.length(); /* no iteration */)
{
HairSalon temp, former = services[i - 1], latter = services[i];
if (former.getPrice() > latter.getPrice())
{
// Perform swap:
temp = former; former = latter; latter = temp;
// decrement i, unless i == 1.
i -= (i > 1) ? 1 : 0;
}
else
{
++i;
}
}
I can't really tell how you intended your program to perform sorting, since you were using the compareTo() method like you would a comparable object, but sortTime compares an int, so it doesn't have any methods, let alone implement the Comparable interface.Try cleaning up some of what I mentioned, and see what it makes you do on the SortSalon file as well. You can get this down to much less repetition.
Wow I changed my sig!
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









