A file called Defendant Data has the following information on inmates: Name (Str
ID: 3568737 • Letter: A
Question
A file called Defendant Data has the following information on inmates:
Name (String)
Age (int)
# of convicted crimes (int)
The name of the crime (String)
The number of years sentenced for the crime (int)
A shell program has been provided that reads the data from the file into an array of PrisonerClass objects.
There are six class methods that you have to define (some of them are simple accessors).
There are three static methods in the main class to define.
These are my results from running my solution:
8 prisoner records read in
Kyle_Jacobs had the longest sentence of 15 years for the crime of Murder_1st
Raymond_Jeers has the maximum total of sentences = 21 years
Program Shell:
============== Util1 Class ====================
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
import java.util.Scanner;
import java.text.DecimalFormat;
import java.io.*;
/**
*
* @author alewit
*/
public class Util1 {
public static Scanner Keyboard = new Scanner(System.in);
/**
* Will either open the file and return a scanner object
* or exit the program if the file cannot be found
* @param Path: a string representing the volume, path and filename
* @return: scanner object
* @throws FileNotFoundException
*/
public static Scanner openFile(String Path) throws FileNotFoundException {
File F = new File(Path);
if (!F.exists()) {
System.err.println(Path + " cannot be found");
System.exit(0);
}
return new Scanner(F);
}
/**
* returns double v rounded to specified number of decimal places
* use -1 to access the currency format
* @param v
* @param d
* @return
*/
public static String Round(double v, int d) {
DecimalFormat Round0 = new DecimalFormat("#,##0");
DecimalFormat Round1 = new DecimalFormat("#,##0.0");
DecimalFormat Round2 = new DecimalFormat("#,##0.00");
DecimalFormat Round3 = new DecimalFormat("#,##0.000");
DecimalFormat Currency = new DecimalFormat("$#,##0.00");
String S = null;
switch (d) {
case -1:
S = Currency.format(v);
break;
case 0:
S = Round0.format(v);
break;
case 1:
S = Round1.format(v);
break;
case 2:
S = Round2.format(v);
break;
case 3:
S = Round3.format(v);
break;
}
return S;
}
}
============== PrisonerClass =====================
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
import java.util.Scanner;
/**
*
* @author alewit
*/
public class PrisonerClass {
private String Name;
private int age, nConv;
private String [] convClass;
private int [] nYrs;
public PrisonerClass(Scanner Fptr){
Name = Fptr.next();
age = Fptr.nextInt();
nConv = Fptr.nextInt();
convClass = new String[nConv];
nYrs = new int [nConv];
for (int j = 0; j < nConv; j++){
convClass[j] = Fptr.next();
nYrs[j] = Fptr.nextInt();
}
}
/**
* #1 Accessor: return the jth conviction class
* @param j
* @return (string)
*/
/**
* #2: Accessor: return the jth sentence in years
* @param j
* @return (int)
*/
/**
* #3: compute and return the sum of years sentenced
* @return (int)
*/
/**
* #4: return the # of years of the conviction with the max sentence
* @return (int)
*/
/**
* #5: return the name of the defendant
* @return (String)
*/
/** #6
* return the age of the defendant
* @return (int)
*/
}
============= Main Class ============
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
import java.util.Scanner;
import java.io.*;
public class JavaApplication1 {
public static final String Path = "C:\temp\DefendantData.txt";
static final int M = 20;
public static void main(String[] args) throws IOException {
// variable declarations
PrisonerClass []Prisoners = new PrisonerClass[M];
int n = getData(Prisoners);
if(n == 0){
System.out.println("no data in file. Halting program");
System.exit(0);
}
System.out.println(n + " prisoner records read in");
// call your void method for #7 here
// call your method for #8 here. Dereference your array with the int defined
// in the line above.
// call your method for #9 here
}///////////////// end of main method //////////////////
/**
* #7: Find the defendant with the maximum number of years for one sentence
* Display the defendants name, the crime class and the # of years sentenced
* @param P: Array of PrisonerClass Objects
* @param n: int representing the # of defendants read in
* THIS IS AN UNFILTERED MAX
*/
/**
*#8 This void method will make it easier to show your results for method #7
* @param P : ONE Prisoner object passed, not the whole array!!!!
*/
/**
* #9: return the INDEX of the prisoner with the most maximum sum of
* sentences among only those prisoners whose age is less than the
* age of the prisoner found in #7
* @param E: Array of PrisonerClass Objects
* @param n: int representing # of prisoners
* @param k: int representing the prisoner found in method #7
* @return : index of the prisoner with max sum of sentences
* THIS IS A FILTERED MAX
*/
/**
// * Open file and read data into array of PrisonerClass objects
* @param E: Array of PrisonerClass Objects
* @return: int representing the # of Prisoners read in
* @throws IOException
*/
public static int getData(PrisonerClass [] P) throws IOException {
Scanner Fptr = Util1.openFile(Path);
int n = 0;
while (Fptr.hasNext() && n < M) {
P[n++] = new PrisonerClass(Fptr);
}
if (Fptr.hasNext()) {
System.out.println("ERROR: more data to read in!");
System.exit(1);
}
Fptr.close();
return n;
}
}/////////////// end of main class //////////////////////////
Explanation / Answer
//============== Util1 Class ====================
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Scanner;
import java.text.DecimalFormat;
import java.io.*;
/**
*
* @author alewit
*/
public class Util1 {
public static Scanner Keyboard = new Scanner(System.in);
/**
* Will either open the file and return a scanner object
* or exit the program if the file cannot be found
* @param Path: a string representing the volume, path and filename
* @return: scanner object
* @throws FileNotFoundException
*/
public static Scanner openFile(String Path) throws FileNotFoundException {
File F = new File(Path);
if (!F.exists()) {
System.err.println(Path + " cannot be found");
System.exit(0);
}
return new Scanner(F);
}
/**
* returns double v rounded to specified number of decimal places
* use -1 to access the currency format
* @param v
* @param d
* @return
*/
public static String Round(double v, int d) {
DecimalFormat Round0 = new DecimalFormat("#,##0");
DecimalFormat Round1 = new DecimalFormat("#,##0.0");
DecimalFormat Round2 = new DecimalFormat("#,##0.00");
DecimalFormat Round3 = new DecimalFormat("#,##0.000");
DecimalFormat Currency = new DecimalFormat("$#,##0.00");
String S = null;
switch (d) {
case -1:
S = Currency.format(v);
break;
case 0:
S = Round0.format(v);
break;
case 1:
S = Round1.format(v);
break;
case 2:
S = Round2.format(v);
break;
case 3:
S = Round3.format(v);
break;
}
return S;
}
}
//============== PrisonerClass =====================
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Scanner;
/**
*
* @author alewit
*/
public class PrisonerClass {
private String Name;
private int age, nConv;
private String [] convClass;
private int [] nYrs;
public PrisonerClass(Scanner Fptr){
Name = Fptr.next();
age = Fptr.nextInt();
nConv = Fptr.nextInt();
convClass = new String[nConv];
nYrs = new int [nConv];
for (int j = 0; j < nConv; j++){
convClass[j] = Fptr.next();
nYrs[j] = Fptr.nextInt();
}
}
/**
* #1 Accessor: return the jth conviction class
* @param j
* @return (string)
*/
public String getConvictionClass(int j) {
return convClass[j];
}
/**
* #2: Accessor: return the jth sentence in years
* @param j
* @return (int)
*/
public int getSentence(int j) {
return nYrs[j];
}
/**
* #3: compute and return the sum of years sentenced
* @return (int)
*/
public int totalSentence() {
int sum = 0;
for(int i=0; i<nYrs.length; i++)
sum += nYrs[i];
return sum;
}
/**
* #4: return the # of years of the conviction with the max sentence
* @return (int)
*/
public int maxSentenceYear() {
int max = nYrs[0];
for(int i=1; i<nYrs.length; i++)
if(max < nYrs[i])
max = nYrs[i];
return max;
}
/**
* #5: return the name of the defendant
* @return (String)
*/
public String getName() {
return Name;
}
/** #6
* return the age of the defendant
* @return (int)
*/
public int getAge() {
return age;
}
public String convictionClassWithMaxSentence() {
int max = nYrs[0];
String convClassWithMaxSentence = convClass[0];
for(int i=1; i<nYrs.length; i++)
if(max < nYrs[i])
{
convClassWithMaxSentence = convClass[i];
max = nYrs[i];
}
return convClassWithMaxSentence;
}
}
//============= Main Class ============
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Scanner;
import java.io.*;
public class JavaApplication1 {
public static final String Path = "C:\temp\DefendantData.txt";
static final int M = 20;
public static void main(String[] args) throws IOException {
// variable declarations
PrisonerClass []Prisoners = new PrisonerClass[M];
int n = getData(Prisoners);
if(n == 0){
System.out.println("no data in file. Halting program");
System.exit(0);
}
System.out.println(n + " prisoner records read in");
// call your void method for #7 here
// call your method for #8 here. Dereference your array with the int defined
// in the line above.
// call your method for #9 here
int maxPrisonerIndex = defendantMaxYears(Prisoners, n);
displayPrisoner(Prisoners[maxPrisonerIndex]);
int maxSumYearsPrisonersIndex = maxSumSentence(Prisoners, n, maxPrisonerIndex);
System.out.println(Prisoners[maxSumYearsPrisonersIndex].getName()
+ " has the maximum total of sentences = "
+Prisoners[maxSumYearsPrisonersIndex].totalSentence()
+" years");
}///////////////// end of main method //////////////////
/**
* #7: Find the defendant with the maximum number of years for one sentence
* Display the defendants name, the crime class and the # of years sentenced
* @param P: Array of PrisonerClass Objects
* @param n: int representing the # of defendants read in
* THIS IS AN UNFILTERED MAX
*/
public static int defendantMaxYears(PrisonerClass[] P, int n) {
int maxPrisonerIndex = 0;
for(int i=1; i<n; i++)
{
if(P[maxPrisonerIndex].maxSentenceYear() < P[i].maxSentenceYear())
{
maxPrisonerIndex = i;
}
}
return maxPrisonerIndex;
}
/**
*#8 This void method will make it easier to show your results for method #7
* @param P : ONE Prisoner object passed, not the whole array!!!!
*/
public static void displayPrisoner(PrisonerClass P) {
System.out.println(P.getName()
+ " had the longest sentence of "
+P.maxSentenceYear()
+" years for the crime of "
+P.convictionClassWithMaxSentence());
}
/**
* #9: return the INDEX of the prisoner with the most maximum sum of
* sentences among only those prisoners whose age is less than the
* age of the prisoner found in #7
* @param E: Array of PrisonerClass Objects
* @param n: int representing # of prisoners
* @param k: int representing the prisoner found in method #7
* @return : index of the prisoner with max sum of sentences
* THIS IS A FILTERED MAX
*/
public static int maxSumSentence(PrisonerClass[] E, int n, int maxPrisonerIndex) {
int maxSumYearsPrisonersIndex = 0;
for(int i=0; i<n; i++)
{
if(E[maxSumYearsPrisonersIndex].totalSentence() < E[i].totalSentence()
&& E[i].getAge() < E[maxPrisonerIndex].getAge() )
maxSumYearsPrisonersIndex = i;
}
return maxSumYearsPrisonersIndex;
}
/**
// * Open file and read data into array of PrisonerClass objects
* @param E: Array of PrisonerClass Objects
* @return: int representing the # of Prisoners read in
* @throws IOException
*/
public static int getData(PrisonerClass [] P) throws IOException {
Scanner Fptr = Util1.openFile(Path);
int n = 0;
while (Fptr.hasNext() && n < M) {
P[n++] = new PrisonerClass(Fptr);
}
if (Fptr.hasNext()) {
System.out.println("ERROR: more data to read in!");
System.exit(1);
}
Fptr.close();
return n;
}
}/////////////// end of main class //////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
INPUT (defendants.txt)
Kyle_Jacobs
23
2
Murder_1st
15
Thief
4
Raymond_Jeers
11
2
Crime_1
10
Crime_1
11
Einstein1
56
2
Crime_1
14
Crime_1
11
OUTPUT
3 prisoner records read in
Kyle_Jacobs had the longest sentence of 15 years for the crime of Murder_1st
Raymond_Jeers has the maximum total of sentences = 21 years
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.