Results 1 to 5 of 5
  1. #1
    BlitzAcez is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default Please help me this is simple bit of code

    I am using BlueJ for this

    My Person Class THIS WORKS FINE, THE BIT THAT I DONT KNOW WHAT TO CHANGE TO IS COLOURED IN ORANGE AND THE BIT IN BLUE NEEDS A BODY OF CODE

    ANY HELP MUCH APPRECIATED

    /**
    * This is the class Person.
    *
    * @author (************************)
    * @version (1.0)
    */
    public class Person
    /**
    * Instance variables of person, the attributes of the class.
    */
    {
    protected Date dateOfBirth;
    //private int age; //Removed
    protected String name;
    protected String gender;
    protected static int counter;
    /**
    * Constructors for objects of class Person
    */
    public Person(String nme, char sex, Date dob) //int howOld removed
    {
    name = nme;
    setGender(sex);
    dateOfBirth = new Date(dob);
    counter++;
    }

    public Person(Person other)
    {
    name = other.name;
    dateOfBirth = other.dateOfBirth;
    gender = other.gender;
    }

    /**
    * Methods of the class person
    */

    public static int count()
    {
    return counter;
    }

    public void setGender(char sex)
    {
    if (sex == 'm' || sex == 'M')
    gender = "male";
    else if (sex == 'f' || sex == 'F')
    gender = "female";
    else gender = "unknown";
    }
    /**
    *Removed
    *
    //public void incAge()
    //{
    //age += 1;
    //}

    public void setAge(int howOld)
    {
    age = howOld;
    }
    *
    */
    public void setName (String nme)
    {
    name = nme;
    }

    public String toString()
    {
    return "Name: " + name + " Age: "
    + dateOfBirth + " Gender : " + gender; //Changed to dateOfBirth from age
    }

    public String getName()
    {
    return name;
    }

    public String getGender()
    {
    return gender;
    }
    /**
    *Removed
    public int getAge()
    {
    return age;
    }
    */

    public Date getdateOfBirth()
    {
    return dateOfBirth;
    }

    public boolean sameGender (Person other)
    {
    return gender.equals(other.gender);
    }

    public boolean isOlderThan (Person other)
    {
    return dateOfBirth.earlierThan (other.dateOfBirth);
    }

    public boolean isFemale()

    {
    if ( this.gender.equals("Female"))
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    public boolean equals (Person other)
    {
    return name.equals(other.name) && dateOfBirth == other.dateOfBirth && //Changed to dateOfBirth from age
    gender.equals(other.gender);
    }

    /**
    * Accessors to compare age to another person
    */
    public boolean isOlderTthan (Person other)
    {
    return dateOfBirth.earlierThan (other.dateOfBirth); //Changed to dateOfBirth from age < other.age
    }

    /**
    * Transformers that will copy a persons details
    */

    public void copy (Person other)
    {
    name = other.name;
    dateOfBirth = other.dateOfBirth; //Changed to dateOfBirth
    gender = other.gender;
    }
    }


    .
    .
    .
    .
    .
    .

    THE EMPLOYEE CLASS

    /**
    * This is the class Employee.
    * @author (***************************)
    * @version (1.0)
    */
    public class Employee extends Person
    /**
    * Instance variables of Employee, the attributes of the class.
    */
    {
    private Date dateStarted;
    private float salary;
    private int id;

    /**
    * Constructors for objects of class Employee
    */
    public Employee(String nme, char sex, Date dob, int number, Date start)
    {
    super(nme,sex,dob);
    id = number;
    dateStarted = new Date(start);
    salary = 0.0f;
    }

    /**
    * Methods of the class Employee
    */

    public boolean equals (Employee other)
    {
    return super.equals(other) && id == other.id && salary == other.salary &&
    dateStarted.equals(other.dateStarted);
    }

    public void setSalary(float salary);
    {
    ??? //NEED NEW CODE HERE ALSO FOR THIS PART TO WORK NOT SURE WHAT TO DO
    }


    public Employee (Employee other)
    {
    this(other.salary,other.id,other.dateStarted);
    }


    THE BIT IN ORANGE NEEDS TO BE CHANGED TO WORK ( Cannot find symbol - Constructor Employeee (float, int, Date)

    public void copy (Employee other)
    {
    dateStarted.copy(other.dateStarted); // QUESTION 4 Not changed Dont know what to do
    salary = other.salary;
    id = other.id;
    }

    public String toString()
    {
    return "Employee Number: "+ id + "salary " + salary + "Started: " + dateStarted.toString();
    }

    public Date getDateStarted()
    {
    return dateStarted;
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    for this code here:
    Java Code:
    public Employee (Employee other)
    {
      this(other.salary,other.id,other.dateStarted);
    }
    The this(...) call here is nothing more than a call to your other Employee constructor, and it has to match that constructor exactly. Thus if the first parameter in the other constructor is a String, the first parameter in your this(...) call must be a String too.

    So take a very close look at your Employee constructor, especially all the parameters used and the variable types for the parameters, and use these same paramters when calling this(...).

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    for the second problem, understand that a "setter" method, a method that has setXXX in it, is used to update a class's instance fields with the parameter passed. For example if I have a class like so:

    Java Code:
    class Fubar()
    {
      int myValue;
    }
    and I wanted to change myValue, I'd do something like this:

    Java Code:
    public void setMyValue(int myValue)
    {
      this.myValue = myValue;
    }
    Notice that I use "this" on the left side to tell the compiler that I want the class's instance myValue on the left of the assignment, the one held by the class (by "this") and the parameter myValue on the right of the assignment.

  4. #4
    BlitzAcez is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default

    this has helpped alot thank you very much.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. A simple Java source code viewer
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:34 PM
  2. Peculiarty in code of simple program...
    By Kreuz14 in forum New To Java
    Replies: 4
    Last Post: 01-23-2008, 03:27 AM
  3. simple problem - code wont compile
    By dirtycash in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 05:49 PM
  4. simple code
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:49 PM
  5. problem with a simple java code
    By boy22 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 02:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •