Answer to Question #231854 in Java | JSP | JSF for jerry

Question #231854

Write a menu driven application to maintain the student information using Java to

demonstrate the concept of inheritance. Assume that you are considering PG and UG

students, where the specialization details are maintained only for the PG students. Your

application must contain the following functionalities along with the use of method

overriding, and super keyword.


a. For each student your application must have the details such as name,

Register Number , department, specialization, cgpa, Hostel Name,

Mentor Name, Number of Arrears.

b. Get the student details from user(admin)

c. Display the student list with all details in a proper and neat format.

d. In the menu give option to display the PG and UG student list

separately.


1
Expert's answer
2021-09-01T23:57:23-0400
import java.util.ArrayList;
import java.util.Scanner;


abstract class Student {
	private String name;
	private String registerNumber;
	private String department;
	private String specialization;
	private int cgpa;
	private String hostelName;
	private String mentorName;
	private int numberArrears;


	public Student() {


	}


	public Student(String name, String registerNumber, String department, String specialization, int cgpa,
			String hostelName, String mentorName, int numberArrears) {
		this.name = name;
		this.registerNumber = registerNumber;
		this.department = department;
		this.specialization = specialization;
		this.cgpa = cgpa;
		this.hostelName = hostelName;
		this.mentorName = mentorName;
		this.numberArrears = numberArrears;
	}


	@Override
	public String toString() {
		return "Name: " + this.name + "\n" + "Register number: " + this.registerNumber + "\n" + "Department: "
				+ this.department + "\n" + "Specialization: " + this.specialization + "\n" + "CGPA: " + this.cgpa + "\n"
				+ "Hostel name: " + this.hostelName + "\n" + "Mentor name: " + this.name + "\n" + "Number arrears: "
				+ this.numberArrears + "\n";
	}


	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}


	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}


	/**
	 * @return the registerNumber
	 */
	public String getRegisterNumber() {
		return registerNumber;
	}


	/**
	 * @param registerNumber the registerNumber to set
	 */
	public void setRegisterNumber(String registerNumber) {
		this.registerNumber = registerNumber;
	}


	/**
	 * @return the department
	 */
	public String getDepartment() {
		return department;
	}


	/**
	 * @param department the department to set
	 */
	public void setDepartment(String department) {
		this.department = department;
	}


	/**
	 * @return the specialization
	 */
	public String getSpecialization() {
		return specialization;
	}


	/**
	 * @param specialization the specialization to set
	 */
	public void setSpecialization(String specialization) {
		this.specialization = specialization;
	}


	/**
	 * @return the cgpa
	 */
	public int getCgpa() {
		return cgpa;
	}


	/**
	 * @param cgpa the cgpa to set
	 */
	public void setCgpa(int cgpa) {
		this.cgpa = cgpa;
	}


	/**
	 * @return the hostelName
	 */
	public String getHostelName() {
		return hostelName;
	}


	/**
	 * @param hostelName the hostelName to set
	 */
	public void setHostelName(String hostelName) {
		this.hostelName = hostelName;
	}


	/**
	 * @return the mentorName
	 */
	public String getMentorName() {
		return mentorName;
	}


	/**
	 * @param mentorName the mentorName to set
	 */
	public void setMentorName(String mentorName) {
		this.mentorName = mentorName;
	}


	/**
	 * @return the numberArrears
	 */
	public int getNumberArrears() {
		return numberArrears;
	}


	/**
	 * @param numberArrears the numberArrears to set
	 */
	public void setNumberArrears(int numberArrears) {
		this.numberArrears = numberArrears;
	}


}


class PG extends Student {
	private int level;


	public PG(String name, String registerNumber, String department, String specialization, int cgpa, String hostelName,
			String mentorName, int numberArrears, int level) {
		super(name, registerNumber, department, specialization, cgpa, hostelName, mentorName, numberArrears);
		this.level = level;
	}


	@Override
	public String toString() {
		return super.toString() + "Level: " + this.level + "\n";
	}


	/**
	 * @return the level
	 */
	public int getLevel() {
		return level;
	}


	/**
	 * @param level the level to set
	 */
	public void setLevel(int level) {
		this.level = level;
	}


}


class UG extends Student {
	private int fees;


	public UG(String name, String registerNumber, String department, String specialization, int cgpa, String hostelName,
			String mentorName, int numberArrears, int fees) {
		super(name, registerNumber, department, specialization, cgpa, hostelName, mentorName, numberArrears);
		this.fees = fees;
	}


	@Override
	public String toString() {
		return super.toString() + "Fees: " + this.fees + "\n";
	}


	/**
	 * @return the fees
	 */
	public int getFees() {
		return fees;
	}


	/**
	 * @param fees the fees to set
	 */
	public void setFees(int fees) {
		this.fees = fees;
	}
}


public class StudentsApp {


	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		ArrayList<Student> students = new ArrayList<Student>();
		int ch = -1;
		while (ch != 5) {


			System.out.println("1. Add a new student");
			System.out.println("2. Display all students");
			System.out.println("3. Display PG students");
			System.out.println("4. Display UG students");
			System.out.println("5. Exit");
			System.out.print("Your choice: ");
			ch = input.nextInt();
			switch (ch) {
			case 1: {
				System.out.println("1. Add PG student");
				System.out.println("2. Add UG student");
				System.out.print("Your choice: ");
				int selectedStudent = input.nextInt();
				String name = "";
				String registerNumber = "";
				String department = "";
				String specialization = "";
				int cgpa = 0;
				String hostelName = "";
				String mentorName = "";
				int numberArrears = 0;
				input.nextLine();
				if (selectedStudent == 1 || selectedStudent == 2) {
					System.out.print("Enter student' name: ");
					name = input.nextLine();
					System.out.print("Enter student' register number: ");
					registerNumber = input.nextLine();
					System.out.print("Enter student' department: ");
					department = input.nextLine();
					System.out.print("Enter student' specialization: ");
					specialization = input.nextLine();
					System.out.print("Enter student' cgpa: ");
					cgpa = input.nextInt();
					input.nextLine();
					System.out.print("Enter student' hostel name: ");
					hostelName = input.nextLine();
					System.out.print("Enter student' mentor name: ");
					mentorName = input.nextLine();
					System.out.print("Enter student' number of arrears: ");
					numberArrears = input.nextInt();
				}


				if (selectedStudent == 1) {
					System.out.print("Enter student' level: ");
					int level = input.nextInt();
					students.add(new PG(name, registerNumber, department, specialization, cgpa, hostelName, mentorName,
							numberArrears, level));


					System.out.println("\nA new student has been added.\n");
				} else if (selectedStudent == 2) {
					System.out.print("Enter student' fees: ");
					int fees = input.nextInt();
					students.add(new UG(name, registerNumber, department, specialization, cgpa, hostelName, mentorName,
							numberArrears, fees));
					System.out.println("\nA new student has been added.\n");
				} else {
					System.out.println("Wrong selection.");
				}


			}
				break;
			case 2: {
				System.out.println("All students:");
				for (int i = 0; i < students.size(); i++) {
					System.out.println(students.get(i).toString());
				}
			}
				break;
			case 3: {
				System.out.println("All PG students:");
				for (int i = 0; i < students.size(); i++) {
					if(students.get(i) instanceof PG) {
						System.out.println(students.get(i).toString());
					}
				}
			}
				break;
			case 4: {
				System.out.println("All UG students:");
				for (int i = 0; i < students.size(); i++) {
					if(students.get(i) instanceof UG) {
						System.out.println(students.get(i).toString());
					}
				}
			}
				break;
			case 5:
				// exit
				break;
			}
		}


		input.close();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS