Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Sorting Question

Options
  • 23-11-2004 9:50am
    #1
    Closed Accounts Posts: 27


    Have to do this for a college Java project http://homepage.eircom.net/~iandowney/m2pra.htm. Basically I have everything done except the last bit of PartC: "The report should be capable of being provided in order of prison number or in order of surname." I have no clue how to go about this. Do you use the java sort method? and if so how? Thanks in advance! :D

    public class prisonManager3
    {
    public static void main(String[]args)
    {
    System.out.println(" ");
    System.out.println("WELCOME TO PRISON MANAGER");
    System.out.println(" ");

    //create array of prisoners
    prisoner [] inmate = new prisoner[3];


    System.out.println("Enter details for each inmate.");

    for(int i=0;i<inmate.length;i++)
    {
    inmate = new prisoner();

    //enter inmate details
    inmate.setName();
    inmate.setAge();
    inmate.setSentence();
    inmate.setNumber();
    inmate.setCellnum(i);

    System.out.println("Inmate Recorded");
    }


    int maxAge = 0, minAge = 999, maxSentence = 0, sentenceCount = 0, youngestCount = 0;

    //check for other details

    for(int i=0;i<inmate.length;i++)
    {
    if(inmate.age > maxAge)
    {
    maxAge = inmate.age;
    }

    if(inmate.age < minAge)
    {
    minAge = inmate.age;
    youngestCount = inmate.pnum;
    }


    if(inmate.sentence > maxSentence)
    {
    maxSentence = inmate.sentence;
    sentenceCount = inmate.cellNum;
    }
    }


    for(int i=0;i<inmate.length;i++)
    {
    System.out.println(" ");
    System.out.println("#" + inmate.getNumber());
    System.out.println("Name: " + inmate.getSurname() + ", " + inmate.getForename());
    System.out.println("Age: " + inmate.getAge());
    System.out.println("Sentence: " + inmate.getSentence() + " years");
    System.out.println(" ");
    }

    //print other details
    System.out.println("The eldest inmate is " + maxAge + " years old.");
    System.out.println("#" + youngestCount + " is the youngest inmate.");
    System.out.println("Cell #" + sentenceCount + " is serving the longest sentence.");

    int yearsCount = 0;

    System.out.println(" ");
    System.out.println(" ");

    for(int j=0;j<inmate.length;j++)
    {
    sentenceCount = inmate[j].sentence / 2;

    for(int i=0;i<100;i++)
    {
    inmate[j].age += 1;
    inmate[j].sentence -= 1;

    if(inmate[j].age == 70 || inmate[j].sentence == sentenceCount)
    {
    i = 100;
    System.out.println("#" + inmate[j].pnum + " will be released after " + yearsCount + " years.");
    }
    else
    {
    yearsCount += 1;
    }
    }
    }

    System.out.println(" ");
    System.out.println(" ");
    System.out.println("!PROGRAM END!");
    System.exit(0);
    }
    }


    Separate class:

    class prisoner
    {
    String forename, surname;
    int age, sentence, pnum=47, cellNum;



    public void setName()
    {
    forename=getit.forename();
    surname=getit.surname();
    }

    public String getForename()
    {
    return forename;
    }

    public String getSurname()
    {
    return surname;
    }




    public void setAge()
    {
    age=getit.age();
    }

    public int getAge()
    {
    return age;
    }




    public void setSentence()
    {
    sentence=getit.sentence();
    }

    public int getSentence()
    {
    return sentence;
    }




    public void setNumber()
    {
    double num;
    num=Math.random();
    num=num * 100;
    num = Math.round(num);
    pnum = (int)num;
    }

    public int getNumber()
    {
    return pnum;
    }




    public void setCellnum(int i)
    {
    cellNum=i + 1;
    }

    }


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Do a search for sorting algorithms. Since there are only 100 prisoners, even a standard linear sort shouldn't be too slow, but a better algorithm will look better and is good experience.

    If you're interested in Object-Oriented Programming (and making a good impressions on lecturers), try to write it in the Object style. Instead of one class doing everything, have two, maybe three classes. Most obviously, a Prisoner class which would take a few variables (forename, surname, age, sentence, prisoner number) and then on creation calculate the earliest possible release time.

    Then you could also have a Prison class, which operates on an array of Prisoner Objects, and providing such methods as addPrisoner(), releasePrisoner(), and also to provide the desired reports for returning the list of Prisoners, sorted what ever way you want.

    :)


  • Registered Users Posts: 1,994 ✭✭✭lynchie


    Also look at the Javadocs for Collections.sort() methods


  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    Arrays.sort() in java.util

    Make inmate.getSurname an object, and sort.

    Arrays.sort(SurnameObject)


  • Closed Accounts Posts: 92 ✭✭tempest


    lynchie wrote:
    Also look at the Javadocs for Collections.sort() methods

    And Arrays.sort() methods and the java.util.Comparator interface.....


  • Closed Accounts Posts: 27 DaZulu


    Thanks for the help. My fourth post since 2001 - a success, lol :D


  • Advertisement
Advertisement