Skip to main content

Section 10.8 Assessment: Writing Classes 1

Subgoals for Writing a Class.

  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 instance variables (that you want to be interrelated)
    1. Name
    2. Data Type
    3. private
  5. Define class variables static as needed
    1. Name
    2. Data Type
    3. public / private / final
  6. Create constructor (behavior) that creates initial state of object
    1. Overloaded constructor (with as many parameters)
    2. public
    3. Same name as class
    4. No return type
    5. Default - no parameters
    6. Logic - initialize all variables
    7. 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
  8. Write toString method
    1. public
    2. Returns String
    3. No parameters
    4. Logic - convert needed attributes to a format that can be printed
  9. Write equals method
    1. public
    2. Returns boolean
    3. Parameter - instance of the class
    4. Logic - compare attributes for equity
  10. 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.