CIS 121 February 10, 2000
There will be no quiz today so you may use your time today to work on your
programming assignments and ask questions. You should be nearing completion
since the program is due tomorrow.
Before beginning, there is an exercise we would like to demonstrate.
Examining the access control keywords
Go to the directory c:\java.
Create two subdirectories. One called test and the other called test1.
Enter test.
Create the following two files.
A.java
package test;
import USA.*;
public class A {
public int a;
private int b;
protected int c;
int f;
public A() {
a = 1;
b = 2;
c = 3;
}
}
B.java
package test;
import USA.*;
public class B extends A {
public int d;
public B() {
d=4;
}
public void showValues() {
System.out.println("The value of a is " + a);
System.out.println("The value of b is " + b);
System.out.println("The value of c is " + c);
System.out.println("The value of f is " + f);
}
public static void main(String[] args) {
B myB = new B();
myB.showValues();
}
}
Enter c:\java\test1 and create the following file.
C.java
package test1;
import USA.*;
import test.*;
public class C {
public static void main(String[] args) {
A myA = new A();
System.out.println("The value of a is " + myA.a);
System.out.println("The value of b is " + myA.b);
System.out.println("The value of c is " + myA.c);
System.out.println("The value of f is " + myA.f);
}
}
Enter c:\java\test.
Compile A.java. There shouldn't be any errors.
However, when you compile B.java, there will be errors.
Before compiling B.java, examine the use of the variables, and
determing which line(s) will cause compile time error(s).
Then compile B.java to test your answer.
What do you learn from this exercise?
Enter c:\java\test1.
When you try to compile C.java there will be errors.
Before compiling C.java, examine the code, and determine which
line(s) will cause compile time error(s).
Then compile C.java to test your answer.
What do you learn from this exercise?
The above exercises should give you a better understanding of what the
access control variables mean.
You may use the rest of this laboratory period to ask questions regarding
your assignment.
Use your time wisely since the program is due in less
than 24 hours!!!!!