- How to provide the
age information:
- We use 3 variable
int day, month, year to store the
birthday of a person.
- The age is
computed using the
curent system time
|
- The direct access and
indirect (ADT) access solutions:
Direct access solution
|
ADT solution
|
public class AgeInformation
{
public String name;
public int day, month, year;
// Constructor
AgeInformation(String s, int a,
int b, int c)
{
name = s;
day = a;
month = b;
year = c;
}
...
}
|
public class AgeInformation
{
private String name;
private int day, month, year;
// Constructor
AgeInformation(String s, int a,
int b, int c)
{
name = s;
day = a;
month = b;
year = c;
}
public int getAge()
{
Read system time and compute age
(Algorithm omitted for clarity sake)
return age;
}
...
}
|
- Now, look at
what you need to change
in the user programs
to make them access (obtain)
the age information using the
different solutions:
Direct access solution
|
ADT solution
|
public class MyProgram
{
public static void main(String[] a)
{
AgeInformation stu1
= new AgeInformation("John",
29, 2, 1967);
int info;
info = stu1.age ??? does not work !!!;
...
}
}
|
public class MyProgram
{
public static void main(String[] a)
{
AgeInformation stu1
= new AgeInformation("John",
29, 2, 1967);
int info;
info = stu1.getAge() /* Still works ! */;
...
}
}
|
|