开发者

Homework Help. I have to "write" a sort I cannot use the API sorts

I have to write a program that takes its arguments from the command line. There are 3 classes. The main app class, a class defining each member of the hall of fame, and then a hall of fame class that creates an array of the members of the hall of fame. The attributes for the HallOFFameMember class are firstName, lastName, yearInducted, and sport. I have to write a sort method within the HallOfFame class that sorts by the year inducted. I was trying to go about it with the comparable utility but I don't think that will work.

Here is the part of the assignment that says write the sort, so I cannot use Array.sort() or anything like that.

"Also define a method in the HallOfFame class named sortMembers that returns an array of HallOfFameMember objects that contains the objects in the members array, sorted on year of induction. Note: the sorting that is done in the sortMembers method, should not use a sort method from the API. You are to write your own sort routine (bubble sort, e.g., would be acceptable)."

I am unsure how to go about writing the sort for an attribute of an object in the array. Any help guiding me to where I can figure out how to do this would be greatly appreciated. Everything I find points to using Array.sort and compareTo method which I cannot use.

** edit **The question is can someone point me to where I can read about or find examples of how to write my own sort to sort by an attribute in an Array of Objects? Ill look into the suggestion of using comparable, as that was what I was leaning towards initially, just couldn't quite get it to work.

     public class HW3 {


        public static void main(String[] args) throws Exception {

            if (args.length % 4 != 0) {
                throw new Exception(
                        "First Name, Last Name, Year Inducted, Sport not entered correctly");
            }

            HallOfFame hallOfFameList = new HallOfFame();
            hallOfFameList.setNumberOfMembers(args.length / 4);

            HallOfFameMember[] tempMembers = new HallOfFameMember[args.length / 4];


            for (int i = 0; i < args.length; i += 4) {
                tempMembers[i/4].setFirstName(args[i]);
                tempMembers[i/4].setLastName(args[i+1]);
                tempMembers[i/4].setYearInducted(Integer.parseInt(args[i+2]));
                tempMembers[i/4].setSport(args[i+3]);
            }

            hallOfFameList.setMembers(tempMembers);
            HallOfFameMember[] sortedMembers = null;
            hallOfFameList.sortMembers(sortedMembers);
            HallOfFame.printReport(sortedMembers);


        }


    }



    public class HallOfFameMember {
    private String firstName;
    private String lastName;
    private String sport;
    private int yearInducted;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getSport() {
        return sport;
    }

    public void setSport(String sport) {
        this.sport = sport;
    }

    public int getYearInducted() {
        return yearInducted;
    }

    public void setYearInducted(int yearInducted) {
        this.yearInducted = yearInducted;
    }

    }
      public class HallOfFame {
private HallOfFameMember[] members;
private int numberOfMembers;

public HallOfFameMember[] getMembers() {
    return members;
}

public void setMembers(HallOfFameMember[] members) {
    this.members = members;
}

public int getNumberOfMembers() {
    return numberOfMembers;
}

public void setNumberOfMembers(int numberOfMembers) {
    this.numberOfMembers = numberOfMembers;
}

public void sortMembers(HallOfFameMember[] sortedMembers){


}

public static void printReport(HallOfFameMember[] print){
    System.out.println("Java Sports Hall of Fame Inductees\n\n");
    System.out.printf("%-30s\t%-30s\t%-30s\n","Name","Year Inducted","Sport");
    for(int i = 0; i < print.length; i++)
    System.out.printf("%-30s\t%-30s\t%-30s\n", print[i].getL开发者_Go百科astName()+","+print[i].getFirstName(), print[i].getYearInducted(), print[i].getSport());
    }

}


To amplify on statements made in my comment:

Since this is homework, most of us will limit our advice some, but we can help you interpret the assignment. I see nothing in the instructions that prevents you from using a Comparator<HallOfFameMember> or implementing Comparable<HallOfFameMember>, and in fact I would do one or the other.

You do however have to write your own sort method, and I'd do this and have it use the Comparator helper class or Comparable interface to help with the sorting. I'd go to wikipedia, read up on the sorting algorithms and choose the easiest, perhaps bubble sort, since you aren't being graded on speed for this assignment, just on getting it done and getting it right.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜