Lesson 1
Managing Data with Lists and Classes in Java
Managing Data with Lists and Classes in Java

Hello, Space Explorer! Today, we’re going to discuss a practical and essential topic in Java: managing data using lists and classes. To practice this concept, we will build a simple Student Management System. Specifically, we will create a class that stores students and their grades. This hands-on approach will help us understand how lists and objects can be used effectively in real-world applications. Are you excited? Great, let's dive in!

Introducing Methods to Implement

To accomplish our task, we need to implement three primary methods within our class:

  • addStudent(String name, int grade): This method allows us to add a new student and their grade to our list. If the student is already on the list, their grade will be updated.
  • getGrade(String name): This method retrieves the grade for a student given their name. If the student is not found, it returns null.
  • removeStudent(String name): This method removes a student from the list by their name. It returns true if the student is successfully removed and false if the student is not found.

Does that sound straightforward? Fantastic, let's break it down step-by-step.

Implementing the Solution Step-by-Step

Let’s start by defining our StudentManager class, which will use a list of objects to manage students and their grades. We will also define a Student class to represent individual student data.

Java
1import java.util.ArrayList; 2 3class StudentManager { 4 private ArrayList<Student> students; 5 6 public StudentManager() { 7 students = new ArrayList<Student>(); 8 } 9} 10 11class Student { 12 private String name; 13 private int grade; 14 15 public Student(String name, int grade) { 16 this.name = name; 17 this.grade = grade; 18 } 19 20 public String getName() { 21 return name; 22 } 23 24 public void setName(String name) { 25 this.name = name; 26 } 27 28 public int getGrade() { 29 return grade; 30 } 31 32 public void setGrade(int grade) { 33 this.grade = grade; 34 } 35} 36 37public class Solution { 38 public static void main(String[] args) { 39 StudentManager manager = new StudentManager(); 40 System.out.println("StudentManager instance created."); 41 } 42}
Step 1: Implementing addStudent

The addStudent method checks if a student already exists in our list. If so, their grade is updated; otherwise, the student is added to the list.

Java
1import java.util.ArrayList; 2 3class StudentManager { 4 private ArrayList<Student> students; 5 6 public StudentManager() { 7 students = new ArrayList<Student>(); 8 } 9 10 public void addStudent(String name, int grade) { 11 for (Student student : students) { 12 if (student.getName().equals(name)) { 13 student.setGrade(grade); 14 System.out.println("Updated: " + name + ", Grade: " + grade); 15 return; 16 } 17 } 18 students.add(new Student(name, grade)); 19 System.out.println("Added: " + name + ", Grade: " + grade); 20 } 21} 22 23class Student { 24 private String name; 25 private int grade; 26 27 public Student(String name, int grade) { 28 this.name = name; 29 this.grade = grade; 30 } 31 32 public String getName() { 33 return name; 34 } 35 36 public void setName(String name) { 37 this.name = name; 38 } 39 40 public int getGrade() { 41 return grade; 42 } 43 44 public void setGrade(int grade) { 45 this.grade = grade; 46 } 47} 48 49public class Solution { 50 public static void main(String[] args) { 51 StudentManager manager = new StudentManager(); 52 53 // Test addStudent method 54 manager.addStudent("Alice", 90); 55 manager.addStudent("Bob", 85); 56 manager.addStudent("Alice", 95); 57 } 58}

Let's break it down:

  • Using a for-each loop, we iterate through students.
  • If we find an object where the name property matches the given name, we update the grade.
  • If not found, we append a new Student object created with {name, grade} to our list.

Why do we need to check if the student already exists before appending? Preventing duplicate entries and ensuring data consistency is key!

Step 2: Implementing getGrade

The getGrade method searches for a student in the list by name and returns their grade.

Java
1import java.util.ArrayList; 2 3class StudentManager { 4 private ArrayList<Student> students; 5 6 public StudentManager() { 7 students = new ArrayList<Student>(); 8 } 9 10 public void addStudent(String name, int grade) { 11 for (Student student : students) { 12 if (student.getName().equals(name)) { 13 student.setGrade(grade); 14 System.out.println("Updated: " + name + ", Grade: " + grade); 15 return; 16 } 17 } 18 students.add(new Student(name, grade)); 19 System.out.println("Added: " + name + ", Grade: " + grade); 20 } 21 22 public Integer getGrade(String name) { 23 for (Student student : students) { 24 if (student.getName().equals(name)) { 25 return student.getGrade(); 26 } 27 } 28 return null; 29 } 30} 31 32class Student { 33 private String name; 34 private int grade; 35 36 public Student(String name, int grade) { 37 this.name = name; 38 this.grade = grade; 39 } 40 41 public String getName() { 42 return name; 43 } 44 45 public void setName(String name) { 46 this.name = name; 47 } 48 49 public int getGrade() { 50 return grade; 51 } 52 53 public void setGrade(int grade) { 54 this.grade = grade; 55 } 56} 57 58public class Solution { 59 public static void main(String[] args) { 60 StudentManager manager = new StudentManager(); 61 62 // Test addStudent method 63 manager.addStudent("Alice", 90); 64 manager.addStudent("Bob", 85); 65 manager.addStudent("Alice", 95); 66 67 // Test getGrade method with formatted output 68 System.out.println("Alice's grade: " + (manager.getGrade("Alice") != null ? manager.getGrade("Alice") : "Not found")); 69 System.out.println("Bob's grade: " + (manager.getGrade("Bob") != null ? manager.getGrade("Bob") : "Not found")); 70 System.out.println("Charlie's grade: " + (manager.getGrade("Charlie") != null ? manager.getGrade("Charlie") : "Not found")); 71 } 72}

Let's consider the approach:

  • We iterate through students.
  • If we find an object where the name property matches the given name, we return the grade property.
  • If we exhaust the list without finding the student, we return null.

Can you think of situations where a student might not be found? Right, they might be new students, or there might be potential typos in the name.

Step 3: Implementing removeStudent

The removeStudent method removes a student from the list by their name and returns whether the operation was successful.

Java
1import java.util.ArrayList; 2 3class StudentManager { 4 private ArrayList<Student> students; 5 6 public StudentManager() { 7 students = new ArrayList<Student>(); 8 } 9 10 public void addStudent(String name, int grade) { 11 for (Student student : students) { 12 if (student.getName().equals(name)) { 13 student.setGrade(grade); 14 System.out.println("Updated: " + name + ", Grade: " + grade); 15 return; 16 } 17 } 18 students.add(new Student(name, grade)); 19 System.out.println("Added: " + name + ", Grade: " + grade); 20 } 21 22 public Integer getGrade(String name) { 23 for (Student student : students) { 24 if (student.getName().equals(name)) { 25 return student.getGrade(); 26 } 27 } 28 return null; 29 } 30 31 public boolean removeStudent(String name) { 32 for (int i = 0; i < students.size(); i++) { 33 if (students.get(i).getName().equals(name)) { 34 students.remove(i); 35 return true; 36 } 37 } 38 return false; 39 } 40} 41 42class Student { 43 private String name; 44 private int grade; 45 46 public Student(String name, int grade) { 47 this.name = name; 48 this.grade = grade; 49 } 50 51 public String getName() { 52 return name; 53 } 54 55 public void setName(String name) { 56 this.name = name; 57 } 58 59 public int getGrade() { 60 return grade; 61 } 62 63 public void setGrade(int grade) { 64 this.grade = grade; 65 } 66} 67 68public class Solution { 69 public static void main(String[] args) { 70 StudentManager manager = new StudentManager(); 71 72 // Test addStudent method 73 manager.addStudent("Alice", 90); 74 manager.addStudent("Bob", 85); 75 manager.addStudent("Alice", 95); 76 77 // Test getGrade method with formatted output 78 System.out.println("Alice's grade: " + (manager.getGrade("Alice") != null ? manager.getGrade("Alice") : "Not found")); 79 System.out.println("Bob's grade: " + (manager.getGrade("Bob") != null ? manager.getGrade("Bob") : "Not found")); 80 System.out.println("Charlie's grade: " + (manager.getGrade("Charlie") != null ? manager.getGrade("Charlie") : "Not found")); 81 82 // Test removeStudent method 83 System.out.println(manager.removeStudent("Alice") ? "Alice removed" : "Alice not found"); 84 System.out.println(manager.removeStudent("Charlie") ? "Charlie removed" : "Charlie not found"); 85 86 // Confirm removal 87 System.out.println("Alice's grade after removal: " + (manager.getGrade("Alice") != null ? manager.getGrade("Alice") : "Not found")); 88 } 89}

Here’s what happens:

  • We iterate through students and check for a matching student name.
  • If a match is found, we delete the entry using remove and return true.
  • If no match is found by the end of the loop, we return false.

This method checks for presence before deletion, ensuring we only attempt to delete valid entries. Why is this check important? Yes, it prevents errors and helps maintain a reliable state.

Summary

In this lesson, we created a StudentManager class to manage students and their grades using Java objects and ArrayList. We implemented three essential methods — addStudent, getGrade, and removeStudent. Each method illustrated key programming paradigms, including iteration, condition checking, and simple data manipulation.

We also demonstrated how to instantiate the StudentManager class and invoke its methods, illustrating how these operations can be used in practice.

By completing this exercise, you have fortified your knowledge of using objects and lists in practical applications. I encourage you to continue practicing these concepts by extending the class with new features or experimenting with similar problems. Always remember — practice makes perfect! Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.