Question

In: Computer Science

Java OOP - how do I avoid using getter and setter method in player class for...

Java OOP - how do I avoid using getter and setter method in player class for better privacy? I am told to use regular method instead. but I dont know how

Thank you

-----------------------------------------------------------------------------------------------------------

package MainPackage;

import java.util.ArrayList;

public class SoccerTeam {
private ArrayList<Player> list;
public SoccerTeam(int maxSubmission) {
list = new ArrayList<>(maxSubmission);
}

public boolean addPlayer(String newPlayer) {
if(newPlayer == null || newPlayer.isEmpty() == true) {
return false;
}
for(Player player : list) {
if(player.getName() == newPlayer) {
return false;
}
}
Player player = new Player(newPlayer);
list.add(player);
return true;
}

public int numOfTeamMembers() {
return list.size();
}

public boolean addFile(String name, int[] goal) {
boolean isPlayerPresent = false;
for(Player player : list) {
if(player.getName() == name) {
if(player.getNumOfSubmissions() <= 10) {
isPlayerPresent = true;
player.increaseNumOfSubmissions();
int goals = 0;
for(int i : goal) {
goals = goals + i;
}
if(player.getGoals() < goals ) {
player.setGoals(goals);
}
}
}
}
if(!isPlayerPresent) {
return false;
}
return true;
}

public int goals(String name) {
if(name == null || name.isEmpty() == true) {
return -1;
}
for(Player player : list) {
if(player.getName() == name) {
return player.getGoals();
}
}
return -1;
}

public int numFile(String name) {
for(Player player : list) {
if(player.getName() == name) {
return player.getNumOfSubmissions();
}
}
return -1;
}
}
public class Player {
private int numOfSubmissions;
private int goals;
private String name;
public Player(String name) {
numOfSubmissions = 0;
goals = 0;
this.name = name;
}
public int getNumOfSubmissions() {
return numOfSubmissions;
}
public void increaseNumOfSubmissions() {
this.numOfSubmissions = numOfSubmissions + 1;
}
public int getGoals() {
return goals;
}
public void setGoals(int goals) {
this.goals = goals;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

Solutions

Expert Solution

import java.util.ArrayList;

public class SoccerTeam {
   private ArrayList<Player> list;

   public SoccerTeam(int maxSubmission) {
       list = new ArrayList<>(maxSubmission);
   }

   public boolean addPlayer(String newPlayer) {
       if (newPlayer == null || newPlayer.isEmpty() == true) {
           return false;
       }
       for (Player player : list) {
           if (player.name == newPlayer) {
               return false;
           }
       }
       Player player = new Player(newPlayer);
       list.add(player);
       return true;
   }

   public int numOfTeamMembers() {
       return list.size();
   }

   public boolean addFile(String name, int[] goal) {
       boolean isPlayerPresent = false;
       for (Player player : list) {
           if (player.name== name) {
               if (player.numOfSubmissions <= 10) {
                   isPlayerPresent = true;
                   player.increaseNumOfSubmissions();
                   int goals = 0;
                   for (int i : goal) {
                       goals = goals + i;
                   }
                   if (player.goals < goals) {
                       player.goals=goals;
                   }
               }
           }
       }
       if (!isPlayerPresent) {
           return false;
       }
       return true;
   }

   public int goals(String name) {
       if (name == null || name.isEmpty() == true) {
           return -1;
       }
       for (Player player : list) {
           if (player.name == name) {
               return player.goals;
           }
       }
       return -1;
   }

   public int numFile(String name) {
       for (Player player : list) {
           if (player.name == name) {
               return player.numOfSubmissions;
           }
       }
       return -1;
   }
}

class Player {
   int numOfSubmissions;
   int goals;
   String name;

   public Player(String name) {
       numOfSubmissions = 0;
       goals = 0;
       this.name = name;
   }
   public void increaseNumOfSubmissions() {
       this.numOfSubmissions = numOfSubmissions + 1;
       }
}

Changed, remove getters and setters and removed the private access specifiers


Related Solutions

Write a java program with the following classes: Class Player Method Explanation: play : will use...
Write a java program with the following classes: Class Player Method Explanation: play : will use a loop to generate a series of random numbers and add them to a total, which will be assigned to the variable score. decideRank: will set the instance variable rank to “Level 1”, “Level 2”, “Level 3”, “Level 4” based on the value of score, and return that string. getScore : will return score. toString: will return a string of name, score and rank....
For a LinkedList in Java how do I - Add a method named replace() that takes...
For a LinkedList in Java how do I - Add a method named replace() that takes in two parameters (the data object to replaced, followed by the one to be inserted) that will remove the first occurrence of the item to be replaced and add the second parameter to the list. The method returns a boolean - true if the item to be replaced was found, false if not - Add an instance method named showList() (no parameters or return...
USING JAVA: complete these one method in the BasicBioinformatics class /** * Class BasicBioinformatics contains static...
USING JAVA: complete these one method in the BasicBioinformatics class /** * Class BasicBioinformatics contains static methods for performing common DNA-based operations in * bioinformatics. * * */ public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static methods for performing common DNA-based operations in * bioinformatics. * * */ public class BasicBioinformatics { /** * Calculates and returns the number of times each type of nucleotide occurs in a DNA sequence. * * @param dna a char array representing a DNA sequence of arbitrary length, containing only the * characters A, C, G and T * * @return an int array...
create scientific calculator using java language with OOP rule and interfaces.
create scientific calculator using java language with OOP rule and interfaces.
create calculator standard using java language with OOP rule and interfaces.
create calculator standard using java language with OOP rule and interfaces.
JAVA- How do I edit the following code as minimally as possible to add this method...
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI? BMI Method: public static double calculateBMI(int height, int weight) { double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254)); Format f = new DecimalFormat("##.######"); return (f.format(BMI)); } Code: import java.text.DecimalFormat; import java.util.Scanner; public class test2 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("##.0"); Scanner reader = new Scanner(System.in); System.out.printf("%10s...
Please answer in JAVA I am using java.time.Duration class. 1.). How can I add all of...
Please answer in JAVA I am using java.time.Duration class. 1.). How can I add all of the given duration time? for example: Duration t1 = Duration.ofMinutes(2).plusSeconds(30) Duration t2 = Duration.ofMinutes(3).plusSeconds(20) then the result would be total = Duration.ofMinutes(5).plusSeconds(50) *** and how would it be if it exceeds 59 minutes and its an hour. 2). How would I be able to add it all up on an ArrayList? does a for loop and simple add() works?
I need this in Java please: Behaviors. In the context of OOP, functions are called methods...
I need this in Java please: Behaviors. In the context of OOP, functions are called methods or behaviors because they typically do something. Most often, they read or change the values of one or more variables in the class. For example, you may have a weight variable in a class, and a method called gainWeight( ) that increases the weight variable by a certain amount. For this part of the lab, create class KoalaBear that has a weight attribute (in...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have three conversion methods: toCelcius(), toKelvin and toFahrenheit(). These methods will return a Temperature in those three scales equal to this temperature. Note that the value of this is not changed int these coversions. In addition to these conversion methods the class will have add(Temperature), subtract(Temperature), multiply(Temperature) and divide(Temperature). These four methods all return a temperature equalled to the respective operation. Note that the this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT