Objective
The objective of this lab is to apply core principles of object-oriented programming
by defining Java classes, declaring instance and class variables, implementing
multiple constructors, applying method overloading, and computing results through
appropriately structured methods.
Additionally, the lab aims to investigate the behavior of Java access modifiers—
public, protected, default, and private by designing a multi-package program that tests
accessibility within the same class, the same package, subclasses across different
packages, and non-subclasses in different packages.
Problem Statement
This lab consists of two major exercises:
Exercise 1: Student Class Implementation
You are required to design a Student class that manages student personal details and
course performance.
The class must accept marks through constructors, compute total marks using method
overloading, determine the grade through a private helper method, and finally display
all relevant information for each student.
Exercise 2: Java Access Modifiers Demonstration
A multi-package Java application must be created to demonstrate how access
modifiers behave in different contexts.
The program should clearly show which class members are accessible within the same
class, same package, different package (subclass), and different package (non-
subclass).
Procedures
Exercise 1 – Student Class
1. Class Definition
A Student class was created containing instance fields for first name, middle name,
last name, student ID, and all performance marks. Class variables were added to
represent the shared course, department, college, and university.
2. Constructor Development
Three constructors were implemented:
a default constructor with preset values,
an overloaded constructor without project mark,
and a second overloaded constructor that includes the project mark.
3. Method Implementation
A method to compute the total mark without the project mark.
An overloaded method to compute the total mark including the project mark.
A private method that evaluates the student’s grade based on the computed total
A method printStudentInfo() to display the full student information and evaluation
results.
4. Main Program Actions
Three Student objects were instantiated using different constructors.
The printStudentInfo() method was invoked for each object, and class-level
information was displayed.
Exercise 2 – Access Modifiers
1. Package Creation
Two separate packages, package1 and package2, were created.
2. Parent Class Construction
In package1, the ParentClass was defined with variables and methods at all four
access levels: public, protected, default, and private.
A method was added to test accessibility from within the same class.
3. Same Package Testing
Another class, SamePackageClass, was implemented in the same package to attempt
accessing the members of ParentClass.
4. Cross-Package Testing
In package2, two classes were created:
SubClass, extending ParentClass, to test inherited access across packages.
OtherPackageClass, a non-subclass, to test cross-package access restrictions.
Test Execution
A TestClass was written to instantiate all relevant classes and execute the access tests,
displaying results on the console.
5. Program Source Code
package student;
// Class definition
public class Student {
// Instance Data fields declaration
String firstName;
String secondName;
String lastName;
int idNumber;
double quizMark;
double midMark;
double labMark;
double projectMark;
double finalMark;
double totalMark;
// Class data fields
static String course = "OOP";
static final String DEPARTMENT ="ECE";
static final String COLLEGE ="CoE";
static final String UNIVERSITY ="AASTU";
// Constructor declaration
// 1. Default constructor
public Student() {
[Link] = "Neba'ot";
[Link] = "Dereje";
[Link] = "Belay";
[Link] = 1234;
[Link] = 14;
[Link] = 13;
[Link] = 10;
[Link] = 24;
[Link] = 21;
[Link] = [Link] + [Link] + [Link] +
[Link] + [Link];
}
// parametrized Constructor (constructor without project mark, the first constructor
overloading)
public Student(String fName, String sName, String lName, int idNum,
double qMark, double mMark, double lMark, double fMark) {
[Link] = fName;
[Link] = sName;
[Link] = lName;
[Link] = idNum;
[Link] = qMark;
[Link] = mMark;
[Link] = lMark;
[Link] = fMark;
[Link] = 0; // Not included
}
// Constructor with project mark (the second time constructor overloading)
public Student(String fName, String sName, String lName, int idNum,
double qMark, double mMark, double lMark, double pMark, double
fMark) {
[Link] = fName;
[Link] = sName;
[Link] = lName;
[Link] = idNum;
[Link] = qMark;
[Link] = mMark;
[Link] = lMark;
[Link] = pMark;
[Link] = fMark;
}
// Method declaration
//Method1, for computing grade without project mark
public String computeTotalMark() {
totalMark = quizMark + midMark + labMark + finalMark;
return calculateGrade(totalMark);
}
// Method2 - Method overloading, for computing grade including project mark
public String computeTotalMark(boolean includeProject) {
totalMark = quizMark + midMark + labMark + projectMark + finalMark;
return calculateGrade(totalMark);
}
// Private helper method, for computing the grade logic
private String calculateGrade(double mark) {
if (mark <= 100 && mark >= 90) {
return "A";
}
else if (mark < 90 && mark >= 80) {
return "B";
}
else if (mark <80 && mark >= 70) {
return "C";
}
else if (mark < 70 && mark >= 50) {
return "D";
}
else if (mark < 50 && mark >= 0) {
return "F";
}
else return "Invalid grade!";
}
//Another method declaration for displaying the student information
public void printStudentInfo() {
[Link]("Full Name: " + firstName + " " + secondName + " " +
lastName);
[Link]("ID: " + idNumber);
[Link]("Course: " + course);
// Choose correct version of computeTotalMark
if (projectMark == 0) {
[Link]("Grade : " + computeTotalMark()); // Invoking Method 1,
when the totalMark is without projectMark
}
else {
[Link]("Grade: " + computeTotalMark(true)); // When totalMark
is with projectMark, in Method 2 or Default constructor
}
[Link]("Score: " + totalMark);
[Link]("------------------------------------------------");
}
// Main method
public static void main(String[] args) {
// Object creation and instantiantion
Student s1 = new Student();
Student s2 = new Student("Eyob", "Sisay", "Seifu", 2345,15.0, 14.5, 17.25,
21.25);
Student s3 = new Student("Daniel", "Simeneh", "Shiferaw", 5678, 12.5, 14.5,
18.25, 18.3, 28.5);
// Accessing instance variables using object name
// [Link]("Full name: "+[Link] +" "+ [Link] +" "+
[Link]);
// [Link]("ID: "+ [Link]);
// Invoking methods using object names
[Link]("\tInfo of s1");
[Link]("\t*-*-*-*-*-*");
[Link]();
[Link]("\tInfo of s2");
[Link]("\t*-*-*-*-*-*");
[Link]();
[Link]("\tInfo of s3");
[Link]("\t*-*-*-*-*-*");
[Link]();
// Accessing class variables using class name
//[Link]([Link]);
[Link]("Department:"+[Link]+"\tCollege:"+
[Link]+"\tUniversity:"+ [Link]);
}}
For the second exersice
package package1;
public class ParentClass {
public int publicVar = 10;
protected int protectedVar = 20;
int defaultVar = 30; // No modifier means default/package-private
private int privateVar = 40;
public void publicMethod() {
[Link]("Public method: Accessible everywhere.");
}
protected void protectedMethod() {
[Link]("Protected method: Accessible within package and by
subclasses.");
}
void defaultMethod() {
[Link]("Default method: Accessible only within package.");
}
private void privateMethod() {
[Link]("Private method: Accessible only within this class.");
}
// Access within the same class (all are accessible)
public void accessWithinClass() {
[Link]("\tAccess within ParentClass");
[Link]("\t--------------------------");
[Link]("Private Var: " + privateVar); // Accessible
privateMethod(); // Accessible
[Link]("Default Var: " + defaultVar); // Accessible
[Link]("Protected Var: " + protectedVar); // Accessible
[Link]("Public Var: " + publicVar); // Accessible
publicMethod(); // callable
protectedMethod(); // callable
defaultMethod(); // callable
privateMethod(); // callable
}}
-------------------------------------------------------------------------------------------------------
package package1;
public class SamePackageClass {
public void accessSamePackage() {
[Link](" ");
[Link]("\tAccess from SamePackageClass (Same Package)");
[Link]("\t-----------------------------------------------");
ParentClass pc = new ParentClass();
[Link]("Public Var: " + [Link]); // Accessible
[Link]("Protected Var: " + [Link]); // Accessible
[Link]("Default Var: " + [Link]); // Accessible
// [Link]("Default Var: " + [Link]); // ERROR: Private access
is restricted, not Accessible
[Link](); // callable
[Link](); // callable
[Link](); // callable
// [Link](); // ERROR: Private access is restricted, not callable
[Link]("Private members are not accessible from a different class in
the same package.");
}}
-------------------------------------------------------------------------------------------------------
package package2;
import [Link];
public class OtherPackageClass {
public void accessOutsidePackage() {
[Link](" ");
[Link]("Access from OtherPackageClass (Different Package, Non-
Subclass)");
[Link]("---------------------------------------------------------------");
ParentClass pc = new ParentClass();
[Link]("Public Var: " + [Link]); // Accessible
[Link]();
// [Link]("Protected Var: " + [Link]); // ERROR: Protected
access restricted outside package without inheritance
// [Link](); // ERROR: Protected access restricted
[Link]("Protected members are not accessible from a non-subclass in
a different package.");
// [Link]("Default Var: " + [Link]); // ERROR: Default not
accessible outside package
[Link]("Default members are not accessible from a non-subclass in a
different package.");
// [Link]("Private Var: " + [Link]); // ERROR: Private not
accessible outside class
[Link]("Private members are not accessible from a non-subclass in a
different package.");
}}
-------------------------------------------------------------------------------------------------------
package package2;
import [Link];
public class SubClass extends ParentClass {
public void accessFromSubclass() {
[Link](" ");
[Link]("\tAccess from SubClass (Different Package, Subclass)");
[Link]("\t-------------------------------------------------- ");
// Accessing members inherited from ParentClass
[Link]("Public Var: " + publicVar); // Accessible
[Link]("Protected Var: " + protectedVar); // Accessible through
inheritance
publicMethod();
protectedMethod();
// [Link]("Default Var: " + defaultVar); // ERROR: Default not
accessible outside package
// defaultMethod(); // ERROR: Default not accessible outside package
[Link]("Default members are not accessible from a subclass in a
different package.");
// [Link]("Private Var: " + privateVar); // ERROR: Private not
accessible outside class
// privateMethod(); // ERROR: Private not accessible outside class
[Link]("Private members are not accessible from a subclass in a
different package.");
}
}
------------------------------------------------------------------------------------------
import [Link];
import [Link];
import [Link];
import [Link];
public class TestClass {
public static void main(String[] args) {
ParentClass pc = new ParentClass();
[Link]();
SamePackageClass spc = new SamePackageClass();
[Link]();
SubClass sc = new SubClass();
[Link]();
OtherPackageClass opc = new OtherPackageClass();
[Link]();
}
}
----------------------------------------------------------------------------------------------
6. Result
The program produced the following outcomes:
Exercise 1 Results
Each student’s full name, ID, course, total score, and grade were successfully
displayed.
The shared academic information (department, college, university) was also printed.
Exercise 2 Results
The output clearly demonstrated the accessibility rules of Java:
Public members were accessible in all contexts.
Protected members were accessible within the same package and through
inheritance across packages.
Default members were accessible only within the same package
Private members were accessible exclusively within the same class.
These results were verified through successful accesses and restricted attempts shown
in the console output.
7. conclusion
This lab effectively demonstrated essential concepts of object-oriented programming,
including class definition, constructor overloading, method overloading,
encapsulation, and grade computation techniques.
The access modifier experiment successfully reinforced how Java controls visibility
and accessibility across packages and inheritance levels.
Overall, the exercises strengthened understanding of structured program design,
abstraction, and modular development in Java.