Untitled
unknown
plain_text
4 years ago
6.0 kB
16
Indexable
Here is the detailed Java code
import java.util.Arrays;
import java.util.Scanner;
public class Temp {
/*
This is main method from where our code will run
*/
public static void main(String[] args) {
// Creating 2 students
Student s1 = new Student("Sanjay", 1, "Computer Science");
Student s2 = new Student("Mohit", 2, "Information Technology");
// Creating 2 PhD Students
StudentPhD sp1 = new StudentPhD("Shayan", 60, "Computer Science", "Android Developer", "Prof. Dr. Roohie Naaz");
StudentPhD sp2 = new StudentPhD("Raman", 60, "BCA", "Web Developer", "Prof. Dr. Ranjeet Rawat");
// Creating the object of College Class
College NIT_SRINAGAR = new College(5, 5);
// Adding students to college
NIT_SRINAGAR.add(s1);
NIT_SRINAGAR.add(s2);
// Adding phd students to college
NIT_SRINAGAR.addPhDStudent(sp1);
NIT_SRINAGAR.addPhDStudent(sp2);
// Finding the phd student by their supervisor name
NIT_SRINAGAR.find("Prof. Dr. Roohie Naaz");
// printing the college
System.out.println(NIT_SRINAGAR);
}
}
class Student {
// Attributes
private String name;
private int id;
private String level;
// Creating Constructor with Arguments
public Student(String name, int id, String level) {
this.name = name;
this.id = id;
this.level = level;
}
// Creating method to getId and getLevel
public int getId() {
return this.id;
}
public String getLevel() {
return this.level;
}
// Creating display method to show the Attributes
public void display() {
System.out.println("Name is : " + this.name);
System.out.println("Id is : " + this.id);
System.out.println(" Level is : " + this.level);
}
}
// Creating the StudentPhD class which is inheriting the features from the Student class
class StudentPhD extends Student {
// Adding the attributes for the phdStudent
private String specialty;
private String supervisorName;
// Creating the constructor for phdStudent
public StudentPhD(String name, int id, String level, String specialty, String supervisorName) {
super(name, id, level);
this.specialty = specialty;
this.supervisorName = supervisorName;
}
// overriding the display method of parent class
@Override
public void display() {
super.display();
System.out.println("Specialty is : " + this.specialty);
System.out.println("Supervisor Name is : " + this.supervisorName);
}
// method to get supervisor name of the PhD Student
public String getSupervisorName() {
return supervisorName;
}
}
// Creating the College class
class College {
private int maxStud;
private int nbStud = 0; // Current number of Students
private Student[] studs;
private int maxPhdStud;
private int nbPhdStud = 0; // Current number of PhD Students
private StudentPhD[] phdStuds;
// Creating the constructor for the College class
public College(int maxStud, int maxPhdStud) {
this.maxPhdStud = maxPhdStud;
this.maxStud = maxStud;
studs = new Student[this.maxStud];
phdStuds = new StudentPhD[this.maxPhdStud];
}
// Creating add method to add the Student in the student array if there is available seat
// This method returns true if the student is added successfully, false otherwise
public boolean add(Student student) {
if (this.nbStud >= this.studs.length) {
System.out.println("There is no Seat available for the Student");
return false;
} else {
this.studs[nbStud++] = student;
return true;
}
}
/*
Since Java doesn't support operator overloading
I Added a method to add the PhD student
this method returns true if the PhD student is added successfully, false otherwise
<Operator Overloading can be easily use in C++ language, and you can relate it with this method, no big deal>
*/
public boolean addPhDStudent(StudentPhD studentPhD) {
if (this.nbPhdStud >= this.phdStuds.length) {
System.out.println("There is no Seat available for the PhD Student");
return false;
} else {
this.phdStuds[nbPhdStud++] = studentPhD;
return true;
}
}
/* writing the find method to find the PhD Student by their supervisorName
This method prints that PhD student's data whose supervisor name matches
else an error message will be displayed
*/
public void find(String supervisorName) {
boolean found = false;
for (StudentPhD studentPhD : phdStuds) {
if (studentPhD != null && supervisorName.strip().equalsIgnoreCase(studentPhD.getSupervisorName().strip().toLowerCase())) {
studentPhD.display();
System.out.println();
found = true;
}
}
if (!found) {
System.out.println("No Such Student Found!!\n");
}
}
// This method will execute automatically whenever we print the college using System.out.println(college);
@Override
public String toString() {
return "College{" +
"maxStud=" + maxStud +
", nbStud=" + nbStud +
", studs=" + Arrays.toString(studs) +
", maxPhdStud=" + maxPhdStud +
", nbPhdStud=" + nbPhdStud +
", phdStuds=" + Arrays.toString(phdStuds) +
'}';
}
}
Output :)
Name is : Shayan
Id is : 60
Level is : Computer Science
Specialty is : Android Developer
Supervisor Name is : Prof. Dr. Roohie Naaz
College{maxStud=5, nbStud=2, studs=[Student@7cca494b, Student@7ba4f24f, null, null, null], maxPhdStud=5, nbPhdStud=2, phdStuds=[StudentPhD@3b9a45b3, StudentPhD@7699a589, null, null, null]}
Thanks!! :))
Editor is loading...