class Student{
/* fields describing the student ID and course marks */
private String ID="1234546";
int math
= 85;
int english = 55;
final int history = 65;
int science = 92;
int copyNo = 0;
/* The constructor I am using to get this action done everytime the
class instantiates. Notice the constructor has the same
name as the
class and looks like a method except with no return type
*/
Student( ){
copyNo = copyNo+1;
}
/* a method returning the students marks */
public int getAverage(){
int total = math + english
+ history + science;
int average = total / 4;
return average;
}
/* a method returning the student's ID */
public String getID(){
return ID;
}
}
class Average_Engine{
public static void main(String [] argv) {
Student av = new Student();
Student av2 = new Student();
Student av3 = new Student();
Student av4 = new Student();
Student av5 = new Student();
int average = av.getAverage( );
System.out.println("Student " + av.getID() + " has an average
of " + average);
System.out.println("The student's science mark is " + av.science);
av.science=23;
// here's the work of the prankster
average = av.getAverage( );
System.out.println("The student's science mark is " + av.science);
System.out.println("Student " + av.getID() + " has an average
of " + average);
System.out.println("av5 is copy number " + av5.copyNo + ".");
}
}
Questions
Students are presented their marks via new instances of classes that
represent their records.
Assume you have access to the Average_Engine class but no direct access
to the student class.
You can only reference the Student class instances in the manner the
prankster below did.
(The 5 instances of the student class are there for use of Q6 and Q7)
1. Notice how a prankster has taken the luster off of this students
average. Change the mark
back to what it is suppose to be from within the
source code for the Average_Engine class.
2. This student actually should have received 99% in math. Change
that mark too by the same
technique. This will give the student the
correct impression of his mark until someone can
change it back in the class.
3. Try to change the student ID by the same technique. from within the
Average_Engine class.
What happens? Why can't the ID be changed?
4. Try to change the history mark. What happens why?
5. What role does the method getID( ) play. What barrier does the method circumvent?
6. Break the rule and paste the following print line into the constructor
immediately after the line,
copyNo = copyNo+1;
System.out.println("I'm in Student class constructor and copyNo is " + copyNo);
Recompile and run the code. The copy numbers are the same why?
7. Break the rule again and go into the Student class and mark the the
copyNo variable static.
(Do this where copyNo is declared) Redo Step 6.
What has changed and why?