Question

In: Computer Science

I have three classes: class VolleyPlayer, class VolleyTeam and class TestDriver 1.class: public class VolleyPlayer implements...

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.

Solutions

Expert Solution

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:-)


Related Solutions

Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType>...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType> { /** * Construct an empty LinkedList. */ public MyLinkedList( ) { doClear( ); } private void clear( ) { doClear( ); } /** * Change the size of this collection to zero. */ public void doClear( ) { beginMarker = new Node<>( null, null, null ); endMarker = new Node<>( null, beginMarker, null ); beginMarker.next = endMarker; theSize = 0; modCount++; } /**...
How do you write the constructor for the three private fields? public class Student implements Named...
How do you write the constructor for the three private fields? public class Student implements Named { private Person asPerson; private String major; private String universityName; @Override public String name() { return asPerson.name(); }
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
This quiz uses the three classes below. Watch out for bugs! public class Trapezoid { private...
This quiz uses the three classes below. Watch out for bugs! public class Trapezoid { private double height, width, base; public Trapezoid(double h, double w, double b) { setHeight(h); setWidth(w); setBase(b); } public double getHeight() { return height; } public double getWidth() { return width; } public double getBase() { return base; } public void setHeight(double h) { height = h; } public void setWidth(double w) { width = w; } public void setBase(double b) { base = b; }...
how to sorted the list from another class!!! example i have a class public meso public...
how to sorted the list from another class!!! example i have a class public meso public meso(string id) public hashmap<string, integer> neou{ this class print out b d e c a now create another class public mesono public mesono(hashmap<string, integer> nei) //want to sorted meso class print list to this class!! // print out a b c d e } ( -------Java------)   
JAVA Assignement In the same file, create two classes: a public class Module1 and a non-public...
JAVA Assignement In the same file, create two classes: a public class Module1 and a non-public (i.e. default visibility) class Module1Data. These classes should have the following data fields and methods: 1) Module1 data fields: a. an object of type Module1Data named m1d b. an object of type Scanner named scanner c. a double named input 2) Module1Data methods: a. a method named square that returns an int, accepts an int parameter named number, and returns the value of number...
Your Task: Starting with the following generic classes: public class LinkedListNode<E> { public E element; public...
Your Task: Starting with the following generic classes: public class LinkedListNode<E> { public E element; public LinkedListNode<E> next; } public class LinkedListBox<E> { public LinkedListNode<E> head; public int count = 0; } Create a LINKED LIST of random integers between 1 and 20. The program must prompt the user for the number of random integers to create before creating the list (i.e: if user asks for 40 integers then the program will link 40 integers between 1 and 20 generated...
I have written this paper for my public speaking class,and I would appreciate it if someone...
I have written this paper for my public speaking class,and I would appreciate it if someone revised it for me to make sure my punctuations are correct as well as everything else.Thank you, Public Speaking…Dealing with It Often when a person thinks of public speaking the thoughts a person may have will vary from the next, because while one person may have experience the other person may not. Being able to communicate effectively requires that a person acknowledges the frame...
Hi I have problem with run this JAVA file import java.io.*; public class DataPresenter { public...
Hi I have problem with run this JAVA file import java.io.*; public class DataPresenter { public static void main (String args []) { System.out.println("File:../SmallAreaIncomePovertyEstData.text"); System.out.println("Id" + "\t" + "Population" + "\t" + "ChildPop" + "\t" + "CPovPop" + "\t" + "CPovPop%"); }// read the data try (FileReader fr = new FileReader("File: C:\\605.201/SmallAreaIncomePovertyEstData.text")) { int c; while (( c = fr.read())!= -1){ System.out.print((char) c); } } catch(IOException e) { System.out.println("I/O Error" + e); }    } Please help to fix
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements StackInterface<T> {    private Node topNode; // References the first node in the chain       public LinkedStack()    {        topNode = null;    } // end default constructor       public void push(T newEntry)    { topNode = new Node(newEntry, topNode); //       Node newNode = new Node(newEntry, topNode); //       topNode = newNode;    } // end push    public...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT