Question: 6.5 Write the declaration for a class named C that declares: (1) a private int instance variable named mX ; (2) a private int class

6.5 Write the declaration for a class named C that declares: (1) a private int instance variable named mX; (2) a private int class variable named mY initialized to 0; (3) a private int class constant named A which is equivalent to 100; (4) a public int class constant named B which is equivalent to 200; (5) public accessor and mutator methods for mX named getX() and setX(); (6) public accessor and mutator methods for mY named getY() and setY(); (7) a constructor that has one int input parameter named pX which calls setX() to initialize mX to pX; (8) a default constructor that calls C(int) to initialize mX to -1.

6.6 Continuing with the previous exercise, write the declaration for a class named Main that implements the main() method. Within main() suppose we wish to instantiate a C object named cObj1 calling the default constructor. Write the code to do this.

6.7 Continuing, write the code to instantiate another C object named cObj2 calling the second constructor to initialize the mX instance variable to 10.

6.8 Continuing, within main(), are the following statements legal, i.e., do they compile? If so, explain what happens when

the statement is executed. If not, explain why the statement is illegal.

(a) int a1 = C.mX;

(b) int a2 = C.mY;

(c) int a3 = C.A;

(d) int a4 = C.B;

(e) cObj1.C(20);

(f) int a5 = cObj1.getX();

(g) cObj1.setX(20);

(h) cObj2.setX(cObj1.getX());

(i) int a6 = C.getX();

(j) C.setX(20);

(k) int a7 = cObj1.getY();

(l) cObj1.setY(20);

(m) int a8 = C.getY();

(n) C.setY(20);

6.9 Continuing, suppose we add these two methods to the declaration of class C. For each assignment statement, if it is legal (compiles) explain what happens when the statement is executed. For each assignment statement that is illegal (syntax error) explain why the code is illegal.

 public void f() { mX = 0; 

mY = 0; }

 public static void g() { mX = 0; 

mY = 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!