Given the class Player below: import java.util.Scanner; public class Player { private String name; private int goals;
Question:
Given the class Player below:
import java.util.Scanner;
public class Player
{
private String name;
private int goals;
private int games;
public Player()
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter soccer player's name: ");
name = scan.nextLine();
System.out.print("Enter " + name + "'s total goals scores: ");
goals = scan.nextInt();
System.out.print("Enter " + name + "'s total games played: ");
games = scan.nextInt();
}
public double getAverage()
{
return (goals==0? 0: goals/games);
}
}
Writeup a method topPlayers() that takes two parameters: 1. team, an array of Player objects 2. target, a double The method returns the total number of players in team that have an average number of goals per game higher than target.
b.Writeup a method firstFewOdds() that takes 2 parameters: 1. numArray, an array of integers 2. target, an integer The method returns an array of target integers, containing only the first target odds found in numArray. If fewer than target odds are found, the array is padded with -1.
For example, int[] my_array={2,3,4,5,6,7,8,10,1};
if target=5, array returned will contain 3 5 7 1 -1
if target=4, array returned will contain 3 5 7 1
if target=3, array returned will contain 3 5 7
c.Influenza, also known as the flu, is a respiratory illness caused by influenza viruses. TypeA and TypeB viruses cause the large outbreaks during the winter season. To protect ourselves from the flu, one measure is to get the flu vaccine. The flu vaccine contains tiny amounts of the viruses (strains) it protects against. Scientists work hard every year to establish the "correct" strains to be included in the vaccine.
Given the class VirusStrain below, writeup a class Vaccine, with the following members: - Two virus strains, instance data, included in the vaccine - A constructor taking 2 virus strains parameters that initialize the instance data - A getter/setter for each instance data - We compare two vaccines based on the total strength of the virus strains it contains. i.e. vaccine 1 is stronger than vaccine2 if the total strength of the two virus strains in vaccine1 is bigger than the corresponding strength in vaccine2.
public class VirusStrain
{
/** A virus strain is represented by its name and the strength Strength is expected to be a number between 1-10, 10 being the highest strength. The stronger the virus the more symptoms it can cause. A vaccine containing a strain with high strength can potentially protect more. */
private String name;
private int strength;
public VirusStrain(String name, int strength)
{
this.name=name; this.strength=strength;
}
public String getName(){return name;}
public void setName(String n){name=n;}
public int getStrength(){return strength;}
public void setStrength(int s){strength=s;}
public String toString(){return name+" strength: "+strength;}
}
Writeup a compareTo() method that compares two vaccines and returns appropriate values (1,-1,0) based on their total strengths.
- A toString() method returning the information for each virus strain in 2 lines, one line for each strain.
d. Writeup an application TestVaccines that analyzes an array of Vaccine objects for their strengths. The program creates an array of 10 vaccines, all containing the "H1N1" strain of the typeA virus, the "Brisbane" strain of the typeB virus and random strengths, an integer in [1-10], for each strain. The program will find and print to the screen the vaccine that protects the most (You must use the compareTo method for full credit). If more than one such vaccine exist, pick arbitrarily (any of them).
For example, if the information of the vaccines in the array is given in the table below, the program should display:
Vaccine # | Strain1 | Strain 2 |
1 | H1N1,4 | Brisbane, 3 |
2 | H1N1,1 | Brisbane, 3 |
3 | H1N1,6 | Brisbane, 4 |
4 | H1N1,7 | Brisbane, 1 |
5 | H1N1,8 | Brisbane, 5 |
6 | H1N1,4 | Brisbane, 9 |
7 | H1N1,10 | Brisbane, 1 |
8 | H1N1,1 | Brisbane, 2 |
9 | H1N1,3 | Brisbane, 3 |
10 | H1N1,4 | Brisbane, 1 |
H1N1 strength: 8
Brisbane strength: 5
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates