Skip to main content

Section 4.16 ObjUse-WE7-P1: Aliasing

Subgoals for using objects (creating instances).

  1. Declare variable of appropriate class datatype.
  2. Assign to variable: keyword new, followed by class name, followed by ().
  3. Determine whether parameter(s) are appropriate (API)
    1. Number of parameters
    2. 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();
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;
You have attempted of activities on this page.