Interpreting a short passage of the text to identify the best class candidates a
ID: 3902568 • Letter: I
Question
Interpreting a short passage of the text to identify the best class candidates as well as the Assume that someone has defined Course as an entity with attributes name, dept, year, - course description (a paragraph of text), inheritance, composition or aggregation relationships that might exist among them: semester (with values F "Fall, S "Summer", W "Winter"). Also each course is associated with: name of professor and his office number, and a phone extension (note that one instructor could teach several courses, one course could teach only one instructor). student enrollment list for the course(usually up to 30 students), average course student mark (average of all students from student enrollment list) First you have design a UML diagram Java implementation in class: Test class should have one professor and a list of students, and your program should calculate a course average student's mark for particular course. You may choose to use aggregation/ composition, inheritance/ abstract classes / interfaces - where necessaryExplanation / Answer
package inheritance;
import java.util.Scanner;
// Class Student definition
class Student
{
// Instance variable to store student information
String stuName;
String course;
double mark;
// Default constructor to initialize data member
Student()
{
stuName = course = "";
mark = 0;
}// End of default constructor
// Method to set name
void setName(String name)
{
stuName = name;
}// End of method
// Method to set course name
void setCourse(String course)
{
this.course = course;
}// End of method
// Method to set mark
void setMark(double mark)
{
this.mark = mark;
}// End of method
// Method to return name
String getName()
{
return stuName;
}// End of method
// Method to return course name
String getCourse()
{
return course;
}// End of method
// Method to return mark
double getMark()
{
return mark;
}// End of method
// Overrides toString() method to return student information
public String toString()
{
return " Student Name: " + getName() + " Course: " + getCourse() + " Marks: " + getMark();
}// End of method
}// End of class
// Defines a class Professor
class Professor
{
// Declares an array of object for student class
Student students[];
String professorName;
String contactNumber;
int count;
double avgMark;
// Default constructor to initialize instance variables
Professor()
{
// Calls the base class default constructor
super();
professorName = contactNumber = "";
avgMark = 0;
students = new Student[30];
count = 0;
}// End of default constructor
// Parameterized constructor to create a professor information
Professor(Student s[], String pn, String cn)
{
// Calls the base class default constructor
super();
students = s;
professorName = pn;
contactNumber = cn;
avgMark = 0;
count++;
}// End of parameterized constructor
// Method to set name
void setProfessorName(String name)
{
professorName = name;
}// End of method
// Method to set contact number
void setContactNumber(String no)
{
this.contactNumber = no;
}// End of method
// Method to return name
String getProfessorName()
{
return professorName;
}// End of method
// Method to return contact number
String getContactNumber()
{
return contactNumber;
}// End of method
// Method to return average mark
double getAvgMark()
{
return avgMark;
}// End of method
// Method to calculate and return average mark
double calculateAvgMark()
{
// Initializes total to zero
double total = 0;
// Loops till number of students
for(int c = 0; c < students.length; c++)
// Calculates total
total += students[c].mark;
// Calculates and returns average mark
return (total/students.length);
}// End of method
// Overrides to toString() to return professor information
public String toString()
{
// Calls the method to calculate average mark
avgMark = calculateAvgMark();
String result = "";
result += " Professor Name: " + getProfessorName() + " Contact Number: " + getContactNumber() + " Average Course Mark: " + getAvgMark();
result += " Students Enrolled ";
// Loops till number of students
for(int c = 0; c < students.length; c++)
// Calls the base each students toString() method and concatenates with result
result += students[c].toString();
// Returns the result
return result;
}// End of method
}// End of class Professor
// Driver class FacultyStudentTest definition
public class FacultyStudentTest
{
// Scanner class object declared
static Scanner sc;
// static method to accept students information
static void acceptStudent(Student st[])
{
String sna, cna;
double ma;
// Loops till number of students
for(int s = 0; s < st.length; s++)
{
st[s] = new Student();
// Accepts students information and assigns using setter method
System.out.print(" Enter student name: ");
sna = sc.nextLine();
st[s].setName(sna);
System.out.print(" Enter student course name: ");
cna = sc.nextLine();
st[s].setName(cna);
System.out.print(" Enter student mark: ");
ma = sc.nextDouble();
st[s].setMark(ma);
sc.nextLine();
}// End of for loop
}// End of method
// static method to accept professor information
static void acceptProfessor(Professor pf[])
{
String pna, con;
// Scanner class object created
sc = new Scanner(System.in);
// To store number of students
int no;
// Accepts number of students
System.out.print(" Enter number of students enrolled: ");
no = sc.nextInt();
sc.nextLine();
// Creates an array of objects for number of students of size no
Student st[] = new Student[no];
// Loops till number of professors
for(int p = 0; p < pf.length; p++)
{
// Accepts professor information
System.out.print(" Enter professor name: ");
pna = sc.nextLine();
System.out.print(" Enter professor contact number: ");
con = sc.nextLine();
// Calls the method to accept student information
acceptStudent(st);
// Creates an professor object using parameterized constructor
// and assigns it to index position p of array of object pf
pf[p] = new Professor(st, pna, con);
}// End of for loop
}// End of method
// main method definition
public static void main(String[] s)
{
// Scanner class object created
sc = new Scanner(System.in);
// To store number of professors
int no;
// Accepts number of professors
System.out.print(" Enter number of professors: ");
no = sc.nextInt();
// Creates an array of objects for class Professor of size no
Professor pf[] = new Professor[no];
// Calls the method to accept professor information
acceptProfessor(pf);
// Loops till number of professors
for(int p = 0; p < pf.length; p++)
// Displays professor information
System.out.print(pf[p]);
}// End of main method
}// End of driver class
Sample Output:
Enter number of professors: 2
Enter number of students enrolled: 2
Enter professor name: Pyari
Enter professor contact number: 12345612
Enter student name: ram
Enter student course name: Fall
Enter student mark: 45
Enter student name: Shyam
Enter student course name: Summer
Enter student mark: 78
Enter professor name: Mohan
Enter professor contact number: 4567891233
Enter student name: Sita
Enter student course name: Winter
Enter student mark: 88
Enter student name: Gita
Enter student course name: Summer
Enter student mark: 79
Professor Name: Pyari
Contact Number: 12345612
Average Course Mark: 83.5
Students Enrolled
Student Name: Winter Course: Marks: 88.0
Student Name: Summer Course: Marks: 79.0
Professor Name: Mohan
Contact Number: 4567891233
Average Course Mark: 83.5
Students Enrolled
Student Name: Winter Course: Marks: 88.0
Student Name: Summer Course: Marks: 79.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.