Question

In: Computer Science

SUMMARY Using Java’s Generics framework, make two classes… one that stores 2 items of any given...

  1. SUMMARY
  1. Using Java’s Generics framework, make two classes… one that stores 2 items of any given type (Pair) and one that stores 3 items of any given type (Triple). Then use the triple value class to store the win & loss records of multiple football teams (or any sport) in an array. Next loop through array and calculate the winning percentage of each team and add team name and win % to a pair class, and then add that instance to another array. Finally print out all team entries in the pair array with their win percentages and state which team has the greatest win percentage.
  1. This lab will involve the following new features:
    1. Generics
  1. DETAILS
  1. Generic classes: You’ll make two classes similar to what we did in class.
    1. One class is the Pair class just like from class, and the other will be a Triple class and will have one more parameter, so A,B,C in the Triple class.
  2. In your main class, you will create at least 5 instances of the Triple class, and in each one store:
    1. Team name – String
    2. Wins – int
    3. Losses – int
  3. Create an array of this Triple generic class type and store these 5 instances in the array.
    1. Here is a guide on creating an array of generic types:

Triple<String,Integer,Integer>[] aoTriple;

aoTriple = new Triple[5];

  1. Now create another array of Pair type that is 5 in length.
  2. Create a for loop and loop through the Triple array.
    1. For each item, calculate the win percentage and create new Pair object of <String,Double> type and place following values in each Pair object:
      1. Team name – String
      2. Winning percentage – double (e.g. .67) – this has to be calculated as opposed to just typing it into code!
        1. NOTE: Winning percentage has to be calculated by dividing wins by total games and can just be stored as a decimal… e.g. .85
        2. IMPORTANT: Int to double note – for each item, you need to cast the wins and losses (int) into double types before doing the above avg math. And you will store the avg in a double.

Remember: casting works like this… (double)aoTriple[x].getSecond()

  1. Print out results:
    1. Finally create a for loop to loop through Pair array and print out the values of each object (team name and win %).
    2. IMPORTANT: Also print out which team has the highest win % by figuring out mathematically.

Solutions

Expert Solution

1. Triple class

package Library;

public class Triple<A,B,C> {
   A teamName;
   B wins;
   C loses;
   public Triple(A teamName, B wins, C loses) {
       super();
       this.teamName = teamName;
       this.wins = wins;
       this.loses = loses;
   }
   public A getTeamName() {
       return teamName;
   }
   public void setTeamName(A teamName) {
       this.teamName = teamName;
   }
   public B getWins() {
       return wins;
   }
   public void setWins(B wins) {
       this.wins = wins;
   }
   public C getLoses() {
       return loses;
   }
   public void setLoses(C loses) {
       this.loses = loses;
   }
  
}

2.Pair Class

package Library;

public class Pair<A,B> {
   A teamName;  
   B winPerc;
  
   public Pair(A teamName, B winPerc) {
       this.teamName = teamName;
       this.winPerc = winPerc;
   }
   public A getTeamName() {
       return teamName;
   }
   public void setTeamName(A teamName) {
       this.teamName = teamName;
   }
   public B getWinPerc() {
       return winPerc;
   }
   public void setWinPerc(B winPerc) {
       this.winPerc = winPerc;
   }
  
  
}

3.Main class

package Library;

public class TeamMian {

   public static void main(String[] args) {
       Triple<String, Integer, Integer>[] aoTriple = new Triple[5];
      
       Triple<String, Integer, Integer> t1 = new Triple<String, Integer, Integer>("Team1", 4, 1);
       Triple<String, Integer, Integer> t2 = new Triple<String, Integer, Integer>("Team2", 2, 2);
       Triple<String, Integer, Integer> t3 = new Triple<String, Integer, Integer>("Team3", 3, 2);
       Triple<String, Integer, Integer> t4 = new Triple<String, Integer, Integer>("Team4", 1, 4);
       Triple<String, Integer, Integer> t5 = new Triple<String, Integer, Integer>("Team5", 2, 3);
      
       aoTriple[0] = t1;
       aoTriple[1] = t2;
       aoTriple[2] = t3;
       aoTriple[3] = t4;
       aoTriple[4] = t5;
      
       Pair<String, Double>[] aoPair = new Pair[5];
       int index = 0;
       double max = 0;
       String highScteam = "";
       for(Triple<String, Integer, Integer> t : aoTriple) {
           int wins = (int) t.getWins();
           int lose = (int) t.getLoses();
           double perc = ((double) t.getWins()/(double) (wins+lose))*100;
           Pair<String, Double> p = new Pair<String, Double>((String)t.getTeamName(), perc);
           aoPair[index] = p;
           if(perc > max) {
               max = perc;
               highScteam = (String)t.getTeamName();
           }
           index++;
       }
      
       for(Pair<String, Double> p : aoPair)
       {
           System.out.println("Team Name : "+p.getTeamName()+ " Win% : "+p.getWinPerc());
       }
      
       System.out.println("Team having highest perc: "+highScteam);
   }

}


Related Solutions

Given the following list of classes, attributes and methods, - identify which items are classes, which...
Given the following list of classes, attributes and methods, - identify which items are classes, which items are attributes and which items are methods; - identify which class each attribute and method belongs to; and - suggest a class hierarchy given your list of classes. *Note - no particular capitalization scheme is used in the list below to differentiate between classes, methods, and attributes. LandOnStatue, NumberOfLegs, Height, ShoeSize, Eat, Animal, Speak, WingSpan, Age, Peck, Sleep, Horse, LengthOfMane, Move, BeakLength, LengthOfTail,...
2. To compare prices of two grocery stores in Toronto, a random sample of items that...
2. To compare prices of two grocery stores in Toronto, a random sample of items that are sold in both stores were selected and their price noted in the first weekend of July 2018: (12 Points) Item Store A Store B Difference (Store A - Store B) 1 1.65 1.99 -0.34 2 8.70 8.49 0.21 3 0.75 0.90 -0.15 4 1.05 0.99 0.06 5 11.30 11.99 -0.69 6 7.70 7.99 -0.29 7 6.55 6.99 -0.44 8 3.70 3.59 0.11 9...
(a) Given that any given meiosis will produce two types of gametes one with two sex...
(a) Given that any given meiosis will produce two types of gametes one with two sex chromosomes and one with only one, what combinations of sex chromosomes would you find in the sperm of a Klinefelter's man and in what proportion? (b) If a Klinefelter’s man mates with a typical (XX) female, what would you expect for the genetic composition of their children? What genotypes would you expect for all their possible children? Indicate the relative proportions of the genotypes...
C++ Assignment 1: Make two classes practice For each of the two classes you will create...
C++ Assignment 1: Make two classes practice For each of the two classes you will create for this assignment, create an *.h and *.cpp file that contains the class definition and the member functions. You will also have a main file 'main.cpp' that obviously contains the main() function, and will instantiate the objects and test them. Class #1 Ideas for class one are WebSite, Shoes, Beer, Book, Song, Movie, TVShow, Computer, Bike, VideoGame, Car, etc Take your chosen class from...
Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes....
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed (such as #include <stdio.h> etc). Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players (you and a friend) take turns placing X’s and O’s onto the board. After each turn, the current board configuration should be displayed, and once a player connects...
Using the Note class and the Quadratic classes you have already developed, make each one Comparable....
Using the Note class and the Quadratic classes you have already developed, make each one Comparable. A Note is larger than another note if it is higher in frequency. A Quadratic is bigger than another Quadratic if it opens faster. 2. Write a driver for each (Note and for Quadratic). In the driver program create a few objects and compare them . then create a list of those objects and sort them. 3. Rewrite the Note class so that a...
To compare prices of two grocery stores in Toronto, a random sample of items that are...
To compare prices of two grocery stores in Toronto, a random sample of items that are sold in both stores were selected and their price noted in the first weekend of July 2018: (12 Points) Item Store A Store B Difference (Store A - Store B) 1 1.65 1.98 -0.33 2 8.70 8.49 0.21 3 0.75 0.89 -0.14 4 1.05 0.99 0.06 5 11.30 11.99 -0.69 6 7.70 7.99 -0.29 7 6.55 6.99 -0.44 8 3.70 3.59 0.11 9 8.60...
Determine if the average price of items at two grocery stores is different. To answer this...
Determine if the average price of items at two grocery stores is different. To answer this question, you will take a sample of the same 20 items at both stores and use this information to perform the appropriate hypothesis test. Which test (the two-sample t-test or paired t-test) is appropriate for this problem based on how the data were collected? How the data are entered into an Excel spreadsheet depends on whether it is more appropriate to use the two-sample...
To compare prices of two grocery stores in Toronto, a random sample of items that are...
To compare prices of two grocery stores in Toronto, a random sample of items that are sold in both stores were selected and their price noted in the first weekend of July 2018: (12 Points) Item Store A Store B Difference (Store A - Store B) 1 1.65 1.98 -0.33 2 8.70 8.49 0.21 3 0.75 0.89 -0.14 4 1.05 0.99 0.06 5 11.30 11.99 -0.69 6 7.70 7.99 -0.29 7 6.55 6.99 -0.44 8 3.70 3.59 0.11 9 8.60...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT