Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Add new features and specifications to the app you developed in Week 10. Develop a class called OnlineUniversity that extends the University class. The new
Add new features and specifications to the app you developed in Week 10.
- Develop a class called OnlineUniversity that extends the University class. The new class "is a" university that delivers its courses online.
- Two new attributes added to the online university:
- -URL which is a string that represents the URL of the online university. For example"monash.edu" and "deakin.com.au"
- -OnlineAccounts which is an array list of strings that contains the students' accounts for the online university
- Develop a constructor that accepts the online university attribute (URL) and the university's attributes.
- Override the addStudent method such that
- -It adds the new students to the students' array list (hint: parent class task)
- -It creates an online account for the student and adds it to the onlineAccounts array list as demonstrated in the example below:
- *take the first two letters from the student's name and add to it three random digits
- *Example: If the URL is "monash.edu" and the student name is "Ellen" then the account is "el942@monash.edu".
- *Example: If the URL is "monash.edu" and the student name is "Max" then the account is "ma013@monash.edu".
- Override the toStirng method to print the attributes of the parent and child classes )hint: use super)
- Update the driver class (main method) to test the new class
This is the code for week 10
public class Enrollment { //Attributes of the class private Student student; private Faculty faculty; // Getters and setters public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public Faculty getFaculty() { return faculty; } public void setFaculty(Faculty faculty) { this.faculty = faculty; } // Constructor of the class public Enrollment(Student student, Faculty faculty) { this.student = student; this.faculty = faculty; } }
import java.util.ArrayList; public class Faculty { // Attribute of class private String name; private int capacity; // Getters and Setters public String getName() { return name; } public void setName(String name) { this.name = name; } public int getCapacity() { return capacity; } public void setCapacity(int capacity) { this.capacity = capacity; } // Constructor of class public Faculty(String name, int capacity) { super(); this.name = name; this.capacity = capacity; } }
public class Student { // Attributes of class private String name; private int age; private double fees; // Constructor of class public Student(String name, int age, double fees) { this.name = name; this.age = age; this.fees = fees; } // Getters and setters public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getFees() { return fees; } public void setFees(double fees) { this.fees = fees; } }
public ArrayListgetEnrollments() { return this.enrollments; } // add students in the University public int addStudent(Student student) { for (int i = 0; i < this.students.size(); i++) { if (students.get(i) == student) return -1; } this.students.add(student); return 0; } // add faculty members in the University public int addFaculty(Faculty faculty) { for (int i = 0; i < this.faculties.size(); i++) { if (faculties.get(i) == faculty) return -1; } this.faculties.add(faculty); return 0; } // add enrolments in the University public int addEnrollments(Faculty faculty, Student student) { if (isFaculty(faculty) == true && isStudent(student) == true && countStudentsFaculty(faculty) < faculty.getCapacity()) { Enrollment enrollment = new Enrollment(student, faculty); // String res = student + "|" + faculty; this.enrollments.add(enrollment); return 0; } return -1; } // check if a person is a student or not public boolean isStudent(Student student) { for (int i = 0; i < this.students.size(); i++) { if (students.get(i) == student) return true; } return false; } // check if a person is a faculty or not public boolean isFaculty(Faculty faculty) { for (int i = 0; i < this.faculties.size(); i++) { if (faculties.get(i) == faculty) return true; } return false; } // method to check how many students are under a faculty public int countStudentsFaculty(Faculty faculty) { int count = 0; for (int i = 0; i < this.enrollments.size(); i++) { Enrollment enrollment = enrollments.get(i); if (enrollment.getFaculty().getName().equals(faculty.getName())) { // String aux = ""; // int j = 0; // while (str.charAt(j) != '|') // j++; // j++; // while (j < str.length()) { // aux += str.charAt(j); // j++; // } // if (aux.equals(faculty.getName())) { count++; } } return count; } // deafult constructor where we are initializing the values from our side public University() { setName("XXX"); setWorldRank(10000); students = new ArrayList (100); faculties = new ArrayList (100); enrollments = new ArrayList (100); } // parameterized constructor public University(String name, int rank) { setName(name); setWorldRank(rank); students = new ArrayList (100); faculties = new ArrayList (100); enrollments = new ArrayList (100); } }
public class Main { // main method of the class public static void main(String[] args) { University uni = new University("CMU", 2); // Creating student objects Student john = new Student("John", 21, 1200); Student tim = new Student("Tim", 22, 1200); Student emma = new Student("Emma", 21, 1200); Student sally = new Student("Sally", 23, 1200); // adding student objects uni.addStudent(john); uni.addStudent(tim); uni.addStudent(emma); uni.addStudent(sally); // Creating faculty objects Faculty fit = new Faculty("FIT", 3); Faculty law = new Faculty("LAW", 3); // Adding faculty objects uni.addFaculty(fit); uni.addFaculty(law); // Creating Enrollments uni.addEnrollments(fit, john); uni.addEnrollments(law, tim); uni.addEnrollments(fit, emma); uni.addEnrollments(fit, sally); uni.addEnrollments(law, sally); // testing method countStudentsFaculty System.out.println(uni.countStudentsFaculty(fit)); System.out.println(uni.countStudentsFaculty(law)); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started