Object Oriented Programming - Fundamentals

Part 1: Exploring the School System Project

  1. Create a new Java project using Eclipse IDE.
  2. Create a Student class. This class must have the following attributes:
   public class Student {
       String firstName;
       String lastName;
       int registration;
       int grade;
       int year;
   }
  1. Now, make the Student class implement the following methods:
   public void printFullName(){
      //TODO implement
   }

   public void isApproved(){
       //TODO implement: should return true if grade >= 60
   }

   public int changeYearIfApproved(){
       //TODO implement: the student should advance to the next year if he/she grade is >= 60
       // Make year = year + 1, and print "Congragulations" if the student has been approved
       return 0;
   }
  1. Add constructors to your Student class:

    • Make the class with at least three different constructors.
  2. Create a Java class for Courses

    • Your Course class must have, as attributes: courseName, professorName, year.
    • Your class must also contain a collection that lists all students enrolled in them.
    • Implement the following methods.
   public void enroll(Student student){
      //TODO add the student to the collection
   }

   public void unEnroll(Student student){
       //TODO remove this student from the collection
       // Hint: check if that really is this student
   }

   public int countStudents(){
       //TODO implement
       return 0;
   }
   
   public int bestGrade(){
       //TODO implement
       return 0;
   }
  1. Run the main method and verify that your implementation works.

  2. Method Overload:

    • Overload the enroll method to take in an array of students.
   public void enroll(Student[] students){
      //TODO add all the students to the collection
   }
  • Add on to the main method and call enroll with a list of students. Verify that your implementation works.

Challenge yourself

  • Implement a function that calculates the average grade for that course.
  • Implement a function that outputs a ranking with all students enrolled in a course and respective grades.
  • Implement a function that, for each student, show if he/she is above course average or not.

Object Oriented Programming - Advanced

Part 1: Packages, Access Modifiers and Encapsulation

  1. Download the source code, import the project into Eclipse IDE and run it to verify it works correctly.
  2. Go through the entire project and read the different classes and components trying to understand the logic behind.
  3. Create at least two packages to organize your project better. Move the corresponding classes to the packages.
  4. Modify the Student class so it follows the encapsulation principle keeping data private to the class.
  5. Modify the Course class so it follows the encapsulation principle keeping data private to the class.
  6. Modify the StudentService class so it follows the encapsulation principle:
  • Make data private so it can only be modified inside the class.
  • Create a method that lets you add students and use that in the main function.

Part 2: Using collections with objects

  1. Implment the following functions in the StudentService class:
   public void showEnrolledStudents(String courseId){
        //TODO implement using collections loops
   }
   
   public void showAllCourses(){
       //TODO implement using collections loops
   }

Part 3: Using Java Exeptions

  1. Modify the enrollStudents method to verify:
  • if the Course does not exists throw a CourseNotFoundException.
  • if the Student does not exists throw a StudentNotFoundException.
       public void enrollStudents( String courseName, String studentID )
    {
        Course course = courseList.get( courseName );

        if ( !coursesEnrolledByStudents.containsKey( studentID ) )
        {
            coursesEnrolledByStudents.put( studentID, new ArrayList<>() );
        }
        coursesEnrolledByStudents.get( studentID ).add( course );
    }

Challenge yourselff

  1. Modify the unEnrollStudents method to verify:
  • if the Course does not exists throw a CourseNotFoundException.
  • if the Student does not exists throw a StudentNotFoundException.
       public void enrollStudents( String courseName, String studentID )
    {
        Course course = courseList.get( courseName );

        if ( !coursesEnrolledByStudents.containsKey( studentID ) )
        {
            coursesEnrolledByStudents.put( studentID, new ArrayList<>() );
        }
        coursesEnrolledByStudents.get( studentID ).add( course );
    }