Harrisburg Area Community College Fah ringer CPS 121AVA Prog
Solution
QuarterbackPerformance.java
import java.text.DecimalFormat;
public class QuarterbackPerformance {
private String name;
private int completedPasses;
private int passAttempts;
private int receivingYards;
private int touchdowns;
private int interceptions;
DecimalFormat df = new DecimalFormat(\"#.###\");
public QuarterbackPerformance(String name, int completedPasses, int passAttempts, int receivingYards,
int touchdowns, int interceptions) {
this.name=name;
this.completedPasses=completedPasses;
this.passAttempts=passAttempts;
this.receivingYards=receivingYards;
this.touchdowns=touchdowns;
this.interceptions=interceptions;
}
/**
* Method to calculate completed passes
* @return
*/
public double percentCompletion(){
double percent = (double)this.completedPasses/this.passAttempts*100;
double result = (percent-30)*0.05;
if(result<0){
result = 0;
}
else if(result>2.375){
result = 2.375;
}
return result;
}
/**
* Method to calculate average yards per pass
* @return
*/
public double avgYardsGained(){
double avgYards = (double)this.receivingYards/this.passAttempts;
double result = (avgYards-3)*0.25;
if(result<0){
result = 0;
}
else if(result>2.375){
result = 2.375;
}
return result;
}
/**
* Method to calculate passing touchdowns
* @return
*/
public double percentOfTouchdowns(){
double result = ((double)this.touchdowns/this.passAttempts)*100*0.2;
if(result>2.375){
result = 2.375;
}
return result;
}
/**
* Method to calculate passing interceptions
* @return
*/
public double percentOfInterceptions(){
double percent = (double)this.interceptions/this.passAttempts*100;
double result = 2.375-(percent*0.25);
if(result<0){
result = 0;
}
return result;
}
/**
* Method to calculate rating of the player
* @return
*/
public double rating(){
double result = (percentCompletion() + avgYardsGained() + percentOfTouchdowns() + percentOfInterceptions()) / 6
* 100;
DecimalFormat df = new DecimalFormat(\"#.#\");
return Double.valueOf(df.format(result));
}
/**
* Method to print player\'s result
* @return
*/
public String printResult(){
StringBuilder str = new StringBuilder();
str.append(\"Player Information\");
str.append(\"\ Name: \"+this.name);
str.append(\"\ Completed Passes: \"+this.completedPasses);
str.append(\"\ Pass Attempts: \"+this.passAttempts);
str.append(\"\ Number of Receiving Yards: \"+this.receivingYards);
str.append(\"\ Number of passing touchdowns: \"+this.touchdowns);
str.append(\"\ Number of passing interceptions: \"+this.interceptions);
str.append(\"\ Rating: \"+rating());
str.append(\"\ Percent Completion: \"+df.format(percentCompletion()));
str.append(\"\ Average Yards Gained: \"+df.format(avgYardsGained()));
str.append(\"\ Percent of Touchdowns: \"+df.format(percentOfTouchdowns()));
str.append(\"\ Percent of Interceptions: \"+df.format(percentOfInterceptions()));
return str.toString();
}
}
Main.java
import java.util.Scanner;
/**
* Main class to input player performance and print rating
* @author Anonymous
*
*/
public class Main {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println(\"Enter Player Information\");
String name = sc.next();
int completed = sc.nextInt();
int attempts = sc.nextInt();
int yards = sc.nextInt();
int touchdowns = sc.nextInt();
int interceptions = sc.nextInt();
QuarterbackPerformance qp = new QuarterbackPerformance(name, completed, attempts, yards, touchdowns, interceptions);
System.out.println(qp.printResult());
}
}


