Skip to main content

Section 8.12 Assessment: Writing Classes 2

Subsection 8.12.1 Subgoals for Writing a Class 4/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

  1. Write toString method

    1. public

    2. Returns String

    3. No parameters

    4. Logic - convert needed attributes to a format that can be printed

  2. Write equals method

    1. public

    2. Returns boolean

    3. Parameter - instance of the class

    4. Logic - compare attributes for equity

  3. Create additional methods as needed

Exercises 8.12.2 Exercises

1.

    Q1: Consider the following method, which is intended to return true if at least one of the three strings s1, s2, or s2 contains the substring "art". Otherwise, the method should return false.

    public boolean containsArt (String s1, String s2, String s3) {
       String all = s1 + s2 + s3;
       return (all.indexOf("art") != -1);
    }
    

    Which of the following method calls demonstrates that the method does NOT work as intended?

  • containsArt("rattrap", "similar", "today")

  • containsArt("start", "article", "Bart")

  • containsArt("harm", "chortle", "crowbar")

  • containsArt("matriculate", "carat", "arbitrary")

  • containsArt("darkroom", "cartoon", "articulate")

2.

    Q2: Considering the following class, which of the following methods can be added to the SomeMethods class without causing a compiler error?

    public class SomeMethods {
       public void one(int first) {
          /* implementation not shown */ }
    
       public void one(int first, int second) {
          /* implementation not shown */ }
    
       public void one(int first, String second) {
          /* implementation not shown */ }
    } // end SomeMethods
    
    1. public void one(int value) …

    2. public void one(String first, int second) …

    3. public void one(int first, int second, int third) …

  • I only

  • I and II only

  • I and III only

  • II and III only

  • I, II and III

3.

    Q3: 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.printf("d d d", one.getNum(), two.getNum(), three.getNum());
    
  • 100 100 100

  • 300 100 100

  • 300 100 300

  • 300 300 100

  • 300 300 300

4.

    Q4: Considering the following instance variables and incomplete method that are part of a class that represents an item. Method updateAge is used to update the variables based on the parameter extraMonths that represents the number of months to be added to the age.

    Figure 8.12.1.
  • I only

  • II only

  • III only

  • II and III only

  • I, II and III

5.

    Q5: Considering the following instance variables and method that appear in a class representing 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 8.12.2.
  • I only

  • II only

  • III only

  • II and III only

  • I, II and III

You have attempted of activities on this page.