Skip to main content
Contents
Dark Mode Prev Up Next Scratch ActiveCode Profile
\(
\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.2 WrClasses-WE1-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 10.2.1
Consider writing a class to represent a song on your phone or music player. You need to define the appropriate attributes (data) to store the name of the song, the artist, and length. All songs should be stored in the same format, which should be MP3.
Complete the following class skeleton.
public class SongType {
___A___ ___B___ title;
___C___ ___D___ artist;
___E___ ___F___ length;
___G___ ___H___ final String FORMAT = "MP3";
}
Exercises Exercises
1.
public
Incorrect
private
Correct
String
Incorrect
int
Incorrect
double
Incorrect
2.
public
Incorrect
private
Incorrect
String
Correct
int
Incorrect
double
Incorrect
3.
public
Incorrect
private
Correct
String
Incorrect
int
Incorrect
double
Incorrect
4.
public
Incorrect
private
Incorrect
String
Correct
int
Incorrect
double
Incorrect
5.
public
Incorrect
private
Correct
String
Incorrect
int
Incorrect
double
Incorrect
6.
public
Incorrect
private
Incorrect
String
Incorrect
int
Incorrect
double
Correct
7.
public
Correct
private
Incorrect
static
Incorrect
void
Incorrect
const
Incorrect
8.
public
Incorrect
private
Incorrect
static
Correct
void
Incorrect
const
Incorrect
You have attempted
of
activities on this page.