Create a new Java class named Student that has the following fields.

QUESTION

Write java code using Classes and Objects for the following:

Create a new Java class named Student that has the following fields:                

a) yearOfAdmission – The yearOfAdmission field is a calendar data type that holds the year of admitting the student. (e.g. 2021)

b) Name – A name field is a String object that holds the name of the student. (e.g. “Ahlam")

c) Marks – The marks field is a integer that holds the total marks of the student scored out of 500(e.g. 399)

 

In addition to that, the student class should have the following methods.

a) Constructor – The constructor should accept yearOfAdmission, name, and marks as arguments. These values should be used to initialize the year of admitting student, name of the student and marks field                   

b) Getter Methods – Write three accessor (getter) methods to get the values stored in an object’s fields

getYear(), getName(), getMarks()

c) Update() method to change the name of the student by passing studentID as an argument to the method.

 

SUMMARY

A class named Student is implemented to hold student details. Certain getter functions are used as the data members of the student class are private. Getter functions are used to return the details about a student. Update functions and display methods are also defined.

 

EXPLANATION

The class Student has id, name, marks, and yearOfAdmission as the members of the class. As these members are private to access in main, getter functions getName, getMarks, and getYear are defined and used. A display method is also used in this program to display the student details. A student object array is passed on to the method update which updates the array. (The array is searched for the student having a certain id, whose name is updated to a new name) . All the above methods are then verified in the main function.

 

CODE

import java.util.*;

class Student 

{

    int studentID;

    private String Name;

    private int marks;

    private int yoa;

    /* constructor */

    Student(int id,String n,int m,int y)

    {

        studentID=id;

        name=n;

        marks=m;

        yoa=y;

    }

    int getYear()

    {

        return yoa;

    }

    String getName()

    {

        return name;

    }


    int getMarks()

    {

        return marks;

    }

    void display()

    {

        System.out.println("ID: "+studentID);

        System.out.println("Name: "+name);

        System.out.println("Marks out of 500: "+marks);

        System.out.println("Year of Admission: "+yoa+"\n");

    }

    void update(int id,Student st[],int n,String na)

    {

        for(int i=0;i<n;i++)

        {

            if(st[i].studentID==id)

            {

                st[i].Name=na;

                st[i].display();

                break;

            }

        }

    }

}

class Main 

{

    public static void main(String args[])

    {

        Student st[]=new Student[3];

        st[0]=new Student(3240,"Tom",467,2020);

        st[1]=new Student(3298,"Bill",498,2021);

        st[2]=new Student(3109,"Monika",399,2019);

        System.out.println("Student Details:");

        for(int i=0;i<3;i++)

        st[i].display();

        System.out.println("After updating:");

        st[2].update(3109,st,3,"Rose");

    }

}

 

OUTPUT

 

 

Also Read: Class c is a child class of B which is class of A. A, B, and C all have destructor and constructor functions.

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *