Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public void setGender (String gender) if(gender.equalsIgnorecase(\"M\")llgender.

ID: 3814720 • Letter: P

Question

public void setGender (String gender) if(gender.equalsIgnorecase("M")llgender.equalsIgnorecase("F")){ this gender gender toUpperCase else{ throw new IllegalArgumentException("Gender should be either M or F") Q1: What is the logic for the above setGender code? Modify the constructor as showed below Student (String name String gender) internal-id++; id internal id this name name setGender (gender); create the toString function as showed below public string t String return "id:"+id+"; name +name+" gender +gender Create StudentController Class keep the code commented. you will create the class Students soon.

Explanation / Answer

Q1. setGender() method will only allow the gender to be M or F (Male or Female) only. In case of invalid input, it throws an exception.

Q2. The above code creates 4 student objects and add them to the studentList(The arraylist which we have created). Doesn't produce any output.

Q3. If some illegal gender is entered, Student object will throw illegalArgumentException, which will be catched in catch block and the exceptionMessage will be printed inside catch block.

Student.java:


public class Student {
   private String name, gender;
   private int id;
   private static int internal_id=1;
   public Student(String name, String gender) {
       internal_id++;
       id = internal_id;
       this.name = name;
       setGender(gender);
   }

   public String getName() {
       return name;
   }

   public String getGender() {
       return gender;
   }

   public int getId() {
       return id;
   }

   public void setGender(String gender) {
       if(gender.equalsIgnoreCase("M") || gender.equalsIgnoreCase("F"))
           this.gender = gender;
       else
           throw new IllegalArgumentException("Gender should be M or F only");
   }

   @Override
   public String toString() {
       return "id=" + id + ";name=" + name + "; gender=" + gender;
   }
  
}




Students.java:

import java.util.ArrayList;


public class Students {
   private ArrayList<Student> list = new ArrayList<>();

   public Students() {
       this.list = new ArrayList<>();
   }

   public void createStudents() {
       Student stu1 = new Student("Zack", "M");
       Student stu2 = new Student("Anna", "M");
       Student stu3 = new Student("Jessica", "M");
       Student stu4 = new Student("Frank", "M");
      
       list.add(stu1);
       list.add(stu2);
       list.add(stu3);
       list.add(stu4);

       try{
           Student stu5 = new Student("Jason", "U");
           list.add(stu5);
       }catch(IllegalArgumentException e) {
           System.out.println(e);
       }
   }
  
   public void printStudents() {
       for(Student s: list){
           System.out.println(s);
       }
   }
  
   public void stats() {
       System.out.println("Total number of students: " + list.size());
       int mCount = 0;
       int fCount = 0;
      
       for(Student s: list) {
           if(s.getGender().equals("M"))
               mCount++;
           if(s.getGender().equals("F"))
               fCount++;
       }
       System.out.println("Total number of female students: " + fCount);
       System.out.println("Total number of male students: " + mCount);
   }
}


StudentController.java:


public class StudentController {

   public static void main(String[] args) {
       Students students = new Students();
       students.createStudents();
       students.printStudents();
       students.stats();
   }
}