HI,
I've created a Comparable interface class Person. Which has variables First_name, surname, month(Of birth), day(Of birth) , and birthday(which i have created from month and day). I have a compareTo method which will compare two person objects by the birthday. If the birthday are the same it outputs int 0, if one is bigger it outputs int 1 and if the other one is bigger it outputs int -1. I have also created an arraylist called people, which holds objects of person. Now i am trying to create a selection sort method which will sort the objects into birthday order, here is the code i have so far but am really stumped on what to do now...
import java.util.ArrayList.*;
public class Main
{
public static void selectionSort(Comparable[] people)
{
for (int k = people.length-1; k>0; k --)
{
Comparable tmp;
int Large = 0;
for (int j=1; j<= k; j++)
{
if (people[j].compareTo(people[k]) <0)
{
Large = j;
}
tmp = people[Large];
people[Large] = people[k];
people[k] = tmp;
}
}
}
}
Any help would be brilliant,
Thanks a lot
Dave
12 2550 JosAH 11,448
Recognized Expert MVP
What's the problem? The selection sort itself? There are numerous implementations
available on Google.
kind regards,
Jos
What's the problem? The selection sort itself? There are numerous implementations
available on Google.
kind regards,
Jos
The main problem is getting the birthdays of person and using selection sort to sort them. I have the selection sort code which compiles but i cant make it look into the array people and sort the objects by birthday.
I'll have a look on google but any more help would be excellent.
Thanks
Dave
The main problem is getting the birthdays of person and using selection sort to sort them. I have the selection sort code which compiles but i cant make it look into the array people and sort the objects by birthday.
I'll have a look on google but any more help would be excellent.
Thanks
Dave
Tips on sorting.
JosAH 11,448
Recognized Expert MVP
The main problem is getting the birthdays of person and using selection sort to sort them. I have the selection sort code which compiles but i cant make it look into the array people and sort the objects by birthday.
I'll have a look on google but any more help would be excellent.
Thanks
Dave
Your Person class implements the Comparable interface right? If so there's
nothing more to do. Persons can compare themselves against (other) Persons
and they use their birthdays as the comparison criteria, right?
Your sort method shouldn't and couldn't care less about *what* it's sorting
as long as those things can compare themselves. The sort method shouldn't
even care about birthdays, first- or last-names. To be honest with you: I
don't really understand your problem: you can compare birthdays (the Person
class can do it itself because if implements the Comparable interface).
Just let your sort method sort those Comparables.
kind regards,
Jos
Your Person class implements the Comparable interface right? If so there's
nothing more to do. Persons can compare themselves against (other) Persons
and they use their birthdays as the comparison criteria, right?
Your sort method shouldn't and couldn't care less about *what* it's sorting
as long as those things can compare themselves. The sort method shouldn't
even care about birthdays, first- or last-names. To be honest with you: I
don't really understand your problem: you can compare birthdays (the Person
class can do it itself because if implements the Comparable interface).
Just let your sort method sort those Comparables.
kind regards,
Jos
Yeah i understand that now, but i the problem is getting the class to sort between an array of person objects. i have just posted another string with my code on. I need it to sort the array which i cant get it to do. I understand that the person class will compare two person objects but i cant take the output of either -1,0 or 1 and put it into the sorting algorithm and use that to sort the array. any ideas?
thank Jos you've been a lot of help.
JosAH 11,448
Recognized Expert MVP
Yeah i understand that now, but i the problem is getting the class to sort between an array of person objects. i have just posted another string with my code on. I need it to sort the array which i cant get it to do. I understand that the person class will compare two person objects but i cant take the output of either -1,0 or 1 and put it into the sorting algorithm and use that to sort the array. any ideas?
thank Jos you've been a lot of help.
You're trying to put too much information into too little sentences ;-)
1) your Person class implements the Comparable interface, so two Persons
can compare themselves, i.e. I can do: -
int result= Jeff.compareTo(Beth);
2) You have a Comparable array full of Persons: -
Comparable[] comps= {
-
new Person("Jeff", "Jones", ...),
-
new Person("Beth", "Scott", ...)
-
};
3) What is the problem? Are you unable to implement your sorting method
in terms of Comparable things?
kind regards,
Jos
You're trying to put too much information into too little sentences ;-)
1) your Person class implements the Comparable interface, so two Persons
can compare themselves, i.e. I can do: -
int result= Jeff.compareTo(Beth);
2) You have a Comparable array full of Persons: -
Comparable[] comps= {
-
new Person("Jeff", "Jones", ...),
-
new Person("Beth", "Scott", ...)
-
};
3) What is the problem? Are you unable to implement your sorting method
in terms of Comparable things?
kind regards,
Jos
Yes,
1) i cant implement my sorting method to my comparable things.
2) Also I need to make this sorting method in a new class and I cant find out how to call my array into the new class.
3) My sorting method has to look at every person in the array and compare it with the one next to it to find out whether it is greater or not.
Thanks Jos
JosAH 11,448
Recognized Expert MVP
Yes,
1) i cant implement my sorting method to my comparable things.
2) Also I need to make this sorting method in a new class and I cant find out how to call my array into the new class.
3) My sorting method has to look at every person in the array and compare it with the one next to it to find out whether it is greater or not.
Thanks Jos
You're welcome. I guess that point 2) is the major hurdle, right? Why not
just pass that array in as a parameter and return the same array again as
a convenience. You can even make your sorter class a utility class, i.e. just
static methods. Read my "tip of the week" article (one of the older ones) and
see how it's done. Here's an outline: -
public class SelectionSort {
-
private SelectionSort() { } // no instantiations of this class possible
-
//
-
public static Comparable[] sort(Comparable[] a) {
-
// a whole lot of voodoo in here, i.e. your sorting method
-
return a;
-
}
-
}
Now you can sort any array like this: -
Comparable[] a = { ... };
-
Comparable[] sortedArray= SelectionSort.sort(a);
kind regards,
Jos
You're welcome. I guess that point 2) is the major hurdle, right? Why not
just pass that array in as a parameter and return the same array again as
a convenience. You can even make your sorter class a utility class, i.e. just
static methods. Read my "tip of the week" article (one of the older ones) and
see how it's done. Here's an outline: -
public class SelectionSort {
-
private SelectionSort() { } // no instantiations of this class possible
-
//
-
public static Comparable[] sort(Comparable[] a) {
-
// a whole lot of voodoo in here, i.e. your sorting method
-
return a;
-
}
-
}
Now you can sort any array like this: -
Comparable[] a = { ... };
-
Comparable[] sortedArray= SelectionSort.sort(a);
kind regards,
Jos
Hi,
I've done what you've said and it's looking very good.
One last thing - in the space where i call my array i have entered People.people.
This is because my array is in the People class and it is named people.
But the method cant find it.
Do i enter the [] somewhere?
or am i being very stupid!
Thankyou again for all your help,
Dave
Ganon11 3,652
Recognized Expert Specialist
It is probably complaining because the array people is private in the People class - as it should be. Instead, refer to the parameter name - in Jos' example, a. Use a instead of People.people.
JosAH 11,448
Recognized Expert MVP
Hi,
I've done what you've said and it's looking very good.
One last thing - in the space where i call my array i have entered People.people.
This is because my array is in the People class and it is named people.
But the method cant find it.
Do i enter the [] somewhere?
or am i being very stupid!
Thankyou again for all your help,
Dave
What is the type of that "people" array? Did you do this:
or:
kind regards,
Jos
What is the type of that "people" array? Did you do this:
or:
kind regards,
Jos
Ok here is my code for my people class: - import java.util.*;
-
import java.util.ArrayList.*;
-
-
public class People
-
{
-
-
public int ArrayList (String[] args)
-
{
-
Person Chris = new Person("Chris", "Davies", 10, 7);
-
Person Jess = new Person ("Jess", "Levy", 4, 17);
-
Person Dave = new Person ("Dave", "McDonald", 4, 14);
-
Person Andy = new Person ("Andy", "Davies", 3, 6);
-
-
-
ArrayList<Person> people = new ArrayList<Person>();
-
people.add(Chris);
-
people.add(Jess );
-
people.add(Dave );
-
people.add( Andy );
-
-
-
int i = people.size();
-
-
-
System.out.println("Size = " + i);
-
Person first = people.get(0);
-
System.out.println(first.GetFirst_name()+"'s Birthday = "+ first.GetDay()+ "/"+ first.GetMonth());
-
Person Second = people.get(1);
-
System.out.println(Second.GetFirst_name()+"'s Birthday = "+ Second.GetDay()+ "/"+ Second.GetMonth());
-
Person Third = people.get(2);
-
System.out.println(Third.GetFirst_name()+"'s Birthday = "+ Third.GetDay()+ "/"+Third.GetMonth());
-
Person Fourth = people.get(3);
-
System.out.println(Fourth.GetFirst_name()+"'s Birthday = "+ Fourth.GetDay()+ "/"+ Fourth.GetMonth());
-
-
return i;
-
}}
-
-
-
and here is the code for my main class (sorting method):
-
-
-
import java.util.Arrays.*;
-
-
public class main
-
{
-
-
-
public static Comparable[] sort(Comparable [] a)
-
{
-
int j;
-
for (int i = 0; i<a.length; i++)
-
{
-
Comparable value = a[i];
-
for ( j = i; j>0; j--);
-
{
-
if (a[j-1].compareTo (a) <0)
-
{
-
break;
-
}
-
else
-
{
-
a[j] = a[j-1];
-
}
-
}
-
a[j] = value;
-
-
-
}
-
return a;
-
}
-
-
public int sortArray()
-
{
-
Comparable[] a = {People[] .people};
-
Comparable[] sortedArray = main.sort(a);
-
}
-
}
-
I've changed the People[].people part to everything i can think of but nothiong works. Thanks
Dave
Sign in to post your reply or Sign up for a free account.
Similar topics
by: srampally |
last post by:
I need the capabilty to hide/show a selection list, just the way its
done at http://www.lufthansa.com (place the cursor over "Group
Companies"). However, I am looking for a javascript that is much...
|
by: shaveta |
last post by:
pls help me to write a program in c that reads n elements of type float, stores them in a linked list ,sort it in the descending order using selection sort methods, and finally output the sorted...
|
by: gemacjr1201 |
last post by:
I need hints for an algorithm using string compare in a selection sort.
I will be sorting lastnames and first namesalphbetically. if the selection sort encounters the same last name it then goes to...
|
by: ShaveDave27 |
last post by:
Hi,
I've created a Person Class with a comparable interface. And i've created an ArrayList People with varaibles from the person class in - First_name, Surname, Month(Birthday), Day(Birthday).
Now...
|
by: Ganon11 |
last post by:
Earlier, mmccarthy was kind enough to post a short article explaining the Bubble Sort. As she said, this is a relatively slow sorting algorithm. Here, I will attempt to present a slightly faster...
|
by: sialater |
last post by:
Hello,
I realise there are a lot of topics related to this problem but many of what I have found has run cold or unresolved. What I have is an addressbook clone where there are groups which have...
|
by: mfshake |
last post by:
i am using only a driver and not a server. I know how to do selection sort for ints but can't figure out how to do it for Strings. I want to sort it my first letter only Here is my code that i...
|
by: HarishAdea |
last post by:
Hi,
I am trying to run the JAVA pgm, but it is giving error as "selection does not contain a main type".
The filename is "ScoreLeadSummary.java" when i try to run it or debug,it gives the pop...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |