Q:

convert uml class diagram to java code | Student and Section

0

COS 102 : Java Programming-II, Lab: Array of Objects

--------------------

Question#1:

Write the Java code for classes Student and Section Class Section:

AddStudent(student:stu): It will add the student details in the Array of objects(arrayStu) , returns true if successfully added otherwise it will return false.

SearchStudent(stuid:id): It will search for the detail of the student using the ID and return the index of the array. If not found, it returns -1. 

calculateTotalAges(): calculate and returns the sum of ages of all students.

calculateAvgAge(): calculates and returns the average student age of the section.

printStuData( stuid:int): Prints the information about the student whose student id is stuid otherwise it displays “Student not Found”.

deleteStu(( stuid:int): This method will delete student with given id stuid and returns true otherwise returns false if student is not found

findMaxAge(): return the maximum age of the student in the section. findMinAge(): return the minimum age of the student in the section.

 

Question#2

Create a test class and creates an object of the Section class with size 10.

Create 5 students objects and add them in section.

Delete any one of the students, display maximum and minimum ages of the students in the section.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

public class Student{
    private int StudentId;
    private  String StudentName;
    private int StudentAge;
    
    public Student(int studId,String stuName,int age)
    {
        StudentId=studId;
        StudentName=stuName;
        StudentAge=age;
    }
    public void setStudentId(int s){
        StudentId=s;
    }
    public int getStudentId()
    {
        return StudentId;
    }
    public void setStudentName(String StudentName){
        this.StudentName=StudentName;
    }
    public String getStudentName()
    {
        return StudentName;
    }
    public void setStudentAge(int StudentAge){
        this.StudentAge=StudentAge;
    }
    public int getStudentAge()
    {
        return StudentAge;
    }
}

 

public class Section{
    private Student arrayStu[];
    private int counter;
    
    public Section(int size)
    {
        arrayStu[]=new Student[size];
        counter=-1;
    }
    public boolean addStudent(Student stu)
    {
        if(counter<size-1) 
        {
            counter++;
        arrayStu[counter].setStudentId(stu.getStudentId());
        arrayStu[counter].setStudentName(stu.getStudentName());
        arrayStu[counter].setStudentAge(stu.getStudentAge());
        return True;
        }
        else //this case happened when counter = size-1
        {
            System.out.println("array of students is full!");
            return false;
        }
    }
    public int searchStudent(int stuid)
    {
        for(int i=0;i<=this.counter;i++)
        {
            if(stuid==arrayStu[i].getStudentId())
            return i;
        }
        return -1;
    }
    public int calculateTotalAges()
    {
        int totalAges=0;
        for(int i=0;i<=this.counter;i++)
        {
            totalAges=totalAges+arrayStu[i].getStudentAge();
        }
        return totalAges;
    }
    public double calculateAvgAge()
    {
        return calculateTotalAges()/(counter+1);
    }
    public void printStuData(int stuid)
    {
        int studentIndex=searchStudent(stuid);
        if(studentIndex==-1)
        System.out.println("the student with id : " + stuid+" is not exsiting!");
        else System.out.println("student name : "+arrayStu[studentIndex].getStudentName+", student age: "+arrayStu[studentIndex].getStudentAge());
    }
    public boolean deleteStu(int stuid)
    {
        int studentIndex=searchStudent(stuid);
        if(studentIndex==-1)
        System.out.println("the student with id : " + stuid+" is not exsiting!");
        else {
        for(int i=studentIndex;i<=this.counter;i++)
        {
            arrayStu[i]=arrayStu[i+1]; //shiftleft
        }
            counter--;// decrease the number of exsiting students
        }
    }
    public int findMaxAge()
    {
        int maxAge=-1;//default value=-1 means that there is no max age at all, because there no students.
        if(counter>-1){
        maxAge=arrayStu[0].getStudentAge();
        for(int i=0;i<=this.counter;i++)
        {
            if(arrayStu[i].getStudentAge()>maxAge)
            maxAge=arrayStu[i];
        }
        return maxAge;
        }
    }
    public int findMinAge()
    {
        int minAge=-1;//default value=-1 means that there is no min age at all, because there no students.
        if(counter>-1){
        minAge=arrayStu[0].getStudentAge();
        for(int i=1;i<=this.counter;i++)
        {
            if(arrayStu[i].getStudentAge()<minAge)
            minAge=arrayStu[i];
        }
        return minAge;       
    }   
}

 

public class Test
{
	public static void main(String[] args) {
	    Section sec=new Section(10);
	    Student s1=new Student(1,"adnan",15);
	    Student s2=new Student(2,"mohammad",18);
	    Student s3=new Student(3,"jhon",10);
	    Student s4=new Student(4,"george",12);
	    Student s5=new Student(5,"ali",19);
	    sec.addStudent(s1);
	    sec.addStudent(s2);
	    sec.addStudent(s3);
	    sec.addStudent(s4);
	    sec.addStudent(s5);
	    sec.deleteStu(2);
	    System.out.println(sec.findMaxAge());
	    System.out.println(sec.findMinAge());
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< convert simple uml ServiceQuote class diagram to j...