public class Pitcher {
public static final int INNINGS_PER_GAME = 9;
private double inningsPitched;
private int runsScored;
private int wins;
private int losses;
public Pitcher() {
// Empty constructor, doesn't do anything.
}
public Pitcher(double inningsPitched, int runsScored, int wins, int losses) {
this.inningsPitched = inningsPitched;
this.runsScored = runsScored;
this.wins = wins;
this.losses = losses;
}
public Pitcher(double inningsPitched, int runsScored) {
this.inningsPitched = inningsPitched;
this.runsScored = runsScored;
}
public double ERA() {
if (this.inningsPitched > 0) {
return this.runsScored / this.inningsPitched * INNINGS_PER_GAME;
}
return 0.0;
}
// Accessor methods
public double inningsPitched() {
return this.inningsPitched;
}
public int runsScored() {
return this.runsScored;
}
public int wins() {
return this.wins;
}
public int losses() {
return this.losses;
}
// Mutator method
public void incrementWins() {
this.wins++;
}
}
Why do you think we don't use this
when we refer to INNINGS_PER_GAME
?
Why do you think we don't use this
when we refer to INNINGS_PER_GAME
?
This is an instance method. Where previously the ERA
function was
written in a separate PitcherUtil
class, and took a Pitcher
as a parameter,
we now write the ERA
function right here in the Pitcher
class
itself.
Notice that the method does not take a Pitcher
as a parameter.
As an instance member of the Pitcher
class, it already has access to
the Pitcher
object to which it belongs. It can access the object using
the this
keyword.
If you prefer, you can think of instance methods as implicitly taking the
calling object (the this
object) as a parameter.
This is an instance method. Where previously the ERA
function was
written in a separate PitcherUtil
class, and took a Pitcher
as a parameter,
we now write the ERA
function right here in the Pitcher
class
itself.
Notice that the method does not take a Pitcher
as a parameter.
As an instance member of the Pitcher
class, it already has access to
the Pitcher
object to which it belongs. It can access the object using
the this
keyword.
If you prefer, you can think of instance methods as implicitly taking the
calling object (the this
object) as a parameter.
Recall from the previous lesson that the this
keyword is a reference
to the object that is calling this instance method.
In this case, we could omit the this
keyword, and just write inningsPitched
, since there
is no other inningsPitched
variable in scope. But it is always more clear to use the this
keyword.
Recall from the previous lesson that the this
keyword is a reference
to the object that is calling this instance method.
In this case, we could omit the this
keyword, and just write inningsPitched
, since there
is no other inningsPitched
variable in scope. But it is always more clear to use the this
keyword.
This is an alternative ("overloaded") constructor. It takes inputs for all of the instance variables.
This is an alternative ("overloaded") constructor. It takes inputs for all of the instance variables.
This is yet another constructor, which only takes inputs for
the inningsPitched
and runsScored
instance variables.
We do this when we are okay with the default values of the other instance variables.
In this case, we are okay with the wins
and losses
defaulting
to 0 to begin with.
This is yet another constructor, which only takes inputs for
the inningsPitched
and runsScored
instance variables.
We do this when we are okay with the default values of the other instance variables.
In this case, we are okay with the wins
and losses
defaulting
to 0 to begin with.
Notice that we have made these instance variables private
. See
the previous lesson's notes about encapsulation for a reminder of why
we do this.
Notice that we have made these instance variables private
. See
the previous lesson's notes about encapsulation for a reminder of why
we do this.
This is commonly referred to as the "default constructor". It does not take any parameters. What do you suppose are the values of this Pitcher's instance variables, if it is created using this constructor?
This is commonly referred to as the "default constructor". It does not take any parameters. What do you suppose are the values of this Pitcher's instance variables, if it is created using this constructor?
Convention in Java is to name static final variables (i.e., constants) using upper-case letters and underscores.
Convention in Java is to name static final variables (i.e., constants) using upper-case letters and underscores.
This class represents a baseball pitcher. The pitcher has a number of innings pitched, a number of runs scored, a number of wins, and a number of losses. The pitcher is also able to compute its own "earned run average" (ERA).
This class represents a baseball pitcher. The pitcher has a number of innings pitched, a number of runs scored, a number of wins, and a number of losses. The pitcher is also able to compute its own "earned run average" (ERA).
This variable is declared to be static
, which means that it does
not belong to any one instance of the class. It belongs to the class itself.
Here, we do this because all Pitcher
s have the same number of innings per game — that
value is shared by all Pitcher
s. We could give each Pitcher
an instance variable
whose value is always 9
, but that would be a waste to duplicate that data for every single
instance. Moreover, if the game of baseball changed, and we now played 10 innings, we would
need to change the value for a ton of Pitcher
objects, instead of changing it once.
This variable is declared to be static
, which means that it does
not belong to any one instance of the class. It belongs to the class itself.
Here, we do this because all Pitcher
s have the same number of innings per game — that
value is shared by all Pitcher
s. We could give each Pitcher
an instance variable
whose value is always 9
, but that would be a waste to duplicate that data for every single
instance. Moreover, if the game of baseball changed, and we now played 10 innings, we would
need to change the value for a ton of Pitcher
objects, instead of changing it once.