Skip to main content

Command Palette

Search for a command to run...

Java using getters and setters to manage a list of student

Updated
2 min read
T

I am a dedicated and skilled Software Engineer specializing in mobile app development, backend systems, and creating secure APIs. With extensive experience in both SQL and NoSQL databases, I have a proven track record of delivering robust and scalable solutions.

Key Expertise:

Mobile App Development: I make high-quality apps for Android and iOS, ensuring they are easy to use and work well.

Backend Development: Skilled in designing and implementing backend systems using various frameworks and languages to support web and mobile applications.

Secure API Creation: Expertise in creating secure APIs, ensuring data integrity and protection across platforms.

Database Management: Experienced with SQL databases such as MySQL, and NoSQL databases like Firebase, managing data effectively and efficiently.

Technical Skills: Programming Languages: Java, Dart, Python, JavaScript, Kotlin, PHP

Frameworks: Angular, CodeIgniter, Flutter, Flask, Django

Database Systems: MySQL, Firebase

Cloud Platforms: AWS, Google Cloud Console

I love learning new things and taking on new challenges. I am always eager to work on projects that make a difference.


PRE REQUISITES:

[1] Data Structure (thirdygayares.com)

[2] OOP Concepts (Using Getters and Setters) (thirdygayares.com)

simple implementation in Java using getters and setters to manage a list of student information based on this Data Structure (thirdygayares.com). We will create a Student class to encapsulate the data, and we will use an ArrayList to store multiple students.

public class Student {
    private int id;
    private String name;
    private String section;
    private int age;
    private String email;

    // Getter and Setter for id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    // Getter and Setter for name
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    // Getter and Setter for section
    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    // Getter and Setter for age
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    // Getter and Setter for email
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    // Display Student Information
    public String toString() {
        return "ID: " + id + ", Name: " + name + ", Section: " + section + ", Age: " + age + ", Email: " + email;
    }
}
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {

        // List to store Student objects
        List<Student> students = new ArrayList<>();

        // Creating Student objects and setting values using setters
        Student student1 = new Student();
        student1.setId(1);
        student1.setName("Juan Carlos");
        student1.setSection("BSIT-4B");
        student1.setAge(22);
        student1.setEmail("[email protected]");

        Student student2 = new Student();
        student2.setId(2);
        student2.setName("Jose Rizal");
        student2.setSection("BSIT-2A");
        student2.setAge(21);
        student2.setEmail("[email protected]");

        students.add(student1);
        students.add(student2);

        // Display all students' information using getters
        for (Student student : students) {
            System.out.println(student.toString());
        }
    }
}
  • Student Class: We define private fields for id, name, section, age, and email. For each field, we have a corresponding getter and setter method to manage the data.

  • Main Class: We create several Student objects, set their data using the setter methods, and store them in an ArrayList. Finally, we loop through the list and print each student's information using the toString() method

Output:

ID: 1, Name: Juan Carlos, Section: BSIT-4B, Age: 22, Email: juan@gmail.com
ID: 2, Name: Jose Rizal, Section: BSIT-2A, Age: 21, Email: jose@gmail.com

More from this blog

T

Thirdy Gayares

99 posts

Software Engineer | Mobile Developer | Data Scientist