Question

In: Computer Science

Please finish this code (Java) 1. Fill in the compare methods for the CompareByPlayoffsAndSalary and CompareByWinsLossesChamps...

Please finish this code (Java)

1. Fill in the compare methods for the CompareByPlayoffsAndSalary and CompareByWinsLossesChamps classes

2. In the CompareByPlayoffsAndSalary the compare method should list:

(a) teams that made the playoffs last year before teams that did not.

(b) If this is the same then it should list teams with lower salary first.

3. In the CompareByWinsLossesChamps list:

(a) teams with more wins first.

(b) If the same then list by teams with fewer losses.

(c) If this is the same, teams with the most championships should be listed first.

BasketBallTeam Class
public class BasketBallTeam {

        String name;
        double salaryInMillions;
        int numberOfWins;
        int numberOfLosses;
        int numberOfChampionships;
        boolean playoffTeam;
        
        public BasketBallTeam(String name, double salaryInMillions, int numberOfWins, int numberOfLosses, int numberOfChampionships,
                        boolean playoffTeam) {
                this.name = name;
                this.salaryInMillions = salaryInMillions;
                this.numberOfWins = numberOfWins;
                this.numberOfLosses = numberOfLosses;
                this.numberOfChampionships = numberOfChampionships;
                this.playoffTeam = playoffTeam;
        }

        public double getSalaryInMillions() {
                return salaryInMillions;
        }

        public void setSalaryInMillions(double salaryInMillions) {
                this.salaryInMillions = salaryInMillions;
        }

        public int getNumberOfWins() {
                return numberOfWins;
        }

        public void setNumberOfWins(int numberOfWins) {
                this.numberOfWins = numberOfWins;
        }

        public int getNumberOfLosses() {
                return numberOfLosses;
        }

        public void setNumberOfLosses(int numberOfLosses) {
                this.numberOfLosses = numberOfLosses;
        }

        public int getNumberOfChampionships() {
                return numberOfChampionships;
        }

        public void setNumberOfChampionships(int numberOfChampionships) {
                this.numberOfChampionships = numberOfChampionships;
        }

        public boolean isPlayoffTeam() {
                return playoffTeam;
        }

        public void setPlayoffTeam(boolean playoffTeam) {
                this.playoffTeam = playoffTeam;
        }

        public String toString(){
                return name;
        }
        
}

CompareByWInsLossesChamps Class

import java.util.Comparator;

public class CompareByWinsLossesChamps implements Comparator<BasketBallTeam> {
        
        public int compare(BasketBallTeam a, BasketBallTeam b){
                        }
        }

}

CompareByPlayoffsAndSalary Class

import java.util.Comparator;

public class CompareByPlayoffsAndSalary implements Comparator<BasketBallTeam> {
        
        public int compare(BasketBallTeam a, BasketBallTeam b){
                                }
        }

}

Solutions

Expert Solution

Note: here comapre function is done as per the requirement in the question along with also write explanantion in comment in the code for better understanding, but still if you have any doubt or issue or want to change in the code, you post the issue here.

CompareByPlayoffsAndSalary.java

import java.util.Comparator;

public class CompareByPlayoffsAndSalary implements Comparator<BasketBallTeam> {
        int teamA = 1, teamB = -1;

        /*
         * if compare return positive value (1) value means first argument true if
         * compare return negative value (-1) means second argument is true if compare
         * return 0 means both argument is equal
         */

        /*
         * if condition is true than it return 1 (Team a wins) first argument true ,
         * else condition is true means compare return -1 which is (team b wins) or
         * second argument true, lastly return 0 , as all condition is equal no one wins
         */
        public int compare(BasketBallTeam a, BasketBallTeam b) {
                if (a.isPlayoffTeam() != b.isPlayoffTeam()) {

                        if (a.isPlayoffTeam())
                                return teamA;
                        else
                                return teamB;

                } else if (a.getSalaryInMillions() != b.getSalaryInMillions()) {

                        if (a.getSalaryInMillions() < b.getSalaryInMillions())
                                return teamA;

                        else
                                return teamB;

                }
                return 0;
        }
}

CompareByWinsLossesChamps.java

import java.util.Comparator;

public class CompareByWinsLossesChamps implements Comparator<BasketBallTeam> {

        int teamA = 1, teamB = -1;
        
        /*
         * if compare return positive value (1) value means first argument true if
         * compare return negative value (-1) means second argument is true if compare
         * return 0 means both argument is equal
         */

        /*
         * if condition is true than it return 1 (Team a wins) first argument true ,
         * else condition is true means compare return -1 which is (team b wins) or
         * second argument true, lastly return 0 , as all condition is equal no one wins
         */
        public int compare(BasketBallTeam a, BasketBallTeam b) {
                if (a.getNumberOfWins() != b.getNumberOfWins()) {

                        if (a.getNumberOfWins() > b.getNumberOfWins())
                                return teamA;
                        else
                                return teamB;

                } else if (a.getNumberOfLosses() != b.getNumberOfLosses()) {

                        if (a.getNumberOfLosses() < b.getNumberOfLosses())
                                return teamA;
                        else
                                return teamB;

                } else if (a.getNumberOfChampionships() != b.getNumberOfChampionships()) {

                        if (a.getNumberOfChampionships() > b.getNumberOfChampionships())
                                return teamA;
                        else
                                return teamB;

                }
                return 0;
        }
}

BasketBallTeam.java

public class BasketBallTeam {

        String name;
        double salaryInMillions;
        int numberOfWins;
        int numberOfLosses;
        int numberOfChampionships;
        boolean playoffTeam;

        public BasketBallTeam(String name, double salaryInMillions, int numberOfWins, int numberOfLosses,
                        int numberOfChampionships, boolean playoffTeam) {
                this.name = name;
                this.salaryInMillions = salaryInMillions;
                this.numberOfWins = numberOfWins;
                this.numberOfLosses = numberOfLosses;
                this.numberOfChampionships = numberOfChampionships;
                this.playoffTeam = playoffTeam;
        }

        public double getSalaryInMillions() {
                return salaryInMillions;
        }

        public void setSalaryInMillions(double salaryInMillions) {
                this.salaryInMillions = salaryInMillions;
        }

        public int getNumberOfWins() {
                return numberOfWins;
        }

        public void setNumberOfWins(int numberOfWins) {
                this.numberOfWins = numberOfWins;
        }

        public int getNumberOfLosses() {
                return numberOfLosses;
        }

        public void setNumberOfLosses(int numberOfLosses) {
                this.numberOfLosses = numberOfLosses;
        }

        public int getNumberOfChampionships() {
                return numberOfChampionships;
        }

        public void setNumberOfChampionships(int numberOfChampionships) {
                this.numberOfChampionships = numberOfChampionships;
        }

        public boolean isPlayoffTeam() {
                return playoffTeam;
        }

        public void setPlayoffTeam(boolean playoffTeam) {
                this.playoffTeam = playoffTeam;
        }

        public String toString() {
                return name;
        }

}

Related Solutions

IN JAVA. I have the following code (please also implement the Tester to test the methods...
IN JAVA. I have the following code (please also implement the Tester to test the methods ) And I need to add a method called public int remove() that should remove the first integer of the array and return it at the dame time saving the first integer and bring down all other elements. After it should decrease the size of the array, and after return and save the integer. package ourVector; import java.util.Scanner; public class ourVector { private int[]...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads three strings from the keyboard. Although the strings are in no particular order, display the string that would be second if they were arranged lexicographically.
Can you fix this code please. the removing methods id no doing anything. this is java...
Can you fix this code please. the removing methods id no doing anything. this is java code import java.util.NoSuchElementException; public class DoublyLinkedList<E> {    public int size;    public Node head;    public Node tail;             @Override    public boolean isEmpty() {               return size == 0;    }    @Override    public int getSize() {               return 0;    }    @Override    public void addAtFront(E element) {       ...
Please code by Java. The following preemptive program with this problem after sample output(fill random number,...
Please code by Java. The following preemptive program with this problem after sample output(fill random number, recursive search, non-recursive search, exit), You just need to modify this code to add sorting and time calculations. Problem Description: Expand on Lab3 by adding methods that implement Bubble Sort, Selection Sort, Insertion Sort, Merge Sort and Quick Sort for displaying the sorted integers from the randomly generated array. NOTE: Create separate methods for each type of sort. Run Tests for all 4 types...
Question: Can I get the code in Java for this assignment to compare? Please and thank you....
Question: Can I get the code in Java for this assignment to compare? Please and thank you. Can I get the code in Java for this assignment to compare? Please and thank you. Description Write a Java program to read data from a text file (file name given on command line), process the text file by performing the following: Print the total number of words in the file. Print the total number of unique words (case sensitive) in the file. Print...
Please finish the following JAVA Codes: 1. The class Person includes information of a person’s first...
Please finish the following JAVA Codes: 1. The class Person includes information of a person’s first name, last name, phone number, and email. Implement the class with appropriate constructors, getters and setters. 2. Derive a Student class that extends Person. In addition to a person’s information, a student has major, year, GPA, and a list of enrolled courses. In addition to appropriate constructors, getters and setters, make two methods addCourse and dropCourse. 3. Design and implement a program, either in...
Please show screenshot outputs and fully functional code for the Java program. Write the following methods...
Please show screenshot outputs and fully functional code for the Java program. Write the following methods to   1) read the content of an array of 5 doubles public static double[] readingArray() 2) find and print:the smallest element in an array of 5 double public static void smallest(double [] array) 3) find and print:the largest element in an array of 5 doubles pubic static void largest (double [] array) In the main method - invoke readingArray and enter the following numbers...
PYTHON - please finish the methods below that are apart of a linkedlist class #return the...
PYTHON - please finish the methods below that are apart of a linkedlist class #return the data value at index(position) in the list. values remain unchanged def __getpos__(self, position): #do not change, checks for valid index if self.size == 0: raise IndexError elif position is None: return self.pop(self.size - 1) elif type(position) != int: raise TypeError elif position < 0 or position >= self.size: raise IndexError #replace the data value at requested position(index). return nothing def __setpos__(self,position,value): #do not change,...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /**...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /** 2 * SameArray2.java 3 * @author Sherri Vaseashta4 * @version1 5 * @see 6 */ 7 import java.util.Scanner;8 public class SameArray29{ 10 public static void main(String[] args) 11 { 12 int[] array1 = {2, 4, 6, 8, 10}; 13 int[] array2 = new int[5]; //initializing array2 14 15 //copies the content of array1 and array2 16 for (int arrayCounter = 0; arrayCounter < 5;...
JAVA CODE FILL IN THE BLANKS you are to complete the skeleton program by completing *...
JAVA CODE FILL IN THE BLANKS you are to complete the skeleton program by completing * the To Do sections. I would suggest doing this a * little bit at a time and testing each step before you * go on to the next step. * *           Add whatever code is necessary to complete this assignment *********************************************************************/ //Add whatever code is necessary to complete this assignment public class Methods {    public static void main(String[] args)    {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT