back icon
instructor profile picture

يزيد فهد النويبت

(25) 4.7

عال 113 | برمجة حاسبات

جامعة الملك سعود logo
جامعة الملك سعود

299 رس
299 رس

شامل ضريبة القيمة المضافة

أكمل الطلب

Lab01-ArrayOfObjects(Part 2)

Lab01-ArrayOfObjects(Part 2)

Lab01-ArrayOfObjects(Part 2)

class Student




  1. public class Student {  
  2. private String studentName;  
  3. private int studentid;  
  4. private int studentAge;  
  5. private String studentGender;  
  6. public Student(String n, int id, int age, String gen) {  
  7.       
  8.     this.studentName =n;  
  9.     this.studentid = id;  
  10.     this.studentAge = age;  
  11.     this.studentGender = gen;  
  12. }  
  13. public String getStudentName() {  
  14.     return studentName;  
  15. }  
  16. public int getStudentid() {  
  17.     return studentid;  
  18. }  
  19. public int getStudentAge() {  
  20.     return studentAge;  
  21. }  
  22. public String getStudentGender() {  
  23.     return studentGender;  
  24. }  
  25. public void display() {  
  26.     System.out.println("studentName"+studentAge);  
  27.     System.out.println("studentid"+getStudentid());  
  28.     System.out.println("studentAge"+studentAge);  
  29.     System.out.println("studentGender"+studentGender);  
  30. }  
  31. }  


class Section


  1. public class Section {  
  2. private Student arrStu[];  
  3. private int nbStu;  
  4.   
  5. public Section(int size) {  
  6.     arrStu=new Student[size];  
  7.     nbStu=0;  
  8. }  
  9. public boolean addStudent(Student st) {  
  10.     if(nbStu>=arrStu.length)  
  11.     return false;  
  12.     arrStu[nbStu]=st;  
  13.     nbStu++;  
  14.     return true;  
  15. }  
  16. public int searchStu(int id) {  
  17.     for(int i=0;i<nbStu;i++)  
  18.         if(arrStu[i].getStudentid()==id)  
  19.             return i;  
  20.       
  21.       
  22.     return -1;  
  23.       
  24. }  
  25. public boolean deleteStu(int id) {  
  26.     int index=searchStu(id);  
  27.     if(index==-1)  
  28.         return false;  
  29.       
  30.     arrStu[index]=arrStu[index-1];  
  31.     arrStu[nbStu-1]=null;  
  32.     nbStu--;  
  33.     return true;  
  34.       
  35. }  
  36. public void displayAll() {  
  37.     for(int i=0;i<nbStu;i++)  
  38.         arrStu[i].display();  
  39. }  
  40. public void display(int index) {  
  41.     if(index>=0 && index<nbStu)  
  42.     arrStu[index].display();  
  43.     else  
  44.         System.out.println("Wrong index");  
  45.       
  46. }  
  47. //OverLoad  
  48. public void display(String name) {  
  49.     for(int i=0;i<nbStu;i++)  
  50. if(arrStu[i].getStudentName().equalsIgnoreCase(name))  
  51.     arrStu[i].display();  
  52.       
  53. }  
  54. public void displaySameAs(Student st) {  
  55.     for(int i=0;i<nbStu;i++) {  
  56. if(arrStu[i].getStudentName().equalsIgnoreCase(st.getStudentName())   
  57.         && arrStu[i].getStudentAge()==st.getStudentAge()   
  58.         && arrStu[i].getStudentGender().equalsIgnoreCase(st.getStudentGender()))  
  59.     arrStu[i].display();  
  60.     }  
  61.       
  62. }  
  63. public Student searchFrist(String name) {  
  64.     Student s=null;//or you use Student s=arrStu[0]  
  65.     for(int i=0;i<nbStu;i++)  
  66.         if( s==null || arrStu[i].getStudentName().equalsIgnoreCase(name))  
  67.             return s=arrStu[i];  
  68.       
  69.       
  70.     return null;  
  71. }  
  72. public Student searchLast(String name) {  
  73.     Student s=null;//or you use Student s=arrStu[0]  
  74.     for(int i=0;i<nbStu;i++)  
  75.         if( s==null || arrStu[i].getStudentName().equalsIgnoreCase(name))  
  76.              s=arrStu[i];  
  77.       
  78.       
  79.     return s;  
  80. }  
  81. public Student[] getStudent(int age) {  
  82.     //Student[] s1=new Student[arrStu.length];  
  83.     int count=0;  
  84.     for(int i=0;i<nbStu;i++) {  
  85.         if(arrStu[i].getStudentAge()<age)  
  86.             count++;  
  87.     }  
  88.     Student[] s1=new Student[count];  
  89.     int NumofS=0;  
  90.       
  91.     for(int i=0;i<nbStu;i++) {  
  92.         if(arrStu[i].getStudentAge()<age)  
  93.             s1[NumofS++]=arrStu[i];  
  94.     }  
  95.       
  96.     return s1;  
  97. }  
  98. }  

Main



  1. public class Test {  
  2.   
  3.     public static void main(String[] args) {  
  4.       
  5.         Section sec1=new Section(4);  
  6.           
  7.         sec1.addStudent(new Student("Ahmead",1,18,"male"));//return true  
  8.         sec1.addStudent(new Student("fahad",2,16,"male"));//return true  
  9.         sec1.addStudent(new Student("saad",3,15,"male"));//return true  
  10.         sec1.addStudent(new Student("yazeed",4,23,"male"));//return true  
  11.         sec1.addStudent(new Student("Ali",5,20,"male"));//return false (not added)  
  12.           
  13.         sec1.displayAll();//to display all student  
  14.           
  15.         System.out.println("------------------");  
  16.           
  17.         System.out.print("Student with index 0 is");  
  18.           
  19.         sec1.display(1);  
  20.           
  21.         System.out.println("------------------");  
  22.   
  23.         System.out.println("Student with name fahad is");  
  24.           
  25.         sec1.display("fahad");  
  26.           
  27.         System.out.println("Last is: "+sec1.searchFrist("Ahmead").getStudentName());  
  28.         System.out.println("------------------");  
  29.   
  30.         System.out.println("Last is: "+sec1.searchLast("saad").getStudentName());  
  31.           
  32.          Student arrStu[]=sec1.getStudent(18); //to get array of student with age >18  
  33.            
  34.          System.out.println("------------");  
  35.          System.out.println("display new array");  
  36.          //display new array  
  37.          for(int i=0; i<arrStu.length;i++) {  
  38.              if(arrStu[i]==null)  
  39.                  break;  
  40.              else  
  41.                  arrStu[i].display();  
  42.          }  
  43.   
  44.           
  45.           
  46.           
  47.           
  48. }  
  49. }