6.5 Write the declaration for a class named C that declares: (1) a private int i
ID: 3876598 • Letter: 6
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 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.
mY = 0; }
mY = 0; }
Explanation / Answer
6.5)
public classC
{
public static final int A= 100;
public static final int B= 200;
private int mX;
private int mY;
public C()
{
mX= -1;
}
public C(int pX)
{
setX(pX);
}
public int getX()
{
return mX;
}
public int getY()
{
return mY;
}
public void setX(int pX)
{
mX= pX;
}
public void setY(int pY)
{
mY= pY;
}
}
6.6)
public static void main(String[] args)
{
C cObj1=newC();
}
6.7)
public static void main(String[] args)
{
C cObj1 = new C();
C cObj2= new C(10);
}
6.8)
a. It will not compile since mX is private so it cannot access outside the class.
b. It will not compile since mY is private so it cannot access outside the class.
c. It will compile and a3 =100 since it is public .
d. It will compile and a4 =200 since it is public .
e. Method is undefined and it will not compile.
f. It will compile and a5 =-1;
g. It will set the value of mX to 20
h. It will set the value of mX to -1
i. It will not compile since we cannot access class method by by means of Class name .
j. It will not compile since setX is class method so it is not probable to access like that.
k. Compile and a7 = 0;
l. mY =20
m. It will not compile since getY is class method not static method.
n. Not compile since setY is class method not static method
6.9)
Function f() is legal and it compile perfectly .When it implemented it will simply set mX=0 and mY=0;
While g() will not compile since we cannot use non-static member in static methods
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.