Contact Details of Hosteller Program in java

Question:

SNMR College of Engineering and Technology wants to create an application to store their students details as well as the details of hostellers.

In case of any changes to be made to the attributes,  admin can update the details like room number and phone number of the hosteler.

Develop a program to implement this scenario.

Create a public class Student with  protected attributes :

int studentId

String name

int departmentId

String gender

String phone

Create a public class Hosteller with private attributes

String hostelName

int roomNumber

Make this class inherit the Student class, as it holds all the properties of Student. 

Use appropriate public getters and setters for both the classes.

Write a class Main with the main function.

In Main class get the input of the hosteller using the method :

public static Hosteller getHostellerDetails().

Invoke this method from the main method and then modify the room number and phone number, if needed.

Sample Input 1:

Enter the Details:
Student Id
1
Student Name
John
Department Id
101
Gender
Male
Phone Number
9876543210
Hostel Name
YMCA
Room Number
10
Modify Room Number(Y/N)
Y
New Room Number
11
Modify Phone Number(Y/N)
Y
New Phone Number
9876543121

Sample Output 1:

The Student Details
1 John 101 Male 9876543121 YMCA 11

Sample Input 2:
Enter the Details:
Student Id
2
Student Name
John Paul
Department Id
112
Gender
Male
Phone Number
9885526536
Hostel Name
YMBA
Room Number
5
Modify Room Number(Y/N)
N
Modify Phone Number(Y/N)
N


Sample Output 2:
The Student Details:
2 John Paul 112 Male 9885526536 YMBA 5

CODE:

Main.java

import java.util.Scanner;
public class Main{
    public static Hosteller getHostellerDetails()
    {
        Hosteller h1=new Hosteller();
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the Details:");
        System.out.println("Student Id");
        int a1=sc.nextInt();
        System.out.println("Student Name");
        sc.nextLine();
        String b1=sc.nextLine();
        System.out.println("Department Id");
        int c1=sc.nextInt();
        System.out.println("Gender");
        sc.nextLine();
        String d1=sc.nextLine();
        System.out.println("Phone Number");
        String e1=sc.nextLine();
        System.out.println("Hostel Name");
        String f1=sc.nextLine();
        System.out.println("Room Number");
        int g1=sc.nextInt();
        h1.setStudentId(a1);
        h1.setName(b1);
        h1.setDepartmentId(c1);
        h1.setGender(d1);
        h1.setPhone(e1);
        h1.setHostelName(f1);
        h1.setRoomNumber(g1);
        System.out.println("Modify Room Number(Y/N)");
        char i=sc.next().charAt(0);
        if(i=='Y')
        {
            System.out.println("New Room Number");
            int x1=sc.nextInt();
            h1.setRoomNumber(x1);
        }
        System.out.println("Modify Phone Number(Y/N)");
        char j=sc.next().charAt(0);
        if(j=='Y')
        {
            System.out.println("New Room Number");
            sc.nextLine();
            String y1=sc.nextLine();
            h1.setPhone(y1);
        }
        return h1;
    }
    public static void main(String args[])
    {
        Hosteller h2=new Hosteller();
        h2=getHostellerDetails();
        System.out.println("The Student Details");
        System.out.println(h2.getStudentId()+" "+h2.getName()+" "+h2.getDepartmentId()+" "+h2.getGender()+
        " "+h2.getPhone()+" "+h2.getHostelName()+" "+h2.getRoomNumber());
        
    }
}

student.java

public class Student
{
    protected int studentId;
    protected String name;
    protected int departmentId;
    protected String gender;
    protected String phone;
    public void setStudentId(int a)
    {
        studentId=1;
    }
    public void setName(String b)
    {
        name=b;
        
    }
    public void setDepartmentId(int c)
    {
        departmentId=c;
    }
    public void setGender(String e)
    {
        gender=e;
    }
    public void setPhone(String f)
    {
        phone=f;
    }
    public int getStudentId()
    {
        return studentId;
    }
    public int getDepartmentId()
    {
        return departmentId;
    }
    public String getName()
    {
        return name;
    }
    public String getGender()
    {
        return gender;
    }
    public String getPhone()
    {
        return phone;
    }
}

Hosteller.java

class Hosteller extends Student
{
    private String hostelName;
    private int roomNumber;
    public Hosteller()
    {
        hostelName=" ";
        roomNumber=0;
    }
    public void setHostelName(String x)
    {
        hostelName=x;
    }
    public void setRoomNumber(int y)
    {
        roomNumber=y;
    }
    public String getHostelName()
    {
        return hostelName;
    }
    public int getRoomNumber()
    {
        return roomNumber;
    }
}
  1. PF and Salary Calculation
  2. Check for Existence of Customer – equals method
  3. Account Manipulation – Abstract class
  4. Employee Loan Eligibility – Polymorphism

Leave a Comment

Scroll to Top