In: Computer Science
Triple<String,Integer,Integer>[] aoTriple;
aoTriple = new Triple[5];
Remember: casting works like this… (double)aoTriple[x].getSecond()
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);
}
}