The Student class

public class Student{

String[] subjects;

String[] grades;

int[] hours;

public Student(){String[] subjects, int[] hours}

this.subjects = subjects;

this.hours = hours;

//construct an empty array for grades (processed later)

this.grades = new String[4];

}

}

The Student class

public class Student{

public void setGrade(String subject, String grade){

int index = 0;

//perform a linear search using a while loop

while(index<subjects.length && !subject.equals(subjects[index])){

index++;

}

if(index != subjects.length){ //this means index stopped inside the array

grades[index] = grade;

System.out.print("Grade updated!");

}else{ //index stepped out of the array

System.out.print("Subject not found.");

}

}

}

Objectives

Students will be able to:

  • unserstand the use of the static modifier in OOP in Java

Static

 

Scenario

A School HAS

In a real-world context, is this a form of composition/containment or aggregation?

Some school-related data refers to all Students, not just individual students.

Let's explore this static context in Java.

Task 1

Watch the video and complete the worksheet.

Exercises

TBD

Tags

idesyntax file class instantiate constructor type propertymethod