Student and Department Detail Program in java

Question:

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 sameple 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.

Sample Input 1:

Enter the Department id:
100
Enter the Department name:
ComputerscienceEnter the Student id:
123
Enter the Student name:
sudha

Sample Output 1:

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

CODE:

import java.util.*;
public class TestMain {
public static Department d1;
public static Student s1;
public static Student createStudent(){
    Scanner sc = new Scanner(System.in);
    s1 = new Student();
    d1 = new Department();
    System.out.println("Enter the Department id:");
    int did = sc.nextInt();
    sc.nextLine();
    System.out.println("Enter the  Department name:");
    String dname=sc.nextLine();
    System.out.println("Enter the Student id:");
    int sid = sc.nextInt();
    sc.nextLine();
    System.out.println("Enter the Student name:");
    String sname=sc.nextLine();
    d1.setDid(did);
    d1.setDname(dname);
    s1.setSid(sid);
    s1.setSname(sname);
    s1.setDepartment(d1);
    return s1;
}
public static void main(String args[]){
    s1=TestMain.createStudent();
    System.out.println("Department id:"+d1.getDid());
    System.out.println("Department name:"+d1.getDname());
    System.out.println("Student id:"+s1.getSid());
    System.out.println("Student name:"+s1.getSname());
}
}
public class Student {
    private int sid;
    private String sname;
    private Department department;
    public void setSid(int sid) {
        this.sid=sid;
    }
    public int getSid(){
        return sid;
    }
    public void setSname(String sname) {
        this.sname=sname;
    }
    public String getSname() {
        return sname;
    }
    public void setDepartment(Department department){
        this.department=department;
    }
    public Department getDepartment(){
        return department;
    }
}
public class Department {
    private int did;
    private String dname;
    public void setDid(int did){
        this.did=did;
    }
    public int getDid(){
        return did;
    }
    public void setDname(String dname){
        this.dname=dname;
    }
    public String getDname() {
        return dname;
    }
}
Next:
  1. Array square
  2. Generate number using odd digits
  3. Alternate Number Difference
  4. Next Greatest number

Leave a Comment

Scroll to Top