CS 1713, Answer to Student Info Practice Problem


package studentinfopkg;
public class StudentInfo {
   // fields
   private String myName;
   private int myCreditHours;
   private double myGradePoints;

   // constructor
   public StudentInfo(String name, int creditHours, double gradePoints) {
      myName = name;
      myCreditHours = creditHours;
      myGradePoints = gradePoints;
   }

   //accessors
   public String getName() {
      return myName;
   }

   public int getCreditHours() {
      return myCreditHours;
   }

   public double getGradePoints() {
      return myGradePoints;
   }

   public double computeGPA() {
      return myGradePoints / (double)myCreditHours;
   }

   public boolean isSenior() {
      if (myCreditHours >= 125 && computeGPA() >= 2.0)
         return true;
      else
         return false;
   }

   public String toString() {
      return "Name: " + myName +
         "\n   Credit Hours: " + myCreditHours +
         "\n   Grade Points: " + myGradePoints +
         "\n   GPA: " + computeGPA() +
         "\n   Senior: " + isSenior() + "\n";
   }
}

package studentinfopkg; public class StudentInfoTest { public static void main (String[] args) { StudentInfo student1 = new StudentInfo( "John Goodstudent", 90, 310); System.out.println(student1); StudentInfo student2 = new StudentInfo( "Joe Slacker", 125, 210); System.out.println(student2); StudentInfo student3 = new StudentInfo( "Jacob Suckup", 130, 400); System.out.println(student3); if (student1.isSenior()) System.out.println(student1.getName() + " is a Senior"); else System.out.println(student1.getName() + " is NOT a Senior"); } }
Name: John Goodstudent Credit Hours: 90 Grade Points: 310.0 GPA: 3.4444444444444446 Senior: false Name: Joe Slacker Credit Hours: 125 Grade Points: 210.0 GPA: 1.68 Senior: false Name: Jacob Suckup Credit Hours: 130 Grade Points: 400.0 GPA: 3.076923076923077 Senior: true John Goodstudent is NOT a Senior