Skip to main content

Section 8.8 Assessment: Writing Classes 1

Subsection 8.8.1 Subgoals for Writing a Class 3/4

  1. Name it

  2. Differentiate class-level (static) vs. instance/object-level variables

  3. Differentiate class-level (static) vs. instance/object behaviors/methods

  4. Define class variables (static) as needed ‘

    1. Name

    2. Data Type

    3. public / private / final

  5. Define instance variables (that you want to be interrelated)

    1. Name

    2. Data Type

    3. private

  6. Create constructor (behavior) that creates initial state of object

    1. public

    2. Same name as class

    3. No return type

    4. Default - no parameters

    5. Logic - initialize all variables

    6. Repeat as needed, adding parameters

  7. Create 1 accessor and 1 mutator behaviors per attribute

  1. Accessors

    1. Name is get_<attr_name>

    2. Public

    3. Return type same data type as attribute

    4. No parameters

    5. Logic - return value

  2. Mutators

    1. Name is set_<attr_name>

    2. Public

    3. Return type is void

    4. Parameter is same data type as attribute

    5. Logic validates input parameter and sets attribute value

Exercises 8.8.2 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." );
          }
       }
    
       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;
       }
    }
    

    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." );
          }
       }
    
       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;
       }
    }
    

    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." );
          }
       }
    
       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;
       }
    }
    

    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." );
          }
       }
    
       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;
       }
    }
    

    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

5.

    Q5: Consider the following class definiton.

    /* 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;
       }
    }
    

    Identify the helper or auxiliary methods.

  • Coins

  • getQuarters, getDimes, getNickels, getPennies

  • setQuarters, setDimes, setNickels, setPennies

  • moneyFromQuarters, moneyFromDimes, moneyFromNickels, moneyFromPennies

  • total, convert

You have attempted of activities on this page.