8.2.1. WrClasses-WE2-P1¶
Subgoals for Writing a Class 2/4
Name it
Differentiate class-level (static) vs. instance/object-level variables
Differentiate class-level (static) vs. instance/object behaviors/methods
Define class variables (static) as needed ‘
Name
Data Type
public / private / final
Define instance variables (that you want to be interrelated)
Name
Data Type
private
Create constructor (behavior) that creates initial state of object
public
Same name as class
No return type
Default - no parameters
Logic - initialize all variables
Repeat as needed, adding parameters
Classes-WE2-P1
Consider the SongType class you began in an earlier exercise, as illustrated in the following UML diagram.
Put the code in the right order to complete the default constructor.
Put the code in the right order to complete the specific overloaded constructor.
- 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
Q11: Which of the following is NOT true about constructors?
- valid
- invalid
- cannot be determined
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*/}
}