Skip to main content\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
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
Name
Data Type
public / private / final
-
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
1.
Q19: Fill in Blank A.
String
int
double
void
return
2.
Q20: Fill in Blank B.
return
String
double
getTitle
setTitle
3.
Q21: Fill in Blank C.
return
String
title
getTitle
setTitle
4.
Q22: Fill in Blank D.
public
private
artist
getArtist
setArtist
5.
Q23: Fill in Blank E.
public
private
artist
getArtist
setArtist
6.
Q24: Fill in Blank F.
return
String
double
getArtist
setArtist
7.
Q25: Fill in Blank G.
String
int
double
void
return
8.
Q26: Fill in Blank H.
return
String
double
getLength
setLength
9.
Q27: Fill in Blank I.
return
String
length
getLength
setLength
10.
Q28: Fill in Blank J.
String
int
double
void
return
11.
Q29: Fill in Blank K.
setTitle
getTitle
void
return
n
12.
Q30: Fill in Blank L.
public
private
artist
getArtist
setArtist
13.
Q31: Fill in Blank M.
double
String
int
return
void
14.
Q32: Fill in Blank N.
setArtist
getArtist
void
return
n
15.
Q33: Fill in Blank O.
String
int
double
void
return
16.
Q34: Fill in Blank P.
double
String
int
return
void
17.
Q35: Fill in Blank Q.
return
length
double
getLength
setLength
You have attempted
of
activities on this page.