public class Player
{
private String firstName;
private String lastName;
private int heightInInches;
private double weightInPounds;
private boolean goalScorer;
private boolean drinksBeer;
public Player(){
}
public Player(String firstName, String lastName, int heightInInches, double weightInPounds, boolean goalScorer, boolean drinksBeer){
if(lastName != null && lastName.trim().length() > 0){
if(lastName != null && lastName.trim().length() > 0){
if(heightInInches >= 0){
if(weightInPounds >= 0){
this.firstName = firstName;
this.lastName = lastName;
this.heightInInches = heightInInches;
this.weightInPounds = weightInPounds;
this.goalScorer = goalScorer;
this.drinksBeer = drinksBeer;
}
}
}
}
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public int getHeightInInches(){
return heightInInches;
}
public double getWeightInPounds(){
return weightInPounds;
}
public boolean getGoalScorer(){
return goalScorer;
}
public boolean getDrinksBeer(){
return drinksBeer;
}
public void setFirstName(String firstName){
if(firstName != null && firstName.trim().length() > 0){
this.firstName = firstName;
}else{
System.out.println("Error. Invalid First Name.");
}
}
public void setLastName(String lastName){
if(lastName != null && lastName.trim().length() > 0){
this.lastName = lastName;
}else{
System.out.println("Error. Invalid Last Name.");
}
}
public void setHeightInInches(int heightInInches){
if(heightInInches >= 0){
this.heightInInches = heightInInches;
}else{
System.out.println("Error. Invalid Height.");
}
}
public void setWeightInPounds(double weightInPounds){
if(weightInPounds >= 0){
this.weightInPounds = weightInPounds;
}else{
System.out.println("Error. Invalid Weight.");
}
}
public void setGoalScorer(boolean goalScorer){
this.goalScorer = goalScorer;
}
public void setDrinksBeer(boolean drinksBeer){
this.drinksBeer = drinksBeer;
}
}
In the overloaded constructor, how can I call the mutator method for each field instead of using an assignment statement? and also if I call the mutator methods should I remove the if statements in the constructor? (I'm using the blueJ) I'm a beginner, so please mention if there is any other problems in my code. Thanks in advance.
All you have to do is reference the mutator method like any other method:
public Player(String firstName, String lastName, int heightInInches, double weightInPounds, boolean goalScorer, boolean drinksBeer) {
setFirstName(firstName);
setLastName(lastName);
setHeightInInches(heightInInches);
setWeightInPounds(weightInPounds);
setGoalScorer(goalScorer);
setDrinksBeer(drinksBeer);
}
Also, there's no need for those if statements, because they weren't actually doing anything useful. If you don't explicitly set any values, int
variables will default to 0
, double
will default to 0.0
, boolean
will default to false
, and String
will default to null
. So your if statements were doing things like "If the first name is null
, don't set it . . . so it'll just be null
anyway by default."
If you want to do something like forcing the height to be a positive number, you can always put that logic in the setter.