![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2004
Posts: 1
Rep Power: 0
![]() |
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;
}
} |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
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;
}
}
}
__________________
"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." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Code tags added to original post.
__________________
"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." - Dwight D. Eisenhower |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|