In: Computer Science
I have three classes: class VolleyPlayer, class VolleyTeam and class TestDriver
1.class:
public class VolleyPlayer implements
Comparable<VolleyPlayer>
{
// instance variables -
private String name;
private int height;
private boolean female;
public VolleyPlayer(String name, int height,
boolean female)
{
this.name = name;
this.height = height;
this.female = female;
}
public String toString()
{
return (female ?"Female":"Male")+": "+name+" ("+height+"
cm)";
}
public boolean isFemale()
{
return female;
}
public boolean isMale()
{
return !female;
}
public int getHeight()
{
return height;
}
-----------------------------------------------------------------------------------
2. class
public class VolleyPlayerTeam
{
private String city;
private ArrayList<VolleyPlayer>players;
/**
* Constructor for objects of class VolleyTeam
*/
public VolleyTeam(String city)
{
this.city = city;
players = new ArrayList<>();
}
public void add(VolleyPlayer vp)
{
players.add(vp);
}
public void printVolleyTeam() {
System.out.println(city);
I am stuck here .....??????
---------------------------------------------
3.class
public class TestDriver
{
public static void test()
{
VolleyPlayer v1= new VolleyPlayer("Misha", 187, false);
VolleyPlayer v2 = new VolleyPlayer("Anya", 172, true);
VolleyPlayer v3= new VolleyPlayer("Andrei, 198, false);
VolleyPlayer v4 = new VolleyPlayer("Lena", 170, true);
VolleyPlayer v5= new VolleyPlayer("Vova", 195, false);
VolleyPlayer v6 = new VolleyPlayer("Dima", 198, false);
System.out.println(v1);
System.out.println(v2);
System.out.println(v3);
System.out.println(v4);
System.out.println(v5);
System.out.println(v6);
VolleyTeam vt= new VolleyTeam("Komsomol");
vt.add(v1);
vt.add(v2);
vt.add(v3);
vt.add(v4);
vt.add(v5);
vt.add(v6);
------------------------------
So my question is:
Program the printVolleyTeam method. The method should print the city the team comes from, followed by all players sorted by gender (women before men). Within each gender, sort by height (maximum to lowest). Try printVolleyTeam in the test method.
Implementation in JAVA:
Code:
import java.util.ArrayList;
import java.util.Collections;
// class TestDriver
public class TestDriver {
// main method
public static void main(String[] args) {
// temporary lists for storing
womens and mens seperately
ArrayList<VolleyPlayer> women
= new ArrayList<VolleyPlayer>();
ArrayList<VolleyPlayer> men =
new ArrayList<VolleyPlayer>();
// create players and store it into
respectative lists
VolleyPlayer v1= new
VolleyPlayer("Misha", 187, false);
if(v1.isFemale()) {
women.add(v1);
}
else {
men.add(v1);
}
VolleyPlayer v2 = new
VolleyPlayer("Anya", 172, true);
if(v2.isFemale()) {
women.add(v2);
}
else {
men.add(v2);
}
VolleyPlayer v3= new
VolleyPlayer("Andrei", 198, false);
if(v3.isFemale()) {
women.add(v3);
}
else {
men.add(v3);
}
VolleyPlayer v4 = new
VolleyPlayer("Lena", 170, true);
if(v4.isFemale()) {
women.add(v4);
}
else {
men.add(v4);
}
VolleyPlayer v5= new
VolleyPlayer("Vova", 195, false);
if(v5.isFemale()) {
women.add(v5);
}
else {
men.add(v5);
}
VolleyPlayer v6 = new VolleyPlayer("Dima", 198, false);
if(v6.isFemale()) {
women.add(v6);
}
else {
men.add(v6);
}
// create object of VolleyPlayerTeam and call constructor
// with passig parameter city name
VolleyPlayerTeam vt= new VolleyPlayerTeam("Komsomol");
// call sort function for sorting womens and mens in descending
order
women = sort(women);
men = sort(men);
// add them into main list
// first add women and the men
for(int i=0;i<women.size();i++) {
vt.add(women.get(i));
}
for(int i=0;i<men.size();i++) {
vt.add(men.get(i));
}
// print team name
System.out.println("\n Team : "+vt.getcity()+"\n");
// call printlist in VolleyPlayerTeam class
vt.printlist();
}
// method for reverse sorting according to their
heights
public static ArrayList<VolleyPlayer>
sort(ArrayList<VolleyPlayer> list){
// declare local variables
ArrayList<VolleyPlayer> temp
= new ArrayList<>();
ArrayList<Integer> height =
new ArrayList<>();
// store heights
for(int i=0;i<list.size();i++)
{
VolleyPlayer vp
= list.get(i);
height.add(vp.getHeight());
}
// reverse sort heights
Collections.sort(height,
Collections.reverseOrder());
//
// fill temp list according to their
heights
for(int i=0;i<height.size();i++)
{
int h =
height.get(i);
for(int
j=0;j< list.size();j++) {
VolleyPlayer vp = list.get(j);
if(vp.getHeight() == h) {
temp.add(vp);
break;
}
}
}
return temp;
}
}
////////////////////// VolleyPlayer
class VolleyPlayer implements
Comparable<VolleyPlayer>
{
// instance variables -
private String name;
private int height;
private boolean female;
public VolleyPlayer(String name, int height, boolean
female)
{
this.name = name;
this.height = height;
this.female = female;
}
public String toString()
{
return (female ?"Female":"Male")+": "+name+" ("+height+"
cm)";
}
public boolean isFemale()
{
return female;
}
public boolean isMale()
{
return !female;
}
public int getHeight()
{
return height;
}
@Override
public int compareTo(VolleyPlayer o) {
return 0;
}
}
// ///////////////////////////// class VolleyPlayerTeam
class VolleyPlayerTeam
{
private String city;
private ArrayList<VolleyPlayer>players;
/**
* Constructor for objects of class VolleyTeam
* @return
*/
public String getcity() {
return city;
}
public VolleyPlayerTeam(String city)
{
this.city = city;
players = new ArrayList<>();
}
public void add(VolleyPlayer vp)
{
players.add(vp);
}
public void printVolleyTeam() {
System.out.println(city);
}
public ArrayList<VolleyPlayer> getlist(){
return players;
}
public void printlist() {
for(int i=0;i<players.size();i++) {
VolleyPlayer vp =
players.get(i);
System.out.println(vp.toString());
}
}
}
SAMPLE OUTPUT;
all players sorted by gender (women before men). Within each gender, sort by height (maximum to lowest)
// PLEASE THUMBS-UP AND RATE POSITIVELY
If you have any doubt regarding this question please ask me in
commnets
// THANK YOU:-)