Skip to main content

Section 10.12 Assessment: Writing Classes 2

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: Considering the following incomplete class definition, which of the following constructors can be added to the SomeClass class without causing a compiler error?
      public class SomeClass {
          public SomeClass(int first ) {
              /* implementation not shown */ }
      
          public SomeClass(int first, int second) {
              /* implementation not shown */ }
      
          public someClass(int first, String second) {
              /* implementation not shown */ }
      } // end SomeClass
      
      1. public SomeClass( ) …
      2. public SomeClass(String first, int second) …
      3. public SomeClass(String first) …
    • I only
    • I and II only
    • I and III only
    • II and III only
    • I, II and III

    2.

      Q2: Considering the following class declaration:
      public class SomeClass {
         private int num;
      
         SomeClass (int n) {
            num = n;
         }
      
         public void increment (int more) {
            num = num + more;
         }
      
         public int getNum() {
            return num;
         }
      }
      
      The following code segment appears in another class. What is the resulting output?
      SomeClass one = new SomeClass(100);
      SomeClass two = new SomeClass(100);
      SomeClass three = one;
      
      one.increment(200);
      System.out.println("d d d", one.getNum(), two.getNum(), three.getNum());
      
    • 100 100 100
    • 300 100 100
    • 300 100 300
    • 300 300 100
    • 300 300 300

    3.

      Q3: Consider a class with the following instance variables and incomplete method updateAge. Method updateAge should update the instance attributes based on the parameter extraMonths, which represents the number of months to be added to the age. Which of the following code segments could be used to replace /* body of updateAge */ so that the method will work as intended?
      private int years;  // age of item in years
      private int months; // age of item in months, 0 <= months <= 11
      		   // example, item can be 1 year 3 months old 
      
      public void updateAge (int extraMonths) {  //extraMonths >= 0
         /* body of updateAge */
      }
      
      Figure 10.12.1.
    • I only
    • II only
    • III only
    • II and III only
    • I, II and III

    4.

      Q4: Consider a class with the following instance attributes and partial method declaration. The class should represent student information.
      private int assignmentCompleted;
      private double testAverage;
      
      public boolean isPassing( ) {
         /* body of isPassing */
      }
      
      A student can pass a programming course if at least one of the following conditions is met:
      • The student has a test average that is greater than or equal to 90.
      • The student has a test average that is greater than or equal to 75 and has at least 4 completed assignments.
      Which of the following code segments could be used to replace /* body of isPassing */ so that the method will work as intended?
      Figure 10.12.2.
    • I only
    • II only
    • III only
    • II and III only
    • I, II and III
You have attempted of activities on this page.