Differentiate class-level static vs. instance/object-level variables
Differentiate class-level static vs. instance/object behaviors/methods
Define instance variables (that you want to be interrelated)
Name
Data Type
private
Define class variables static as needed
Name
Data Type
public / private / final
Create constructor (behavior) that creates initial state of object
Overloaded constructor (with as many parameters)
public
Same name as class
No return type
Default - no parameters
Logic - initialize all variables
Repeat as needed, adding parameters
Create 1 accessor and 1 mutator behaviors per attribute
Accessors
Name is get_<attr_name>
Public
Return type same data type as attribute
No parameters
Logic - return value
Mutators
Name is set_<attr_name>
Public
Return type is void
Parameter is same data type as attribute
Logic validates input parameter and sets attribute value
Write toString method
public
Returns String
No parameters
Logic - convert needed attributes to a format that can be printed
Write equals method
public
Returns boolean
Parameter - instance of the class
Logic - compare attributes for equity
Create additional methods as needed
Subsection10.11.1
ExercisesExercises
1.
Q51: Identify all constructors.
/* Coins Class */
public class Coins {
private int quarters;private int dimes;private int nickels;private int pennies;
public Coins(int newQuarters, int newDimes, int newNickels, int newPennies) {
setQuarters( newQuarters );
setDimes( newDimes );
setNickels( newNickels );
setPennies( newPennies );
}
public Coins( ) {
setQuarters( 0 );
setDimes( 0 );
setNickels( 0 );
setPennies( 0 );
}
public int getQuarters( ) {
return quarters;
}
public void setQuarters(int newQuarters) {
if ( newQuarters >= 0 )
quarters = newQuarters;
else {
System.out.println( "The number of quarters cannot be negative." );
System.out.println( "Value not changed." );
}
}
public int getDimes( ) {
return dimes;
}
public void setDimes(int newDimes) {
if ( newDimes >= 0 )
dimes = newDimes;
else {
System.out.println( "The number of dimes cannot be negative." );
System.out.println( "Value not changed." );
}
}
public int getNickels( ) {
return nickels;
}
public void setNickels(int newNickels) {
if ( newNickels >= 0 )
nickels = newNickels;
else {
System.out.println( "The number of nickels cannot be negative." );
System.out.println( "Value not changed." );
}
}
public int getPennies( ) {
return pennies;
}
public void setPennies(int newPennies) {
if ( newPennies >= 0 )
pennies = newPennies;
else {
System.out.println( "The number of pennies cannot be negative." );
System.out.println( "Value not changed." );
}
}
public String toString( ) {
return( "quarters: " + quarters + "; dimes: " + dimes
+ "; nickels: " + nickels + "; pennies:" + pennies );
}
public boolean equals( Coins c ) {
return ( quarters == c.quarters&& dimes == c.dimes
&& nickels == c.nickels&& pennies == c.pennies );
}
private double moneyFromQuarters( ) {
return ( quarters * .25 );
}
private double moneyFromDimes( ) {
return ( dimes * .1 );
}
private double moneyFromNickels( ) {
return ( nickels * .05 );
}
private double moneyFromPennies( ) {
return ( pennies * .01 );
}
public double total( ) {
return moneyFromQuarters +
moneyFromDimes +
moneyFromNickels +
moneyFromPennies;
}
public double convert(double rate) {
return total * rate;
}
}
2.
Q52: Identify all accessor/getter methods.
/* Coins Class */
public class Coins {
private int quarters;private int dimes;private int nickels;private int pennies;
public Coins(int newQuarters, int newDimes, int newNickels, int newPennies) {
setQuarters( newQuarters );
setDimes( newDimes );
setNickels( newNickels );
setPennies( newPennies );
}
public Coins( ) {
setQuarters( 0 );
setDimes( 0 );
setNickels( 0 );
setPennies( 0 );
}
public int getQuarters( ) {
return quarters;
}
public void setQuarters(int newQuarters) {
if ( newQuarters >= 0 )
quarters = newQuarters;
else {
System.out.println( "The number of quarters cannot be negative." );
System.out.println( "Value not changed." );
}
}
public int getDimes( ) {
return dimes;
}
public void setDimes(int newDimes) {
if ( newDimes >= 0 )
dimes = newDimes;
else {
System.out.println( "The number of dimes cannot be negative." );
System.out.println( "Value not changed." );
}
}
public int getNickels( ) {
return nickels;
}
public void setNickels(int newNickels) {
if ( newNickels >= 0 )
nickels = newNickels;
else {
System.out.println( "The number of nickels cannot be negative." );
System.out.println( "Value not changed." );
}
}
public int getPennies( ) {
return pennies;
}
public void setPennies(int newPennies) {
if ( newPennies >= 0 )
pennies = newPennies;
else {
System.out.println( "The number of pennies cannot be negative." );
System.out.println( "Value not changed." );
}
}
public String toString( ) {
return( "quarters: " + quarters + "; dimes: " + dimes
+ "; nickels: " + nickels + "; pennies:" + pennies );
}
public boolean equals( Coins c ) {
return ( quarters == c.quarters&& dimes == c.dimes
&& nickels == c.nickels&& pennies == c.pennies );
}
private double moneyFromQuarters( ) {
return ( quarters * .25 );
}
private double moneyFromDimes( ) {
return ( dimes * .1 );
}
private double moneyFromNickels( ) {
return ( nickels * .05 );
}
private double moneyFromPennies( ) {
return ( pennies * .01 );
}
public double total( ) {
return moneyFromQuarters +
moneyFromDimes +
moneyFromNickels +
moneyFromPennies;
}
public double convert(double rate) {
return total * rate;
}
}
3.
Q53: Identify all mutator/setter methods.
/* Coins Class */
public class Coins {
private int quarters;private int dimes;private int nickels;private int pennies;
public Coins(int newQuarters, int newDimes, int newNickels, int newPennies) {
setQuarters( newQuarters );
setDimes( newDimes );
setNickels( newNickels );
setPennies( newPennies );
}
public Coins( ) {
setQuarters( 0 );
setDimes( 0 );
setNickels( 0 );
setPennies( 0 );
}
public int getQuarters( ) {
return quarters;
}
public void setQuarters(int newQuarters) {
if ( newQuarters >= 0 )
quarters = newQuarters;
else {
System.out.println( "The number of quarters cannot be negative." );
System.out.println( "Value not changed." );
}
}
public int getDimes( ) {
return dimes;
}
public void setDimes(int newDimes) {
if ( newDimes >= 0 )
dimes = newDimes;
else {
System.out.println( "The number of dimes cannot be negative." );
System.out.println( "Value not changed." );
}
}
public int getNickels( ) {
return nickels;
}
public void setNickels(int newNickels) {
if ( newNickels >= 0 )
nickels = newNickels;
else {
System.out.println( "The number of nickels cannot be negative." );
System.out.println( "Value not changed." );
}
}
public int getPennies( ) {
return pennies;
}
public void setPennies(int newPennies) {
if ( newPennies >= 0 )
pennies = newPennies;
else {
System.out.println( "The number of pennies cannot be negative." );
System.out.println( "Value not changed." );
}
}
public String toString( ) {
return( "quarters: " + quarters + "; dimes: " + dimes
+ "; nickels: " + nickels + "; pennies:" + pennies );
}
public boolean equals( Coins c ) {
return ( quarters == c.quarters&& dimes == c.dimes
&& nickels == c.nickels&& pennies == c.pennies );
}
private double moneyFromQuarters( ) {
return ( quarters * .25 );
}
private double moneyFromDimes( ) {
return ( dimes * .1 );
}
private double moneyFromNickels( ) {
return ( nickels * .05 );
}
private double moneyFromPennies( ) {
return ( pennies * .01 );
}
public double total( ) {
return moneyFromQuarters +
moneyFromDimes +
moneyFromNickels +
moneyFromPennies;
}
public double convert(double rate) {
return total * rate;
}
}
4.
Q54: Identify the attributes.
/* Coins Class */
public class Coins {
private int quarters;private int dimes;private int nickels;private int pennies;
public Coins(int newQuarters, int newDimes, int newNickels, int newPennies) {
setQuarters( newQuarters );
setDimes( newDimes );
setNickels( newNickels );
setPennies( newPennies );
}
public Coins( ) {
setQuarters( 0 );
setDimes( 0 );
setNickels( 0 );
setPennies( 0 );
}
public int getQuarters( ) {
return quarters;
}
public void setQuarters(int newQuarters) {
if ( newQuarters >= 0 )
quarters = newQuarters;
else {
System.out.println( "The number of quarters cannot be negative." );
System.out.println( "Value not changed." );
}
}
public int getDimes( ) {
return dimes;
}
public void setDimes(int newDimes) {
if ( newDimes >= 0 )
dimes = newDimes;
else {
System.out.println( "The number of dimes cannot be negative." );
System.out.println( "Value not changed." );
}
}
public int getNickels( ) {
return nickels;
}
public void setNickels(int newNickels) {
if ( newNickels >= 0 )
nickels = newNickels;
else {
System.out.println( "The number of nickels cannot be negative." );
System.out.println( "Value not changed." );
}
}
public int getPennies( ) {
return pennies;
}
public void setPennies(int newPennies) {
if ( newPennies >= 0 )
pennies = newPennies;
else {
System.out.println( "The number of pennies cannot be negative." );
System.out.println( "Value not changed." );
}
}
public String toString( ) {
return( "quarters: " + quarters + "; dimes: " + dimes
+ "; nickels: " + nickels + "; pennies:" + pennies );
}
public boolean equals( Coins c ) {
return ( quarters == c.quarters&& dimes == c.dimes
&& nickels == c.nickels&& pennies == c.pennies );
}
private double moneyFromQuarters( ) {
return ( quarters * .25 );
}
private double moneyFromDimes( ) {
return ( dimes * .1 );
}
private double moneyFromNickels( ) {
return ( nickels * .05 );
}
private double moneyFromPennies( ) {
return ( pennies * .01 );
}
public double total( ) {
return moneyFromQuarters +
moneyFromDimes +
moneyFromNickels +
moneyFromPennies;
}
public double convert(double rate) {
return total * rate;
}
}
5.
Q55: Identify the helper or auxiliary methods. Often any method that doesn’t fall into another category (constructor, accessor, mutator, toString, equals) is considered a helper or auxiliary method.
/* Coins Class */
public class Coins {
private int quarters;private int dimes;private int nickels;private int pennies;
public Coins(int newQuarters, int newDimes, int newNickels, int newPennies) {
setQuarters( newQuarters );
setDimes( newDimes );
setNickels( newNickels );
setPennies( newPennies );
}
public Coins( ) {
setQuarters( 0 );
setDimes( 0 );
setNickels( 0 );
setPennies( 0 );
}
public int getQuarters( ) {
return quarters;
}
public void setQuarters(int newQuarters) {
if ( newQuarters >= 0 )
quarters = newQuarters;
else {
System.out.println( "The number of quarters cannot be negative." );
System.out.println( "Value not changed." );
}
}
public int getDimes( ) {
return dimes;
}
public void setDimes(int newDimes) {
if ( newDimes >= 0 )
dimes = newDimes;
else {
System.out.println( "The number of dimes cannot be negative." );
System.out.println( "Value not changed." );
}
}
public int getNickels( ) {
return nickels;
}
public void setNickels(int newNickels) {
if ( newNickels >= 0 )
nickels = newNickels;
else {
System.out.println( "The number of nickels cannot be negative." );
System.out.println( "Value not changed." );
}
}
public int getPennies( ) {
return pennies;
}
public void setPennies(int newPennies) {
if ( newPennies >= 0 )
pennies = newPennies;
else {
System.out.println( "The number of pennies cannot be negative." );
System.out.println( "Value not changed." );
}
}
public String toString( ) {
return( "quarters: " + quarters + "; dimes: " + dimes
+ "; nickels: " + nickels + "; pennies:" + pennies );
}
public boolean equals( Coins c ) {
return ( quarters == c.quarters&& dimes == c.dimes
&& nickels == c.nickels&& pennies == c.pennies );
}
private double moneyFromQuarters( ) {
return ( quarters * .25 );
}
private double moneyFromDimes( ) {
return ( dimes * .1 );
}
private double moneyFromNickels( ) {
return ( nickels * .05 );
}
private double moneyFromPennies( ) {
return ( pennies * .01 );
}
public double total( ) {
return moneyFromQuarters +
moneyFromDimes +
moneyFromNickels +
moneyFromPennies;
}
public double convert(double rate) {
return total * rate;
}
}