Question: Exercise on Access Modifiers: Objectives: Design and implement a Java application that tests the visibility of the members of the Alpha class as shown in

Exercise on Access Modifiers:

Exercise on Access Modifiers: Objectives: Design and implement a Java application that

Objectives: Design and implement a Java application that tests the visibility of the members of the Alpha class as shown in the table in Figure B.

1. Draw a UML class diagram to show the name, attributes and methods of each of the classes in your application. Include appropriate associations between the classes. Hand in the class diagram. 2. Implement your design by converting them into Java classes. Your class definitions should exhibit good object-oriented programming practice. such as data hiding using set( ) and get( ) methods, proper access modifiers, etc.

I have my java programs, Here is my main.java program:

package referencingobjectsofsameclassdemo;

public class Main { // intVariable is declared as package-private (without modifier) int intVariable;

public void setIntVariable (int value) { intVariable = value; }

public int getIntVariable () { return intVariable; }

public int testMethod (int v1, int v2) { Main objMain = new Main(); objMain.intVariable = v1-v2; return objMain.intVariable; } }

Here is my main2.java program:

/* Purposes: * A. This application tests Java's access modifiers for instance variables. * Both private and package-private are tested. * B. It also tests the cases where an object references another object of the same class. * * To test the application: Place both classes (Main and Main2) in the same folder/package. */ // When an object of a class has a reference // to another object of the same class, the first object can access all // the second object's data and methods, including those that are private. // //Cases and Rationale: // //case 1: The second object is one of the instance variables of the first object. // Q: Would doing so create infinite references to objects of the same type? // A: No, because the referenced object is initially null. // //case 2: The second object is a local variable in one of the methods of the first object.

package referencingobjectsofsameclassdemo;

public class Main2 { private int intPrivate; private static int numberOfMain2Objects; private Main2 objSelfReference; //A reference to an object of the same class (case 1)

public void setObjSelfReference (Main2 obj) { objSelfReference = obj; }

public Main2 getObjSelfReference () { return objSelfReference; } public static int getNumberOfMain2Objects(){ return numberOfMain2Objects; } public void testObjSelfReference (int v) { //objSelfReference is an instance variable that is of the same class as the class where testObjSelfReference is defined objSelfReference = new Main2 (); objSelfReference.intPrivate = v; System.out.printf("in testObjSelfReference: objSelfReference.intPrivate = %d ", objSelfReference.intPrivate); }

public Main2() { // Use the class variable to keep track of the number of Main2 objects this.numberOfMain2Objects++; }

public void setIntPrivate (int value) { intPrivate = value; }

public int getIntPrivate () { return intPrivate; }

public void testMethod (int v1, int v2) { Main objMain = new Main(); objMain.intVariable = v1-v2; System.out.printf("objMain in testMethod(%d, %d) = %d ", v1, v2, objMain.intVariable);

Main2 objMain2 = new Main2(); // case 2 objMain2.intPrivate = v1-v2; System.out.printf("objMain2 in testMethod(%d, %d) = %d ", v1, v2, objMain2.intPrivate); } public static void main(String[] args) {

Main objMain = new Main(); objMain.setIntVariable(100); objMain.intVariable = 200; // main can access intVariable in objMain System.out.println(" >>>>>>>> main() in Main2:"); System.out.printf(" objMain: %s ", objMain.toString()); System.out.printf("objMain.intVariable: %s ", objMain.intVariable);

Main2 obj = new Main2(); obj.setIntPrivate(100); obj.intPrivate = 222; // main can access intPrivate in obj System.out.printf(" >>>>>>>> obj: %s ", obj.toString()); System.out.printf("obj.intPrivate: %s ", obj.intPrivate); System.out.printf("Main2.numberOfMain2Objects: %d ", Main2.numberOfMain2Objects);

obj.testMethod(200, 50); System.out.printf("Main2.numberOfMain2Objects: %d ", Main2.numberOfMain2Objects);

obj.testObjSelfReference(777); System.out.printf("in main: obj.objSelfReference.intPrivate = %d ", obj.objSelfReference.intPrivate); }

}

As discussed in class, there exist four different access modifiers for instance variables and methods in a Java class. The purpose of this lab is to test and verify the various access modifiers. A sample Java application: As illustrated in Figure B, the classes Alpha and Beta belong to Package One, while classes AlphaSub and Gamma are part of Package Two. Class AlphaSub is a subclass of class Alpha. Let's look at a collection of classes and see how access levels affect visibility. The following figure shows the four classes in this example and how they are related Package One Package Two SubclassAlphaSub Alpha Subclass Beta Gamma Classes and Packages of the Example Used to llustrate Access Levels The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them Visibility Modifier Alpha Beta Alphasub Gamma publicYYY rotectedY YY no modifier Y Y N privateY NN Figure B. An illustration of Java's access modifiers source: http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol html)

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!