Ads

Sunday, December 27, 2020

Create a class "Student" with three data members which are name, age and address. The constructor of the class assigns default values name as "unknown", age as "0" and address as "not available". It has two members with the same name "setInfo()". First method has two parameters for name and age and assigns the same whereas the second method takes has three parameters which are assigned to name, age and address respectively. Print the name, age and address of 5 students.


Here is the code to implement the scenario described


Source Code


package student;

 //@author Java_Programs

public class Student {


    String name , address;

    int age;

    

    public Student(){

        

    this.name = "Unknown";

    this.address ="Not available" ;

    this.age = 0; 

    }


    public static void main(String[] args) {


        Student s = new Student();

        s.setInfo("Ali", "H#1123", 20);

     

        Student s1 = new Student();

        s1.setInfo("Asad", "H#1234", 22);

     

        Student s2 = new Student();        

        s2.setInfo("Ahad", "H#1232", 25);

        

        Student s3 = new Student();        

        s3.setInfo("Sunny", "H#fht12/2" , 22);  

    

        Student s4 = new Student();        

        s4.setInfo("Akhtar", "H#ght33", 24);

    }

    void setInfo(String name , int age){

    this.name = name;

    this.age = age;

    System.out.println("Name is: "+name+ "\tAge is: "+age);

    }

    void setInfo(String name ,String address , int age){

    this.name = name;

    this.address = address;

    this.age = age;

    System.out.println("Name : "+name+"\tAddress : "+address+"\tAge : "+age);

    }

}


Explanation:

The given code defines a Java class named "Student" which has three data members, namely "name", "address", and "age". The class also has a default constructor and two parameterized constructors. The class has two methods named "setInfo", which are overloaded, to set the values of the data members.

 

In the default constructor, the values of the data members are initialized with default values, i.e., "Unknown" for name, "Not available" for address, and 0 for age.

 

In the main method, five objects of the class "Student" are created using the "new" keyword and the "setInfo" method is called for each object to set the values of the data members.

 

The first object "s" is initialized with name "Ali", address "H#1123", and age 20. Similarly, the second object "s1" is initialized with name "Asad", address "H#1234", and age 22, and so on for the other objects.

The overloaded method "setInfo" takes two parameters, i.e., "name" and "age", and prints the values of name and age on the console.

The other overloaded method "setInfo" takes three parameters, i.e., "name", "address", and "age", and prints the values of all three data members on the console.

 

Concept: 

The concept of overloading is used in this program where two methods are named the same but differ in the number and type of parameters they take. This is done to provide flexibility to the user while using the class. The user can choose which method to call based on their requirements.


No comments:

Post a Comment