OnlineStudent a subclass of Student add currentLocation and

- OnlineStudent: a subclass of Student; add “currentLocation” and “currentAffiliation” as data members


-   OnlineUAClass: a subclass of UAClass; add “onlineTAFirstName,” “onlineTALastName” as data members


In the OnlineStudent class, implement the GPA method such that in addition to GPA calculation, it will print out the currentLocation and currentAffiliation.


In the Student’s main() method, initiate one OnlineStudent object and one Student object, and print out their GPAs.

}

Solution

import java.util.*;
import java.lang.*;
import java.io.*;

class Student {
String firstName;
String lastName;
String departmentIn;
int yearGraduation;
Class[] classes;
double [] grades;

public Student(String firstName, String lastName, String departmentIn, int yearGraduation, Class[] classes, double[] grades) {
this.firstName = firstName;
this.lastName = lastName;
this.departmentIn = departmentIn;
this.yearGraduation = yearGraduation;
this.classes = classes;
this.grades = grades;
}
  

public static void main(String[] args) {

Class c1 = new Class(\"t\",\"1\",\"first\",5); //initialize 3 Class objects
Class c2 = new Class(\"t\",\"2\",\"second\",3);
Class c3 = new Class(\"t\",\"3\",\"first\",7);
Class[] cls = {c1,c2,c3}; //add Class objects to an array
      
       OnlineUAClass oc1 = new OnlineUAClass(\"t\",\"4\",\"third\",8,\"Samara\",\"Jones\"); //initialize 2 Class objects
       OnlineUAClass oc2 = new OnlineUAClass(\"t\",\"6\",\"second\",6,\"Tom\",\"Black\");
      
       OnlineUAClass[] ocls = {oc1,oc2}; //add Class objects to an array
      
double gds[] = {7.8,8.6,9.0}; //initialize grades array
       double ogds[] = {6.8,7.6,7.0}; //initialize grades array
Student student = new Student(\"ABC\",\"XY\",\"first\",2012,cls,gds); //initialize a Student object
      
       OnlineStudent onlinestudent = new OnlineStudent(\"XYZ\",\"AB\",\"second\",2015,ocls,ogds,\"NY\",\"Oxford University\"); //initialize an Online Student object
      
System.out.println(\"GPA = \"+student.calculateGPA()); //print the students GPA
System.out.println(\"GPA = \"+onlinestudent.calculateGPA()); //print the online students GPA
}

public double calculateGPA(){
double sum = 0; //initialize sum
int cls = 0; //initialize class count
for(Class c : classes) //count the total no:of classes
cls++;
for(double g : grades){ //calculate the total sum of grades
sum = sum+g;
}
double gpa = sum/cls; //get the GPA
return gpa;
}

//Getters and setters for all member variables
public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getDepartmentIn() {
return departmentIn;
}

public void setDepartmentIn(String departmentIn) {
this.departmentIn = departmentIn;
}

public int getYearGraduation() {
return yearGraduation;
}

public void setYearGraduation(int yearGraduation) {
this.yearGraduation = yearGraduation;
}

public Class[] getClasses() {
return classes;
}

public void setClasses(Class[] classes) {
this.classes = classes;
}

public double[] getGrades() {
return grades;
}

public void setGrades(double[] grades) {
this.grades = grades;
}
}
class OnlineStudent extends Student
{
   private String currentLocation;
   private String currentAffiliation;
  
   public OnlineStudent(String firstName, String lastName, String departmentIn, int yearGraduation, Class[] classes, double[] grades,String currentLocation,String currentAffiliation)
   {
       super(firstName,lastName,departmentIn,yearGraduation,classes,grades);
       this.currentLocation = currentLocation;
       this.currentAffiliation = currentAffiliation;
   }
   public double calculateGPA()
   {
       double gpa;
       gpa = super.calculateGPA();
       System.out.println(\"Current Location \"+currentLocation);
       System.out.println(\"Current Affiliation \"+currentAffiliation);
       return gpa;
   }
  
   public void setCurrentLocation(String currentLocation)
   {
       this.currentLocation = currentLocation;
   }
   public String getCurrentLocation()
   {
       return currentLocation;
   }
   public void setCurrentAffiliation(String currentAffiliation)
   {
       this.currentAffiliation = currentAffiliation;
   }
   public String getCurrentAffiliation()
   {
       return currentAffiliation;
   }
}
class Class{
String teacherFirstName;
String teacherLastName;
String semesterOffered;
int numCredits;

public Class(String teacherFirstName, String teacherLastName, String semesterOffered, int numCredits) {
this.teacherFirstName = teacherFirstName;
this.teacherLastName = teacherLastName;
this.semesterOffered = semesterOffered;
this.numCredits = numCredits;
}


//Getters and setters for all member variables
public String getTeacherFirstName() {
return teacherFirstName;
}

public void setTeacherFirstName(String teacherFirstName) {
this.teacherFirstName = teacherFirstName;
}

public String getTeacherLastName() {
return teacherLastName;
}

public void setTeacherLastName(String teacherLastName) {
this.teacherLastName = teacherLastName;
}

public String getSemesterOffered() {
return semesterOffered;
}

public void setSemesterOffered(String semesterOffered) {
this.semesterOffered = semesterOffered;
}

public int getNumCredits() {
return numCredits;
}

public void setNumCredits(int numCredits) {
this.numCredits = numCredits;
}
}
class OnlineUAClass extends Class
{
   private String onlineTAFirstName;
   private String onlineTALastName;
  
   public OnlineUAClass(String teacherFirstName, String teacherLastName, String semesterOffered, int numCredits,String onlineTAFirstName,String onlineTALastName)
   {
       super(teacherFirstName,teacherLastName,semesterOffered,numCredits);
       this.onlineTAFirstName = onlineTAFirstName;
       this.onlineTALastName = onlineTALastName;
   }
   public String getOnlineTAFirstName()
   {
       return onlineTAFirstName;
   }
   public void setOnlineTAFirstName()
   {
       this.onlineTAFirstName = onlineTAFirstName;
   }
   public String getOnlineTALastName()
   {
       return onlineTALastName;
   }
   public void setOnlineTALastName()
   {
       this.onlineTALastName = onlineTALastName;
   }
}

output:

- OnlineStudent: a subclass of Student; add “currentLocation” and “currentAffiliation” as data members - OnlineUAClass: a subclass of UAClass; add “onlineTAFirs
- OnlineStudent: a subclass of Student; add “currentLocation” and “currentAffiliation” as data members - OnlineUAClass: a subclass of UAClass; add “onlineTAFirs
- OnlineStudent: a subclass of Student; add “currentLocation” and “currentAffiliation” as data members - OnlineUAClass: a subclass of UAClass; add “onlineTAFirs
- OnlineStudent: a subclass of Student; add “currentLocation” and “currentAffiliation” as data members - OnlineUAClass: a subclass of UAClass; add “onlineTAFirs

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site