Question
Draw a UML class and structure diagram which shows the relationship among the classes in your solution. It must be submitted as an image file
-
Draw a UML class and structure diagram which shows the relationship among the classes in your solution. It must be submitted as an image file (e.g. jpg, png).
-
Use Javadoc style comments for ALL public methods and constructors in your Contestant and
Draw a UML class and structure diagram which shows the relationship among the classes in your solution. It must be submitted as an image file (e.g. jpg, png).
Use Javadoc style comments for ALL public methods and constructors in your Contestant and
Contestant Class
public class Contestant implements Comparable
//Instance variables private String name; private int id; private double score;
/** * @param name * @param id * @param score */ public Contestant(String name, int id, double score) { super(); this.name = name; this.id = id; this.score = score; } //Setters public void setScore(double score) { this.score = score; } //Getters public String getName() { return name; }
public int getId() { return id; }
public double getScore() { return score; }
@Override public String toString() { return "Contestant [name=" + name + ", id=" + id + ", score=" + score + "]"; }
@Override public boolean equals(Object obj) { //Compares id and name (case insensitive) Contestant other = (Contestant) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equalsIgnoreCase(other.name)) return false;
return true; }
@Override public int compareTo(Contestant obj) { //Descending orde rof score Double score=this.getScore(); if(score < obj.getScore()) return -1; else if(score>obj.getScore()) return 1; else return 0; }
}
Contestant List Class
public class ContestantList {
//Create contestant array Contestant[] contestantArr; int DEFAULT_SIZE = 3; //To store no of items in array int NO_OF_ITEMS;
//Default contructor public ContestantList() { contestantArr = new Contestant[DEFAULT_SIZE]; } //Parameterized contructor public ContestantList(int SIZE) { contestantArr = new Contestant[SIZE]; } //Insert a contestant public void insert(Contestant cont) { Contestant[] newContestantArr; //If array has enough size if (NO_OF_ITEMS < contestantArr.length) { contestantArr[NO_OF_ITEMS] = cont; NO_OF_ITEMS++; } else { //Create a new array double the size of old array newContestantArr = new Contestant[contestantArr.length * 2]; for (int i = 0; i < NO_OF_ITEMS; i++) { newContestantArr[i] = contestantArr[i]; } newContestantArr[NO_OF_ITEMS] = cont; NO_OF_ITEMS++; contestantArr = newContestantArr; } //Sort array in descending order of scores sortArray(contestantArr); } //Return size of array public int size() { return NO_OF_ITEMS; } //To find contestant in the array public Contestant find(String name) { for (int i = 0; i < NO_OF_ITEMS; i++) { if (contestantArr[i].getName().equalsIgnoreCase(name)) return contestantArr[i]; } return null; } //Count number of occurences of given contestant public int countOccurences(Contestant cont) { int noOfInstances = 0;
for (int i = 0; i < NO_OF_ITEMS; i++) { if (contestantArr[i].equals(cont)) noOfInstances++; } return noOfInstances; } //Count no of contestants in score range public int countRange(int sc1, int sc2) { int noOfContestants = 0; if (sc1 > sc2) return 0; else { for (int i = 0; i < NO_OF_ITEMS; i++) { if (contestantArr[i].getScore() >= sc1 && contestantArr[i].getScore() <= sc2) noOfContestants++; } } return noOfContestants; } //Remove a contestant in array public Contestant delete(Contestant c) { Contestant contestantRemoved = null; int index = 0; //Get the contestant to be removed for (int i = 0; i < NO_OF_ITEMS; i++) { if (contestantArr[i].equals(c)) { contestantRemoved = contestantArr[i]; index = i; break; } } if (contestantRemoved != null) { // shift elements to left for (int i = index; i < NO_OF_ITEMS - 1; i++) { contestantArr[i] = contestantArr[i + 1]; } NO_OF_ITEMS--; } return contestantRemoved; } //Sort array in descending order of scores private void sortArray(Contestant[] contArr) { Contestant temp=null; for(int i=0;i
StringBuilder str = new StringBuilder(); System.out.println("Name\tID\tScore"); System.out.println("---------------------"); for (int i = 0; i < NO_OF_ITEMS; i++) { str.append(contestantArr[i].getName()).append("\t").append(contestantArr[i].getId()).append("\t") .append(contestantArr[i].getScore()); str.append(" "); } return str.toString(); }
}
Contestant Runner
public class ContestantRunner { //Driver function for Contestant public static void main(String[] args) {
//Create contestant collection object ContestantList contestantList=new ContestantList(5); //Insert 7 items into collection contestantList.insert(new Contestant("Tom", 1, 100)); contestantList.insert(new Contestant("Jack", 2, 150)); contestantList.insert(new Contestant("John", 3, 225)); contestantList.insert(new Contestant("Prince", 4, 75)); contestantList.insert(new Contestant("Prince", 4, 450)); contestantList.insert(new Contestant("Alex", 6, 175)); contestantList.insert(new Contestant("Sachin",7, 275)); //Access contestant methods System.out.println("Number of contestants added: "+contestantList.size()); Contestant cont=new Contestant("Prince", 4, 450); System.out.println(" No of occurences of contestant 'Prince':"+contestantList.countOccurences(cont)); System.out.println(" No of contestants in score range (200-500):"+contestantList.countRange(200, 500)); System.out.println(" Contestants in descending order of score:"); System.out.println(contestantList.toString()); Contestant contToDelete=contestantList.find("John"); System.out.println("Find John: "+contestantList.find("John")); //Remove contestant contestantList.delete(contToDelete); System.out.println(" After deleting John:"); //Display contestants System.out.println("Contestants in descending order of score:"); System.out.println(contestantList.toString());
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started