/* * Author: Stephanie Elzer * Date: October 10, 2006 * Purpose: To demonstrate basic class design, using a Student object */ public class Student { // Instance data private String MNumber, fName, mName, lName; private String addrLine, city, state, zip; private int month, day, year; private double earnedCredits; private double qualityPoints; private char gender; // Constructor that takes in id number, full name and gender public Student(String mnum, String first, String middle, String last, char studentGender) { MNumber = mnum; fName = first; mName = middle; lName = last; gender = studentGender; addrLine = ""; city = ""; state = ""; zip = ""; month = 0; day = 0; year = 0; earnedCredits = 0; qualityPoints = 0; } // Method that updates earned credits and quality points when a student completes a class // Returns the updated GPA; public double finishClass(double credits, double grade) { double qualityPts = credits * grade; earnedCredits += credits; qualityPoints += qualityPts; // calls the getGPA method and returns the result return getGPA(); } // Method for setting the complete birthdate (month, day and year) rather than calling separate setter methods public void setBirthdate(int newmonth, int newday, int newyear) { month = newmonth; day = newday; year = newyear; } // Method that returns the student's birthdate formatted as m-d-y public String getBirthdate() { String result; result = Integer.toString(month) + "-" + Integer.toString(day) + "-" + Integer.toString(year); return result; } // Method for setting complete address, rather than calling four separate setter methods public void setAddress(String addrline, String cityname, String statename, String zipcode) { addrLine = addrline; city = cityname; state = statename; zip = zipcode; } // Method that computes and returns the student's GPA. Note that the conditional test prevents dividing by 0 // if the student has not yet earned any credits public double getGPA() { return qualityPoints / earnedCredits; } // To String method for displaying the Student object public String toString() { String result = fName + " " + mName + " " + lName + "\n"; result += getBirthdate() + "\n"; result += addrLine + " " + city + ", " + state + " " + zip + "\n"; result += "Credits: " + earnedCredits + "\n"; result += "GPA: " + getGPA() + "\n"; return result; } // Getter and setter methods for all attributes of Student public String getAddressLine() { return addrLine; } public void setAddressLine(String addrLine) { this.addrLine = addrLine; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public int getBirthDay() { return day; } public void setBirthDay(int day) { this.day = day; } public double getEarnedCredits() { return earnedCredits; } public void setEarnedCredits(double earnedCredits) { this.earnedCredits = earnedCredits; } public String getFirstName() { return fName; } public void setFirstName(String name) { fName = name; } public char getGender() { return gender; } public void setGender(char gender) { this.gender = gender; } public String getLastName() { return lName; } public void setLastName(String name) { lName = name; } public String getMiddleName() { return mName; } public void setMiddleName(String name) { mName = name; } public String getMNumber() { return MNumber; } public void setMNumber(String number) { MNumber = number; } public int getBirthMonth() { return month; } public void setBirthMonth(int month) { this.month = month; } public double getQualityPoints() { return qualityPoints; } public void setQualityPoints(double qualityPoints) { this.qualityPoints = qualityPoints; } public String getState() { return state; } public void setState(String state) { this.state = state; } public int getBirthYear() { return year; } public void setBirthYear(int year) { this.year = year; } public String getZip() { return zip; } public void setZip(String zip) { this.zip = zip; } }