Section 10.7 WrClasses-WE3-P1
Subgoals for Writing a Class.
-
Name it
-
Differentiate class-level
static
vs. instance/object-level variables -
Differentiate class-level
static
vs. instance/object behaviors/methods -
Define instance variables (that you want to be interrelated)
-
Define class variables
static
as needed -
Create constructor (behavior) that creates initial state of object
-
Overloaded constructor (with as many parameters)
-
public
-
Same name as class
-
No return type
-
Default - no parameters
-
Logic - initialize all variables
-
Repeat as needed, adding parameters
-
-
Create 1 accessor and 1 mutator behaviors per attribute
-
Accessors
-
Name is get_<attr_name>
-
Public
-
Return type same data type as attribute
-
No parameters
-
Logic - return value
-
-
Mutators
-
Name is set_<attr_name>
-
Public
-
Return type is void
-
Parameter is same data type as attribute
-
Logic validates input parameter and sets attribute value
-
-
-
Write toString method
-
public
-
Returns String
-
No parameters
-
Logic - convert needed attributes to a format that can be printed
-
-
Write equals method
-
public
-
Returns boolean
-
Parameter - instance of the class
-
Logic - compare attributes for equity
-
-
Create additional methods as needed
Subsection 10.7.1 Classes-WE3-P1
Consider the SongType class you began in an earlier exercise, as illustrated in the following UML diagram.
Fill in the blanks for the following code:
public class SongType {
//attributes declared here
//...
//getters/accessors
public ___A___ getTitle () {
___B___ ___C___;
}
___D___ String ___E___ () {
___F___ artist;
}
public ___G___ getLength () {
___H___ ___I___;
}
//setters/mutators
public ___J___ setTitle (String n) {
title = ___K___;
}
___L___ void setArtist (___M___ n) {
artist = ___N___;
}
public ___O___ setLength (___P___ n) {
if (n > 0)
___Q___ = n;
}
}
Exercises Exercises
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
You have attempted 1 of 3 activities on this page.