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 4.16 ObjUse-WE7-P1: Aliasing
Subgoals for using objects (creating instances).
-
Declare variable of appropriate class datatype.
-
Assign to variable: keyword new, followed by class name, followed by ().
-
Determine whether parameter(s) are appropriate (API)
-
-
Data types of the parameters
Subsection 4.16.1
Exercises Exercises
1.
Q20: What is the value of
gamma after this code segment is run?
Widget alpha = new Widget(5);
Widget beta = new Widget(15);
Widget delta = alpha;
delta.setBibs(25);
beta = delta;
beta.setBibs(30);
int gamma = alpha.getBibs();
5
Incorrect
15
Incorrect
25
Incorrect
30
Correct
2.
Q21: What are the values of
gamma and
zeta after this code segment is run?
Widget alpha = new Widget(0);
Widget beta = new Widget(24);
Widget delta = alpha;
delta.setBibs(40);
beta = delta;
beta.setBibs(30);
delta = new Widget(50);
int gamma = alpha.getBibs();
int zeta = delta.getBibs();
gamma = 0; zeta = 0;
Incorrect
gamma = 24; zeta = 30;
Incorrect
gamma = 30; zeta = 50;
Correct
gamma = 40; zeta = 50;
Incorrect
3.
Q22: What is the value of
gamma after this code segment is run?
Widget alpha = new Widget(55);
Widget beta = new Widget(70);
alpha.setBibs(46);
Widget delta = alpha;
beta.setBibs(79);
delta = new Widget(50);
int gamma = alpha.getBibs() + 10;
56
Correct
60
Incorrect
65
Incorrect
89
Incorrect
You have attempted
of
activities on this page.