Basically is this my test file for some reason is just giving me problems and so I'm currently in the moment trying to correct the problem. I'm also trying to beat a deadline tonight that would give me 20% EC on this assignment.

What I need is for someone to review the code and see by just looking at the code if it's doing what I think it is doing and that is doing what the assignment asks.

The assignment is this: Add a data field motherMaidenName to Person. Write and accessor and a modifier method for this data field. Modify class toString and class equals to include the data field. Assume two Person objects are equal if they have the same ID number and mother's maiden name.

Here's the code:

/** Person is a class that represents a human being.
  *  @author Koffman and Wolfgang
 
  * */
/** @author Noliving*/
public class Person {
   // Data Fields
   /** The given name */
   private String givenName;

   /** The family name */
   private String familyName;

   /** The ID number */
   private String IDNumber;

   /** The birth year */
   private int birthYear = 1900;
   
  /**  Mother's maiden name
  @author Noliving*/
   private String mothersMaidenName;

   // Constants
   /** The age at which a person can vote */
   private static final int VOTE_AGE = 18;

   /** The age at which a person is considered a senior citizen */
   private static final int SENIOR_AGE = 65;

   // Constructors
   /** Construct a person with given values
       @param first The given name
       @param family The family name
       @param ID The ID number
       @param birth The birth year
    */
   public Person(String first, String family, String ID, int birth) {
     givenName = first;
     familyName = family;
     IDNumber = ID;
     birthYear = birth;
     mothersMaidenName = "";//This is adding the maiden name value that was requested from the assignment
   }

   /** Construct a person with only an IDNumber specified.
       @param ID The ID number
    */
   public Person(String ID) {
     IDNumber = ID;
   }

   // Modifier Methods
   /** Sets the givenName field.
       @param given The given name
    */
   public void setGivenName(String given) {
     givenName = given;
   }

   /** Sets the familyName field.
       @param family The family name
    */
   public void setFamilyName(String family) {
     familyName = family;
   }

   /** Sets the birthYear field.
       @param birthYear The year of birth
    */
   public void setBirthYear(int birthYear) {
     this.birthYear = birthYear;
   }

   // Accessor Methods
   /** Gets the person's given name.
       @return the given name as a String
    */
   public String getGivenName() {
     return givenName;
   }

   /** Gets the person's family name.
       @return the family name as a String
    */
   public String getFamilyName() {
     return familyName;
   }

   /** Gets the person's ID number.
       @return the ID number as a String
    */
   public String getIDNumber() {
     return IDNumber;
   }

   /** Gets the person's year of birth.
       @return the year of birth as an int value
    */
   public int getBirthYear() {
     return birthYear;
   }

   // Other Methods
   /**  Calculates a person's age at this year's birthday.
        @param year The current year
        @return the year minus the birth year
    */
	
   public int age(int year) {
     return year - birthYear;
   }

   /** Determines whether a person can vote.
       @param year The current year
       @return true if the person's age is greater than or
               equal to the voting age
    */
   public boolean canVote(int year) {
     int theAge = age(year);
     return theAge >= VOTE_AGE;
   }

   /** Determines whether a person is a senior citizen.
       @param year the current year
       @return true if person's age is greater than or
               equal to the age at which a person is
               considered to be a senior citizen
    */
   public boolean isSenior(int year) {
     return age(year) >= SENIOR_AGE;
   }

   /** Retrieves the information in a Person object.
       @return the object state as a string
    */
   public String toString() {
     return "Given name: " + givenName + "\n"
         + "Family name: " + familyName + "\n"
         + "ID number: " + IDNumber + "\n"
         + "Year of birth: " + birthYear + "\n"
		  + "Mothers name: " + mothersMaidenName + "\n";
   }

   /** Compares two Person objects for equality.
       @param per The second Person object
       @return true if the Person objects have same
               ID number; false if they don't
       @return also if they have the same mother's maiden name
    */
   public boolean equals(Person per) {
     if (per == null)
       return false;
     else
       return IDNumber.equals(per.IDNumber)&& mothersMaidenName.equals(per.mothersMaidenName);
   
      }
      
      //This method is public string, it is to get the intials of the of the person and leave a period behind the first and last initial.
   
   
   public String getInitials(){
   String firstname = getGivenName();
   String lastname = getFamilyName();
   String firstnameinitial = firstname.substring(0,1);
   String lastnameinitial = lastname.substring(0,1);
   String initial = firstnameinitial + "." + lastnameinitial + ".";
   return initial;//returns the initial(s) as a string
   }  
	 /** This is programming problem #3 from assignment, this is comparing the ID number of objects too see if they have the same ID if they do it
       return a zero
            @return whether or not they have the Same ID number*/
   public int compareTo(Person person){
   return IDNumber.compareTo(person.IDNumber);
   }
 
   private void SwitchNames(){
   String holdoldgivenname = "";
   holdoldgivenname = givenName;
   givenName = familyName;
   familyName = holdoldgivenname;
   }
  }

Thanks for the help in advance.

Oh I think I forgot to do a public void setmotherMaidenName(String maidenname) { motherMaidenName = maidenname;

Or something like that under the modifier methods.

Hi there,
First things first.

String IDNumber

Unless you are expecting non-numeric characters to populate this string you should try and declare the ID as an integer.

e.g.

private int IDNumber = 0;

But please note, I come from more of a C programmers background so that is just my prerogative.

Secondly, change the name of toString as that is already a method of the String class.

Thirdly,

public void setMotherMaidenName(String maidenName) {
        mothersMaidenName = maidenName;
    }

    public String getMotherMaidenName()
    {
        return mothersMaidenName;
    }

An example usage could be as follows:

import Person.Person;
 import java.io.*;

public class testPerson {
    public static void main(String args[])
    {
        String maidenName = "Latrene";
        Person someMother = new Person("Mary","O'Connor","666", 1951);
        System.out.println(someMother.PertoString());
        someMother.setMotherMaidenName(maidenName);
        System.out.println(someMother.getMotherMaidenName());
  
    }
    
      
}

This would produce the following output:

Given name: Mary
Family name: O'Connor
ID number: 666
Year of birth: 1951
Mothers name:

Latrene


Hope this helps you out a little bit.

Cheers Bernie.

bmfitzgerald82@hotmail.com

Oh sweet sweet Jane.

The toString() method MUST stay with the same name. In java, the toString() is not a method of String object but of the Object class. And you must override it in your class.
ex: when you call:

Person p = new Person(....);
System.out.println(p);

Then the system.out, automatically calls the toString() method of the class Person. If you haven't implementd it in Person then it calls the toString() of the super class, which in this case is the Object class. So if you are to implement a method that returns a String with the values of the class then you should call it toString().
The rest are correct.
You should add set, get methods for mothersMaidenName variable.
I think it will also be necessary to add it as an argument at your constructor.

public Person(String first, String family, String ID, int birth, String mothersMN ) {
  givenName = first;
  familyName = family; 
  IDNumber = ID; 
  birthYear = birth;
  //mothersMaidenName = "";
  mothersMaidenName = mothersMN ;
//This is adding the maiden name value that was requested from the assignment
}

About the last I am not sure if this is what your assignment says that you must do.
And as personal opinion, (you can do whatever you want) I would keep ID as String. I use int only if I intend to do some addition, subtractions or multiplication with the variable. Since you will not do these things with ID I would leave it String IF it was my code

Thanks guys for the help.

Here's where everyone is been having the problem javaaddict he won't allow us to add mothersMaidenName to the constructor. He suggested making it null if, which is what believe I did, when it came to the constructor.

Actually, if you want it to be null write:
mothersMaidenName = null; //mothersMaidenName doesn't have a value.

mothersMaidenName = ""; it has an empty String as value.

Don't forget the setMothersMaidenName method in order to be able to change its value.

Actually, if you want it to be null write:
mothersMaidenName = null; //mothersMaidenName doesn't have a value.
Don't forget the setMothersMaidenName method in order to be able to change its value.

Question though, mothersMaidenName is a string so wouldn't using mothersMaidenName = ""; be just as good as mothersMaidenName = null;

Alright this is what I currently have with some of the stuff added:

/** Person is a class that represents a human being.
  *  @author Koffman and Wolfgang
  @author or name of person that created this is Jeffrey Weik, I did this with the tutor named I believe seeven bathi the one at 7pm on thursdays.
  * */
/** @author Noliving*/
public class Person {
   // Data Fields
   /** The given name */
   private String givenName;

   /** The family name */
   private String familyName;

   /** The ID number */
   private String IDNumber;

   /** The birth year */
   private int birthYear = 1900;
   
  /**  Mother's maiden name
  @author Noliving*/
   private String mothersMaidenName;

   // Constants
   /** The age at which a person can vote */
   private static final int VOTE_AGE = 18;

   /** The age at which a person is considered a senior citizen */
   private static final int SENIOR_AGE = 65;

   // Constructors
   /** Construct a person with given values
       @param first The given name
       @param family The family name
       @param ID The ID number
       @param birth The birth year
    */
   public Person(String first, String family, String ID, int birth) {
     givenName = first;
     familyName = family;
     IDNumber = ID;
     birthYear = birth;
     mothersMaidenName = null;//Doesn't have a value.
   }

   /** Construct a person with only an IDNumber specified.
       @param ID The ID number
    */
   public Person(String ID) {
     IDNumber = ID;
   }

   // Modifier Methods
   /** Sets the givenName field.
       @param given The given name
    */
   public void setmothersMaidenName(String maidenname) {
     mothersMaidenName = maidenname;
   }
   
   public void setGivenName(String given) {
     givenName = given;
   }

   /** Sets the familyName field.
       @param family The family name
    */
   public void setFamilyName(String family) {
     familyName = family;
   }

   /** Sets the birthYear field.
       @param birthYear The year of birth
    */
   public void setBirthYear(int birthYear) {
     this.birthYear = birthYear;
   }

   // Accessor Methods
   /** Gets the person's given name.
       @return the given name as a String
    */
   //Gets the person's maiden name
   //@return the maiden name string
   public String getmothersMaidenName() {
     return mothersMaidenName;
   }
   
   public String getGivenName() {
     return givenName;
   }

   /** Gets the person's family name.
       @return the family name as a String
    */
   public String getFamilyName() {
     return familyName;
   }

   /** Gets the person's ID number.
       @return the ID number as a String
    */
   public String getIDNumber() {
     return IDNumber;
   }

   /** Gets the person's year of birth.
       @return the year of birth as an int value
    */
   public int getBirthYear() {
     return birthYear;
   }

   // Other Methods
   /**  Calculates a person's age at this year's birthday.
        @param year The current year
        @return the year minus the birth year
    */
	
   public int age(int year) {
     return year - birthYear;
   }

   /** Determines whether a person can vote.
       @param year The current year
       @return true if the person's age is greater than or
               equal to the voting age
    */
   public boolean canVote(int year) {
     int theAge = age(year);
     return theAge >= VOTE_AGE;
   }

   /** Determines whether a person is a senior citizen.
       @param year the current year
       @return true if person's age is greater than or
               equal to the age at which a person is
               considered to be a senior citizen
    */
   public boolean isSenior(int year) {
     return age(year) >= SENIOR_AGE;
   }

   /** Retrieves the information in a Person object.
       @return the object state as a string
    */
   public String toString() {
     return "Given name: " + givenName + "\n"
         + "Family name: " + familyName + "\n"
         + "ID number: " + IDNumber + "\n"
         + "Year of birth: " + birthYear + "\n"
		  + "Mothers name: " + mothersMaidenName + "\n";//the assignment in programming problem #2 wanted us to modify the class toString by adding the mothersMaidenName to the toString
   }

   /** Compares two Person objects for equality.
       @param per The second Person object
       @return true if the Person objects have same
               ID number; false if they don't
       @return also if they have the same mother's maiden name
    */
   public boolean equals(Person per) {
     if (per == null)
       return false;
     else
       return IDNumber.equals(per.IDNumber)&& mothersMaidenName.equals(per.mothersMaidenName);
   
      }
      
      //This method is public string, it is to get the intials of the of the person and leave a period behind the first and last initial

   
   
   public String getInitials(){
   String firstname = getGivenName();
   String lastname = getFamilyName();
   String firstnameinitial = firstname.substring(0,1);
   String lastnameinitial = lastname.substring(0,1);
   String initial = firstnameinitial + "." + lastnameinitial + ".";
   return initial;//returns the initial(s) as a string
   }  
	 /** This is programming problem #3 from assignment, this is comparing the ID number of objects too see if they have the same ID if they do it
       return a zero
            @return whether or not they have the Same ID number*/
   public int compareTo(Person person){
   return IDNumber.compareTo(person.IDNumber);
   }
   /**@author Noliving
   */
   private void SwitchNames(){
   String holdoldgivenname = "";
   holdoldgivenname = givenName;
   givenName = familyName;
   familyName = holdoldgivenname;
   }
  }

You can use whatever you want but they are different.

String s="";
You create a new String with the value to be an empty String: "". Meaning that s exists and you can use it:
you can call s.toString().If you print this: System.out.println(">"+s+"<"); it will print: ><
s.length will return 0

BUT

String s=null;
You don't create an object, s does not exist, and you cannot call any methods.
If you try to call any of the previous methods:
s.toString(), s.length, or any other method you will get a NullPointerException at runtime.

Plus:
String s=null;
(s==null): returns true
( s.equals("") ): will not execute, will throw an Exception

String s="";
(s==null): returns false;
( s.equals("") ): will execute and return true

This article has been dead for over six months. Start a new discussion instead.