Search code examples
javaclassobjectgetter-setter

How can I access the methods of Department class using Student class object


Create a class Department with the following private member variables

int did
String dname

Include appropriate getters and setters method in Department class.

Create a class Student with the following private member variables

int sid
String sname
Department department

Include appropriate getters and setters method in Student class.

Create a TestMain class which has main method.

In addition to main method, create a method

       public static Student createStudent() -  All input as shown in the sample input should be got in this method.  Set the values to the Student object and return that object

Note : In main method, invoke the createStudent method and print the details of the object returned by that method.

I have already tried creating a new object using the name of Department but I am not able to access the methods

import java.util.Scanner;

public class TestMain {

    public static Student student;
    public static Department department;

    public static void main(String args[]) {
        createStudent();
        System.out.println("Department id:" + department.getDid());
        System.out.println("Department name:" + department.getDname());
        System.out.println("Student id:" + student.getSid());
        System.out.println("Student name:" + student.getSname());
    }

    public static Student createStudent() {
        Scanner sc = new Scanner(System.in);
        student = new Student();
        department = new Department();
        System.out.println("Enter the Department id:");
        department.setDid(sc.nextInt());
        System.out.println("Enter the Department name:");
        department.setDname(sc.next());
        System.out.println("Enter the Student id:");
        student.setSid(sc.nextInt());
        System.out.println("Enter the Student name:");
        student.setSname(sc.next());
        return student;
    }
}

public class Student {

    private int sid;
    private String sname;
    private Department department;

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }
}

public class Department {

    private int did;
    private String dname;

    public int getDid() {
        return did;
    }

    public void setDid(int did) {
        this.did = did;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }
}

Sample Input 1:

Enter the Department id:
100
Enter the Department name:
Computerscience

Enter the Student id:
123
Enter the Student name:
Sudha

Sample Output 1:

Department id:100
Department name:Computerscience
Student id:123
Student name:sudha

Solution

  • you are missing:

    student.setDepartment(department);
    

    after that, you can do:

    student.getDepartment().getSname();