Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8339093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:47:18+00:00 2026-06-09T04:47:18+00:00

I have included all my code below (5 Classes) for a simple database of

  • 0

I have included all my code below (5 Classes) for a simple database of students.

I cowrote this code with someone who believes that in the Name, Address and Student classes it is necessary to include all of the getters and setters that are currently there. I think the code will function without them and that they are not necessary and should be removed? If they are necessary could someone explain why or could someone confirm that they are not necessary?

Address Class

public class Address {

private String street, area, city, country;

public Address(String street, String area, String city, String country) {
    this.street = street;
    this.area = area;
    this.street = city;
    this.country = country;
}

public void setStreet(String street) {
    this.street = street;
}

public void setArea(String area) {
    this.area = area;
}

public void setCity(String city) {
    this.city = city;
}

public void setCountry(String country) {
    this.country = country;
}

public String street() {
    return street;
}

public String area() {
    return area;
}

public String city() {
    return city;
}

public String country() {
    return country;
}

    /** Returns area, street, city and country concatenated together respectively */
public String toString() {
    return area + ", " + street + ", " + city + ", " + country + ".";
}

    }

Name Class

public class Name{

private String firstName, lastName;

public Name (String firstName, String lastName){
    this.firstName = firstName;
    this.lastName = lastName;
}

public void setFirstName(String firstName){
    this.firstName = firstName;
}

public void setLastName(String lastName){
    this.lastName = lastName;
}

public String getFirstName() {
    return firstName;
}

public String getLastName() {
    return lastName;
}

/** Returns first name concatenated to last name */
public String toString() {
    return firstName + " " + lastName;
}

}

Student Class

     public class Student {
    private Name name; // This is calling from the Name class, giving it the               name 'name'
    private Address address; // This calls from Address, giving it the name 'address'

    private char gender;

    private String course, college;

    private int gradePointAverage, id, age, level;

    public Student(int id, String firstName, String lastName, String street, String area, String city, String country, int age, char gender, String college, String course,  int level, int gradePointAverage){ //This is the list of variables called from the Student class
        this.id = id;
        this.name = new Name(firstName, lastName);
        this.address = new Address(street, area, city, country);
        this.age = age;
        this.gender = gender;
        this.college = college;
        this.course = course;
        this.level = level;
        this.gradePointAverage = gradePointAverage;
    }

    public int getId(){
        return id;
    }

    public String getName(){
        return name.toString();
    }

    public String getAddress(){
        return address.toString();
    }

    public int getAge(){
        return age;
    }

    public char getGender(){
        return gender;
    }

    public String getCollege(){
        return college;
    }

    public int getLevel() {
        return level;
    }

     public String getCourse() {
        return course;
    }

    public int getGradePointAverage() {
        return gradePointAverage;
    }

    public void printStudent() {
        System.out.println("The Student " + name.toString() + " is logged under the student ID number " + id + ".");
        System.out.println("They live at " + address.toString() + " and their age is " + age + ".");
        System.out.println("Their gender is " + gender + ".");
        System.out.println("The student studies at " + college + " attending classes in " + course + ".");
        System.out.println("Their level is " + level + " and the student grade average in points is " + gradePointAverage + ".");
        System.out.println();
    }

    }

CollegeCommunity Class

    import java.util.*;

public class CollegeCommunity

    {

    private ArrayList<Student> students; // Setting up an arraylist to store student details




        public CollegeCommunity()

            {
                students = new ArrayList<Student>();
            }


        public void addStudent(Student student) // adds a student.

            {
                students.add(student); // .add method command.
            }


        public void removeStudent(int id) // deleting a student, after being passed id to locate desired student.

            {
                for (int i=0; i<students.size(); i++ ){ // using a loop to decide what student to remove by matching the student ID which was passed to the method with the student ID's on record. Once there's a match, the student will be removed (.remove).
                    if(students.get(i).getId()==id) {
                        students.remove(i);
                    }
                }
            }


        public void showStudent(int id) // same as remove above but instead using print command to view details of particular student.

            {
                for (int i=0; i<students.size(); i++ ){
                    if(students.get(i).getId()==id) {
                        students.get(i).printStudent();
                   }
                }
            }


        public void showAllStudents()

            {
                for (int i=0; i<students.size(); i++ ){ // This loop command will display ALL student details as no specific ID was passed, so it will run as long as value 'i' is less than student.size.
                    int id=students.get(i).getId();
                        showStudent(id);
                }
            }


        public void showStudentsInCourse(String course) // This will show students in a particular course.

            {
                for(int i=0; i<students.size(); i++ ){ // Loop is same as remove but comparing the string course with the course of each student. .equals is used to compare strings.
                    if(students.get(i).getCourse().equals(course)){
                        int id=students.get(i).getId();
                            showStudent(id);
                    }
                }
            }


        public int calculateGradePointAverage() // calculating the grade point average as a percentage

        {
            int total = 0;
            for (int i = 0; i < students.size(); i++ ){
                total = total + students.get(i).getGradePointAverage(); // total is calculated as it loops, each students score (getGradePointAverage).
            }

            total = total / students.size(); // final figure is total divided by the number of students, to give an average score.

            return total;

        }
}

TestSystem Class

    import java.util.*;

    public class TestSystem

{
   public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        Student student;
        CollegeCommunity collegeCommunity = new CollegeCommunity(); // New CollegeCommunity created called 'collegeCommunity'.

        int id, age, level, gradePointAverage; // ints created for student id, age, level of course, and the grade point value

        String fName,lName, street, area, city, country, course, college; // first name, last name strings created

        char gender; // character gender created

        boolean finish = false; // boolean finish has value of False.

            do

             {
                switch(getMenu())

                {

                    case '1':


                        System.out.println();

                        System.out.print("Please enter a new Student ID > ");
                        id = sc.nextInt();
                        sc.nextLine();

                        System.out.println();

                        System.out.print("Please enter the Student's first name > ");
                        fName = sc.nextLine();

                        System.out.println();

                        System.out.print("Please enter the Student's last name > ");
                        lName = sc.nextLine();

                        System.out.println();

                        System.out.print("Please enter the street > ");
                        street = sc.nextLine();

                        System.out.println();

                        System.out.print("Please enter the area > ");
                        area = sc.nextLine();

                        System.out.println();

                        System.out.print("Please enter the city > ");
                        city = sc.nextLine();

                        System.out.println();

                        System.out.print("Please enter the country > ");
                        country = sc.nextLine();

                        System.out.println();

                        System.out.print("Please enter the age > ");
                        age = sc.nextInt();

                        sc.nextLine();

                        System.out.println();

                        System.out.print("Please enter the gender > ");
                        gender = sc.nextLine().charAt(0);

                        System.out.println();

                        System.out.print("Please enter the college > ");
                        college = sc.nextLine();

                        System.out.println();

                        System.out.print("Please enter the course > ");
                        course = sc.nextLine();

                        System.out.println();

                        System.out.print("Please enter level > ");
                        level = sc.nextInt();

                        sc.nextLine();

                        System.out.println();

                        System.out.print("Please enter the average grade in points [0-100] > ");
                        gradePointAverage = sc.nextInt();

                        sc.nextLine();

                        System.out.println();

                        student = new Student(id, fName, lName, street, area, city, country, age, gender, college, course, level, gradePointAverage);
                        collegeCommunity.addStudent(student);


                        break;


                    case '2':


                        System.out.println();

                        System.out.print("Please enter the student ID number > ");
                        id = sc.nextInt();

                        sc.nextLine();
                        // this will delete the student selected
                        collegeCommunity.removeStudent(id);

                        System.out.println();

                        System.out.print("Student " + id + " has been deleted.");

                        System.out.println();
                        System.out.println();

                        break;


                    case '3':


                        System.out.println();

                        System.out.print("Please enter the student ID number > ");
                        id = sc.nextInt();

                        sc.nextLine();
                        // This will allow for the details of the selected student to be displayed. Calls stored data from community college.
                        collegeCommunity.showStudent(id);

                        System.out.println();
                        System.out.println();

                        break;


                    case '4':


                        System.out.println();
                        // This will show all of the students stored in the system.
                        collegeCommunity.showAllStudents();

                        System.out.println();

                        break;


                    case '5':

                        System.out.println();

                        System.out.print("Please enter the course > ");
                        course = sc.nextLine();
                        // This will show students that are enrolled in a chosen course.
                        collegeCommunity.showStudentsInCourse(course);

                        System.out.println();

                        break;

                    case '6':

                        System.out.println();

                        System.out.print("The Average grade score is " + collegeCommunity.calculateGradePointAverage() + "%");

                        System.out.println();

                        break;

                    case 'x':
                    case 'X':

                        finish = true; // boolean value changes to true if X is selected

                        System.exit(0);

                    break;

                        default:

                        System.out.println();

                        System.out.println("That is an invalid selection.");

                        System.out.println("      Please try again");

                    break;

                    }

        }while (!finish);

    }

        public static char getMenu()

            {
                Scanner sc = new Scanner(System.in);

                System.out.println();

                System.out.println("This is your community college menu");
                System.out.println("Please select from the menu options below");

                System.out.println();

                System.out.println("1 Add a Student");

                System.out.println();

                System.out.println("2 Delete a Student");

                System.out.println();

                System.out.println("3 Show details on an individual student");

                System.out.println();

                System.out.println("4 Show details on all students");

                System.out.println();

                System.out.println("5 Shows details on all students on a course");

                System.out.println();

                System.out.println("6 Display the average grade (points)");

                System.out.println();

                System.out.println("X Exit");

                System.out.println();
                System.out.println();

                System.out.print("Please make selection > ");

                return sc.next().charAt(0);
            }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:47:19+00:00Added an answer on June 9, 2026 at 4:47 am

    You can always set the properties as public or protected or with default scope and access them (provided the class is in the appropiate package)

    myStudent.age = 9;
    

    That said, it is always good practice to use setters and getters. It eases control of what values are being set. For instance. You can put a check in your set so that the age is never less than 4

    public void setAge(int age) throws Exception {
      if (age <= 3) {
        throw new Exception("Age must be more than 3");
      }
      this.age = age;
    }
    

    Setting this with properties is left to the programmer, who may miss the check somewhere in the code. Debugging that later is very tricky.

    Setters and getters also help to add logs / breakpoints to check the behavior of the program.

    And most decent IDEs allow you to automatically code setters and getters just from the properties, so it is not a big deal.

    As a side note, I would usually recommend storing birth dates and calculating age when needed, since age is a value that changes while birth date not, and you can always calculate ages from birth dates but not the opposite.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have included my two code sources below. The problem that I am having
I have the code below (I've included what I believe are all relevant sections):
Can anyone please tell why this error? I have included everything ( all header,
I have a header file, lets say Common.h, that is included in all of
I have a index.php page that is the main page. All pages are included
I have a code file from the boto framework pasted below, all of the
I have created an utility function which I include in almost all Javascript code
I have a PHP statement on a sidebar, the sidebar is included on all
I have been writing some code that creates a generic blog. Its features are
I have policies that derive from a base policy. Some classes specialize for the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.