Skip to main content\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 10.8 Assessment: Writing Classes 1
Subgoals for Writing a Class.
Name it
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)
-
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
Exercises Exercises
1.
Q1: Consider the following class defintion.
/* 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." );
}
}
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;
}
}
Identify all the constructors from the given class definition above. Choose all that apply.
public Coins( )
public Coins(int newQuarters, int newDimes, int newNickels, int newPennies)
public double total( )
public double convert( double rate )
public NewCoins()
2.
Q2: Consider the following class definition.
/* 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." );
}
}
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;
}
}
Identify the accessor/getter methods.
Coins
getQuarters, getDimes, getNickels, getPennies
setQuarters, setDimes, setNickels, setPennies
moneyFromQuarters, moneyFromDimes, moneyFromNickels, moneyFromPennies
total, convert
3.
Q3: Consider the following class definition.
/* 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." );
}
}
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;
}
}
Identify the mutator/setter methods.
Coins
getQuarters, getDimes, getNickels, getPennies
setQuarters, setDimes, setNickels, setPennies
moneyFromQuarters, moneyFromDimes, moneyFromNickels, moneyFromPennies
total, convert
4.
Q4: Consider the following class definition.
/* 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." );
}
}
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;
}
}
Which of the choices lists attributes
of the given class?
quarters, dimes, nickels, pennies
getQuarters, getDimes, getNickels, getPennies
setQuarters, setDimes, setNickels, setPennies
moneyFromQuarters, moneyFromDimes, moneyFromNickels, moneyFromPennies
total, convert
You have attempted
of
activities on this page.