Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CODE IN JAVA: package model; import java.util.ArrayList; public abstract class P

ID: 3573380 • Letter: C

Question

CODE IN JAVA:

package model;

import java.util.ArrayList;

public abstract class Person {

      private int id;

      private String name;

      private static ArrayList<Person> persons=new ArrayList<>();

     

      public Person(int id, String name) {

            this.id = id;

            this.name = name;

            persons.add(this);

      }

      public int getId() {

            return id;

      }

      public void setId(int id) {

            this.id = id;

      }

      public String getName() {

            return name;

      }

      public void setName(String name) {

            this.name = name;

      }

     

      public static ArrayList<Person> getPersons() {

            return persons;

      }

     

      public abstract String getClassName();

      public String toCSV() {

            return id + "," + name +","+getClassName() ;

      }

}

The above mentioned is the Person class.Add a subclass called Student that has one private attribute called gpa (double). Add the setter and getter for gpa. Add a 3-parameter constructor. Implement the getClassName() method so that it returns the subclass name. Also override toCSV() method so that it returns a string representing id, name and gpa (formatted to have two digits after the decimal point) .Then,Add a subclass called Staff that has one private attribute called jobTitle (String). Add the setter and getter for jobTitle. Add a 3-parameter constructor. Implement the getClassName() method so that it returns the subclass name. Also override toCSV() method so that it returns a string representing id, name and jobTitle.Then,the static ArrayList called persons which is declared in the Person class, and an enhanced for-loop, perform the following: [hint: use instanceof operator if needed]

- compute the total GPA for all the students, and also the total number of students.

- use toCSV() to display the objects details, each on a separate line.

After the loop, print the average gpa (formatted to 2 decimal places) and the number of students. Each value should be properly labeled.Using an enhanced for-loop output the name and gpa of the students whose gpa is above the average.

Explanation / Answer

Student.java:

package Model;

public class Student extends Person {

  
   private double gpa;

   public Student(int id, String name, double gpa) {
       super(id, name);
       this.gpa=gpa;
       // TODO Auto-generated constructor stub
   }

   public double getGpa() {
       return gpa;
   }

   public void setGpa(double gpa) {
       this.gpa = gpa;
   }

   @Override
   public String getClassName() {
       // TODO Auto-generated method stub
       return this.getClass().getSimpleName();
      
   }

   public String toCSV(){
       String num = String.format("%.2f",this.gpa);
       return super.getId() + "," + super.getName() +","+ num ;
   }
  
}

Staff.java:

package Model;

public class Staff extends Person {

   private String jobTitle;

   public String getJobTitle() {
       return jobTitle;
   }

   public void setJobTitle(String jobTitle) {
       this.jobTitle = jobTitle;
   }

   public Staff(int id, String name, String jobTitle) {
       super(id, name);
       this.jobTitle=jobTitle;
       // TODO Auto-generated constructor stub
   }

   @Override
   public String getClassName() {
       // TODO Auto-generated method stub
       return this.getClass().getSimpleName();
   }
  
   public String toCSV(){
       return super.getId() + "," + super.getName() +","+ jobTitle ;
   }
}

Driver.java

package Model;

public class Driver {

   public static void main(String args[]){
   Person p1=new Student(1,"p1",9.0);
   Person p2=new Staff(2,"p2","Teacher");
   Person p3=new Student(3,"p3",8.0);
   Person p4=new Student(1,"p4",9.1);

   double gpaSum=0.0;
   int count=0;
  
      
   for(Person p : Person.getPersons()){
       if(p instanceof Student){
           count++;
           gpaSum+=((Student) p).getGpa();
           System.out.println(p.toCSV());
       }
   }
  
   System.out.println("Student count: "+ count);
   double avg=gpaSum/count;
   String num = String.format("%.2f",avg);
   System.out.println("Average GPA: " + num);
  
   System.out.println(" Students with GPA greater than average GPA: ");

   for(Person p : Person.getPersons()){
       if(p instanceof Student){
           if(((Student) p).getGpa()>=avg){
               System.out.println("Student Name: "+ p.getName() + " Student GPA: "+ ((Student) p).getGpa());
           }
       }
}
   }
}

Sample run:

1,p1,9.00
3,p3,8.00
1,p4,9.10
Student count: 3
Average GPA: 8.70

Students with GPA greater than average GPA:
Student Name: p1   Student GPA: 9.0
Student Name: p4   Student GPA: 9.1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote