For this program, you should modify the class you defined from the previous modu
ID: 3641567 • Letter: F
Question
For this program, you should modify the class you defined from the previous module to include additional instance methods. (The code from previous module assignment is pasted below this assignment's requirements.)Requirements
1. Begin with the class that you previously defined in the last small program on constructors
2. Provide one additional instance method for this class (use parameters as needed) to achieve something interesting for your class (for example, if you created a Person class with an age attribute, you could create a yearsUntilRetirement( ) method)
3. Modify the main function/client program from the previous program you wrote such that the main program also invokes(or tests) this new method you've added to your class.
4. Ensure that the method is being invoked correctly (perhaps by printing something within each or show that the method is having some change on the objects when they are invoked).
------------*****MAIN CLASS****----------------
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Student s1 = new Student( "Jones", "222-88-1111", 3.7, "03/25/86" );
Student s2 = new Student( "Smith", "333-99-4444", 3.2, "06/05/95" );
System.out.println( "The name of student #1 is " + s1.getName( ) );
System.out.println( "The social security number of student #1 is " + s1.getSsn( ) );
System.out.println( "The gpa of student #1 is " + s1.getGpa( ) );
System.out.println( "The D.O.B of student #1 is " + s1.getDob( ) );
System.out.println( );
System.out.println( "The name of student #2 is " + s2.getName( ) );
System.out.println( "The social security number of student #2 is " + s2.getSsn( ) );
System.out.println( "The gpa of student #2 is " + s2.getGpa( ) );
System.out.println( "The D.O.B of student #2 is " + s2.getDob( ) );
}
}
---------*****Student Class****--------------
public class Student {
private String name;
private String ssn;
private double gpa;
private String dob;
public Student( String newName, String newSsn, double newGpa, String newDob )
{
setName( newName );
setSsn( newSsn );
setGpa( newGpa );
setDob( newDob );
}
public String getName( )
{
return name;
}
public void setName( String newName )
{
name = newName;
}
public String getSsn( )
{
return ssn;
}
public void setSsn( String newSsn )
{
ssn = newSsn;
}
public double getGpa( )
{
return gpa;
}
public String getDob( )
{
return dob;
}
public void setDob( String newDob )
{
dob = newDob;
}
public void setGpa( double newGpa )
{
gpa = newGpa;
}
}
Explanation / Answer
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Student s1 = new Student("Jones", "222-88-1111", 3.7, "03/25/1986");
Student s2 = new Student("Smith", "333-99-4444", 3.2, "06/05/1995");
System.out.println("The name of student #1 is " + s1.getName());
System.out.println("The social security number of student #1 is " + s1.getSsn());
System.out.println("The gpa of student #1 is " + s1.getGpa());
System.out.println("The D.O.B of student #1 is " + s1.getDob());
System.out.println("The retirement years left of student #1 is " + s1.yearsUntilRetirement(65));
System.out.println();
System.out.println("The name of student #2 is " + s2.getName());
System.out.println("The social security number of student #2 is " + s2.getSsn());
System.out.println("The gpa of student #2 is " + s2.getGpa());
System.out.println("The D.O.B of student #2 is " + s2.getDob());
System.out.println("The retirement years left of student #1 is " + s2.yearsUntilRetirement(65));
}
} import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Student {
private String name;
private String ssn;
private double gpa;
private String dob;
public Student(String newName, String newSsn, double newGpa, String newDob) {
setName(newName);
setSsn(newSsn);
setGpa(newGpa);
setDob(newDob);
}
int yearsUntilRetirement(int retirementAge) {
Calendar currentDate = Calendar.getInstance();
int yearNow = currentDate.get(Calendar.YEAR);
Calendar birthDate = Calendar.getInstance();
try {
DateFormat dobFormat=new SimpleDateFormat("MM/dd/yyyy");
birthDate.setTime(dobFormat.parse(this.getDob()));
} catch (Exception e) {
System.out.println("Invalid Date Format, correct format : MM/dd/yyyy ");
}
return retirementAge- (yearNow - birthDate.get(Calendar.YEAR));
}
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
public String getSsn() {
return ssn;
}
public void setSsn(String newSsn) {
ssn = newSsn;
}
public double getGpa() {
return gpa;
}
public String getDob() {
return dob;
}
public void setDob(String newDob) {
dob = newDob;
}
public void setGpa(double newGpa) {
gpa = newGpa;
}
}
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Student s1 = new Student("Jones", "222-88-1111", 3.7, "03/25/1986");
Student s2 = new Student("Smith", "333-99-4444", 3.2, "06/05/1995");
System.out.println("The name of student #1 is " + s1.getName());
System.out.println("The social security number of student #1 is " + s1.getSsn());
System.out.println("The gpa of student #1 is " + s1.getGpa());
System.out.println("The D.O.B of student #1 is " + s1.getDob());
System.out.println("The retirement years left of student #1 is " + s1.yearsUntilRetirement(65));
System.out.println();
System.out.println("The name of student #2 is " + s2.getName());
System.out.println("The social security number of student #2 is " + s2.getSsn());
System.out.println("The gpa of student #2 is " + s2.getGpa());
System.out.println("The D.O.B of student #2 is " + s2.getDob());
System.out.println("The retirement years left of student #1 is " + s2.yearsUntilRetirement(65));
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.