Access over 35 million academic & study documents

6 1 Explain how an instance method differs from a class method (stat

Content type
User Generated
Showing Page:
1/7
6.1 Explain how an instance method differs from a class
method (static method).
6.2 Explain what happens if we write a class named C but
do not implement any constructors in C.
6.3 (a) In a static method, it is easy to differentiate
between calls to instance methods and calls to static
methods. How do you tell them apart? (b) Why is it not as
easy for methods that are called from an instance method?
6.4 Explain what happens when this application runs and
why.
public class C { private int x; private String s; public static
void main(String[] pArgs) { new C(); }
public C() { x = s.length();
System.out.println(\"x = \" + x); } }
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.

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/7
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; }
Solution

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/7

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 7 pages?
Access Now
Unformatted Attachment Preview
6.1 Explain how an instance method differs from a class method (static method). 6.2 Explain what happens if we write a class named C but do not implement any constructors in C. 6.3 (a) In a static method, it is easy to differentiate between calls to instance methods and calls to static methods. How do you tell them apart? (b) Why is it not as easy for methods that are called from an instance method? 6.4 Explain what happens when this application runs and why. public class C { private int x; private St ring s; public static void main(String[] pArgs) { new C(); } public C() { x = s.length(); System.out.println(\"x = \" + x); } } 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 t ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Indeed
4.5
Sitejabber
4.4