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....
create scientific calculator using java language with OOP rule and interfaces.
create scientific calculator using java language with OOP rule and interfaces.
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...
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- 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...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering Inheritance. Core Level Requirements (up to 6 marks) The scenario for this assignment is to design an online shopping system for a local supermarket (e.g., Europa Foods Supermarket or Wang Long Oriental Supermarket). The assignment is mostly concentrated on the product registration system. Design and draw a UML diagram, and write the code for the following classes: The first product category is a fresh...
Check errors and revise/update this java code below and update with OOp class; at least make...
Check errors and revise/update this java code below and update with OOp class; at least make two classes, use java library, validate user input, format and create UML/Pseudocode and flowchart for the code. import java.util.Scanner; public class TestScore {    public static void main(String[] args) {        String firstName;        String lastName;        int numTest;        int i;        int score;        double totalScore;        double avgScore;        String grade;        Scanner input = new Scanner(System.in);        System.out.println("Enter First Name");        firstName = input.nextLine();        System.out.println("Enter Last Name");        lastName = input.nextLine();        System.out.println("How many...
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...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑ Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. ☑ Add a method addToCollection. In this method,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT