Constant variable in class c a private variable

This preview shows page 4 - 7 out of 7 pages.

constant variable in class C. A private variable cannot be accessed using its class name in another class.
(d) int a4 = C.B;
(e) cObj1.C(20); This is a legal statement when a method is created in class C with the following heading: public void C(int i); however, it is not good practice to create a method with the same name as the class name. (f) int a5 = cObj1.getX(); This is a legal statement; it will compile. It stores the current value of cObj1’s mX into the variable a5. After execution, a5 = -1. (g) cObj1.setX(20); This is a legal statement; it will compile. After execution, cObj1’s mX is set to 20. cObj1’s mX = 20. (h) cObj2.setX(cObj1.getX()); This is a legal statement; it will compile. It will set the value of cObj2’s mX to cObj1’s mX. cObj2’s mX = 20. (i) int a6 = C.getX(); This is an illegal statement; it will not compile. The getX method is not a static method; a non-static method cannot be accessed using its class name in another class. (j) C.setX(20); This is an illegal statement and it will not compile. The setX method is not a static method. A non-static method cannot be accessed using its class name in another class. (k) int a7 = cObj1.getY(); This is a legal statement; it will compile. It stores the current value of cObj1’s mY into the variable a7. a7 = 0. (l) CObj1.setY(20); This is a legal statement; it will compile. It sets cObj1’s mY to 20. CObj1’s mY = 20. (m) int a8 = C.getY(); This is an illegal statement; it will not compile. The getY method is not a static method; a non-static method cannot be accessed using its class name in another class. (n) C.setY(20); This is an illegal statement; it will not compile. The setY method is not a static method; a non-static method cannot be accessed using its class name in another class. 7.2 On the next page is a UML class diagram that shows several classes that are related in various ways. (a) What type of class relationship, if any, exists between Course and Roster ? If there is a relationship, modify the diagram to indicate the relationship and label each end of the relationship with a multiplicity.
The class object Course has a composition relationship with the class object Roster because Course has the sole responsibility for the creation and destruction of the Roster. Course is independent of Roster; however, Roster is dependent of the Course class object. The multiplicity for Course is 1 and the multiplicity for Roster is 1. For example, this one

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture