Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 26th, 2004, 2:51 PM   #1
DaZulu
Newbie
 
Join Date: Nov 2004
Posts: 1
Rep Power: 0 DaZulu is on a distinguished road
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. I've looked at javadocs and I know there are things like Arrays.sort()., but I couldn't figure out how to do it. Can you please explain how to do it? Thanks


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[i] = new prisoner();

//enter inmate details
inmate[i].setName();
inmate[i].setAge();
inmate[i].setSentence();
inmate[i].setNumber();
inmate[i].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[i].age > maxAge)
{
maxAge = inmate[i].age;
}

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


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


for(int i=0;i<inmate.length;i++)
{
System.out.println(" ");
System.out.println("#" + inmate[i].getNumber());
System.out.println("Name: " + inmate[i].getSurname() + ", " + inmate[i].getForename());
System.out.println("Age: " + inmate[i].getAge());
System.out.println("Sentence: " + inmate[i].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);
}
}
[/b]

Separate class:

[b]
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;
}

}
DaZulu is offline   Reply With Quote
Old Nov 26th, 2004, 3:25 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
You could use an arrayList with the sort in the collections class, but if you don't want to do that, you could always use a simple bubble sort. Here's the general format:

for(int i = 0; i<array.length-1; i++)
{
   for(int j = 0; j<array.length-1; j++)
   {
      if(array[j]>array[j+1])
      {
        int temp = array[j]; // int of course could be replaced with anything including your prisoner class
        array[j] = array[j+1];
        array[j+1] = temp;
      }
   }
}
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Nov 26th, 2004, 3:26 PM   #3
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Code tags added to original post.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC