/* * Skater.java * * Created on February 3, 2003, 4:03 PM */ package Skater; /** * * @author Administrator */ public class Skater { private String name = ""; private String country = ""; private static int numSkaters = 0; private static int numJudges = 10; private double[] artScores; private double[] techScores; public static final double MAX_SCORE = 6.0; public static final double MIN_SCORE = 0.0; //////////////////////////////////////////////////////////// //// Constructors //////////////////////////////////////////////////////////// /** * Creates a default new instance of Skater */ public Skater() { // set up arrays - allow values to take default (zero) artScores = new double[numJudges]; techScores = new double[numJudges]; // keep track that a skater was created numSkaters++; } /** * Creates a new instance of Skater with name and country known */ public Skater(String nam, String nation) { name = nam; country = nation; // set up arrays - allow values to take default (zero) artScores = new double[numJudges]; techScores = new double[numJudges]; // keep track that a skater was created numSkaters++; } //////////////////////////////////////////////////////////// //// Overriding from Object //////////////////////////////////////////////////////////// /** * report whether two skaters are considered the same * Need a policy decision here * For now, consider them the same if they have the same name * and country */ public boolean equals(Object obj) { // avoid blow up if object passed isn't a skater if (obj instanceof Skater) { return false; } else { // correctly comparing to a skater - check if same name and country Skater compared = (Skater) obj; if ((this.getName().equals(compared.getName())) && (this.getCountry().equals(compared.getCountry()))) { return true; } else { return false; } } } /** * give an output friendly representation of the skater * Overrides Object version * Policy decision: * show name, country, artistic and technical ratings if available, and total if available */ public String toString() { String res = this.getName(); res = res + " " + this.getCountry(); res = res + " Art: " + this.getArtRating(); res = res + " Tech: " + this.getTechRating(); res = res + " Total: " + this.getTotalRating(); return res; } /** * Calculate a hash code for a Skater Object * Overrides Object version * From what I understand, in order to make some collections classes work, * if you override toString / equals you should override hashCode * (Unfortunately, my decisions on toString and equals are not consistent * with each other :-) ) */ public int hashCode() { String temp = this.toString(); int convert = 0; for (int c = 0; c < temp.length(); c++) { convert += (int) temp.charAt(c); } int hash = convert % 10; // just take last digit - binning into 10 bins return hash; } //////////////////////////////////////////////////////////// //// Inspectors //////////////////////////////////////////////////////////// /** * report skater's name */ public String getName() { return name; } /** * report skater's country */ public String getCountry() { return country; } /** * static method to get how many judges there are */ public static int getNumJudges() { return numJudges; } /** * Calculate and report the skater's total technical rating * This involves dropping high and low score and getting sum of rest * Assumes that any Skater has array of Tech Scores, even if the scores * themselves havn't been altered from default 0 */ public double getTechRating() { // default max and min to bad values so first judges rating will be both highest and lowest double max = MIN_SCORE - 1; double min = MAX_SCORE + 1; // start total double total = 0.0; // loop through all scores finding max, min, and total for (int judge = 0; judge < getNumJudges(); judge++) { total = total + getNthTechScore(judge); // if current score is new highest, keep track if (getNthTechScore(judge) > max) { max = getNthTechScore(judge); } // if current score is new lowest, keep track if (getNthTechScore(judge) < min) { min = getNthTechScore(judge); } } // remove highest and lowest score from total total = total - (max + min); return total; } /** * Calculate and report the skater's total artistic rating * This involves dropping high and low score and getting sum of rest * Assumes that any Skater has array of Artistry Scores, even if the scores * themselves havn't been altered from default 0 */ public double getArtRating() { // default max and min to bad values so first judges rating will be both highest and lowest double max = MIN_SCORE - 1; double min = MAX_SCORE + 1; // start total double total = 0.0; // loop through all scores finding max, min, and total for (int judge = 0; judge < getNumJudges(); judge++) { total = total + getNthArtScore(judge); // if current score is new highest, keep track if (getNthArtScore(judge) > max) { max = getNthArtScore(judge); } // if current score is new lowest, keep track if (getNthArtScore(judge) < min) { min = getNthArtScore(judge); } } // remove highest and lowest score from total total = total - (max + min); return total; } /** * calculate and report skater's total score - artistic + technical */ public double getTotalRating() { double res = getTechRating() + getArtRating(); return res; } /** * report Nth judge's score for this skater on artistry * if invalid N is passed, for now return -1 * In future, once we cover exception handling, throw an exception */ public double getNthArtScore(int n) { if ((n >= 0) && (n < getNumJudges())) { // valid n return artScores[n]; } else { return -1; } } /** * report Nth judge's score for this skater on technical skill * if invalid N is passed, for now return -1 * In future, once we cover exception handling, throw an exception */ public double getNthTechScore(int n) { if ((n >= 0) && (n < getNumJudges())) { // valid n return techScores[n]; } else { return -1; } } //////////////////////////////////////////////////////////// //// Mutators //////////////////////////////////////////////////////////// /** * change skaters name * Assumes name is ok, regardless */ public void setName(String nam) { name = nam; } /** * change skaters country * Assumes country is ok, regardless * Could use a lookup in fancier class */ public void setCountry(String nam) { country = nam; } /** * Change the number of judges * Policy Decision: * Don't accept change if there have been skaters created * Could be controversial - we might have different competitions with different numbers of judges * but in that case, we should make numJudges non-static */ public static boolean setNumJudges(int num) { if (numSkaters > 0) { // reject change return false; } else { numJudges = num; return true; } } /** * set a particular judge's score for the skater for artistry * Reject change if invalid judge number or invalid score */ public boolean setNthArtScore(int n, double score) { if ((n >= 0) && (n < getNumJudges())) { // valid n // check score if ((score >= MIN_SCORE) && (score <= MAX_SCORE)) { // valid score artScores[n] = score; return true; } else { return false; } } else { return false; } } /** * set a particular judge's score for the skater for artistry * Reject change if invalid judge number or invalid score */ public boolean setNthTechScore(int n, double score) { if ((n >= 0) && (n < getNumJudges())) { // valid n // check score if ((score >= MIN_SCORE) && (score <= MAX_SCORE)) { // valid score techScores[n] = score; return true; } else { return false; } } else { return false; } } /** * @param args the command line arguments */ public static void main(String[] args) { Skater x = new Skater(); System.out.println("Skater: " + x); Skater y = new Skater("Tara Lipinski","USA"); System.out.println("Skater: " + y); } }