Skip to main content

Section 8.4 WrClasses-WE2-P1

Subsection 8.4.1 Subgoals for Writing a Class 2/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

Subsection 8.4.2 Classes-WE2-P1

Consider the SongType class you began in an earlier exercise, as illustrated in the following UML diagram.

Figure 8.4.1.

Exercises Exercises

1.

Put the code in the right order to complete the default constructor.

2.

Put the code in the right order to complete the specific overloaded constructor.

3.

    Q11: Which of the following is NOT true about constructors?

  • Constructors must be named the same name as the class

  • Default constructors have no parameters

  • Classes can only have a single constructor

  • Constructors must be public

  • Constructors have no return type, not even void

4.

    Q12: Two constructors are shown for the Point class below. Is this code valid?

    public class Point {
       private int x;
       private int y;
    
       public Point (int one, int two) {/*LOGIC*/}
       public Point (int a, int b) {/*LOGIC*/}
    }
    
  • valid

  • invalid

  • cannot be determined

You have attempted of activities on this page.