Question: Assignment: Write a class encaptulating the concept of a student (Student.java), a student has the following attributes: name (the length of name is limitted to
Assignment: Write a class encaptulating the concept of a student (Student.java), a student has the following attributes: name (the length of name is limitted to [2,50]), use the first 50 characters if the length is greater than 50, set name to null if less then 2. ssn (social security number in the format 000-00-0000), set to 000-00-0000 if invalid GPA (0.0-4.0), set to 0 if invalid Include all setter and getter method, and a constructor that accepts (name, ssn, GPA) overdide the toString method to return the student information in the following format: Steve Davis 000-00-0000 3.9
The code that I have written so far is:
public class Student{
private String name; private String ssn; private double gpa;
public Student(){ } public Student(String name, String ssn, double gpa){ setName(name); setSsn(ssn); setGpa(gpa); }
public String getName(){ return name; } public String getSsn(){ return ssn; } public double getGpa(){ return gpa; } public void setName(String name){ if(name.length() < 2) name = null; else if(name.length() > 50) name = name.substring(0,50); this.name = name; } public void setSsn(String ssn){ this.ssn = ssn; } public void setGpa(double gpa){ if(gpa < 0 || gpa > 4) gpa = 0; this.gpa = gpa; } public String toString(){ return String.format("%s:%30s%3dcr",name,ssn,gpa); }
}
I would like to know how to write the code under the setter of ssn. The format that is being asked is a specifc case and I am not sure how to do it.
Also please let me know if there is anything else wrong with the code i currently have. I appreciate the help.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
