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 9.7 WrClasses-WE3-P1
Subgoals for Writing a Class.
-
Pick a name for the class, is usually a noun
-
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)
-
-
-
-
-
Logic - initialize all variables
-
Repeat as needed, adding parameters
-
Create 1 accessor and 1 mutator behaviors per attribute
-
-
-
-
Return type same data type as attribute
-
-
-
-
-
-
-
Parameter is same data type as attribute
-
Logic validates input parameter and sets attribute value
-
-
-
-
-
Logic - convert needed attributes to a format that can be printed
-
-
-
-
Parameter - instance of the class
-
Logic - compare attributes for equity
-
Create additional methods as needed
Subsection 9.7.1 Classes-WE3-P1
Consider the SongType class you began in an earlier exercise, as illustrated in the following UML diagram.
Figure 9.7.1.
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.
-
String
-
Correct
-
int
-
Incorrect
-
double
-
Incorrect
-
void
-
Incorrect
-
return
-
Incorrect
2.
-
return
-
Correct
-
String
-
Incorrect
-
double
-
Incorrect
-
getTitle
-
Incorrect
-
setTitle
-
Incorrect
3.
-
return
-
Incorrect
-
String
-
Incorrect
-
title
-
Correct
-
getTitle
-
Incorrect
-
setTitle
-
Incorrect
4.
-
public
-
Correct
-
private
-
Incorrect
-
artist
-
Incorrect
-
getArtist
-
Incorrect
-
setArtist
-
Incorrect
5.
-
public
-
Incorrect
-
private
-
Incorrect
-
artist
-
Incorrect
-
getArtist
-
Correct
-
setArtist
-
Incorrect
6.
-
return
-
Correct
-
String
-
Incorrect
-
double
-
Incorrect
-
getArtist
-
Incorrect
-
setArtist
-
Incorrect
7.
-
String
-
Incorrect
-
int
-
Incorrect
-
double
-
Correct
-
void
-
Incorrect
-
return
-
Incorrect
8.
-
return
-
Correct
-
String
-
Incorrect
-
double
-
Incorrect
-
getLength
-
Incorrect
-
setLength
-
Incorrect
9.
-
return
-
Incorrect
-
String
-
Incorrect
-
length
-
Correct
-
getLength
-
Incorrect
-
setLength
-
Incorrect
10.
-
String
-
Incorrect
-
int
-
Incorrect
-
double
-
Incorrect
-
void
-
Correct
-
return
-
Incorrect
11.
-
setTitle
-
Incorrect
-
getTitle
-
Incorrect
-
void
-
Incorrect
-
return
-
Incorrect
-
n
-
Correct
12.
-
public
-
Correct
-
private
-
Incorrect
-
artist
-
Incorrect
-
getArtist
-
Incorrect
-
setArtist
-
Incorrect
13.
-
double
-
Incorrect
-
String
-
Correct
-
int
-
Incorrect
-
return
-
Incorrect
-
void
-
Incorrect
14.
-
setArtist
-
Incorrect
-
getArtist
-
Incorrect
-
void
-
Incorrect
-
return
-
Incorrect
-
n
-
Correct
15.
-
String
-
Incorrect
-
int
-
Incorrect
-
double
-
Incorrect
-
void
-
Correct
-
return
-
Incorrect
16.
-
double
-
Correct
-
String
-
Incorrect
-
int
-
Incorrect
-
return
-
Incorrect
-
void
-
Incorrect
17.
-
return
-
Incorrect
-
length
-
Correct
-
double
-
Incorrect
-
getLength
-
Incorrect
-
setLength
-
Incorrect
You have attempted
of
activities on this page.