Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2010
    Location
    England
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Post Clean up a small bit of code

    In the method isOlderThan() the message getAge() is sent to two different
    objects. One of the objects is referenced by aVoter. What is the other object? I believe it is Voter but I could be wrong :/

    How could I modify the code to make it more readable in this respect,

    Any help on the modified code needed would be greatly appreciated.

    Below is the code in question:

    import ou.*
    /**
    * The class Voter models voters. There are two instance variables, representing
    * the age of the voter at the time of the forthcoming election, and an
    * electoral roll reference number. Instances of the Voter class have to
    * collaborate with objects of other classes that will need to have access to
    * the values of the instance variables.
    * The age and electoral roll reference number should not be changed
    * once they have been set.
    */
    public class Voter
    {
    private int age; //the age of a person
    private String electoralRollReference; //a unique reference number
    /**
    * Constructor for objects of class Voter
    */
    public Voter(String aString)
    {
    this.electoralRollReference = aString;
    this.requestAge();
    }
    /**
    * Sets the receiver's age instance variable to the argument,
    * anInt as long as it is greater than or equal to 18.
    */
    private void setAge(int anInt)
    {
    if (anInt >= 18)
    {
    this.age = anInt;
    }
    }
    /**
    * Returns the receiver's age
    */
    private int getAge()
    {
    return this.age;
    }
    5
    /**
    * Returns the receiver's electoralRollReference
    */
    private String getElectoralRollReference()
    {
    return this.electoralRollReference;
    }
    /**
    * Requests the user to enter an age. Sets the
    * receiver's age to this value
    */
    private void requestAge()
    {
    int answer = Integer.parseInt(OUDialog.request("Enter your age"));
    this.age = answer;
    }
    /**
    * Returns true if the age of the receiver is greater than the
    * age of the argument, and false otherwise
    */
    public boolean isOlderThan(Voter aVoter)
    {
    return getAge() > aVoter.getAge();
    }
    }
    Thanks in advance

    Hyp3r
    Last edited by Hyp3r; 11-28-2010 at 04:28 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    This can't be cleaned up.
    aVoter is a Voter, and it is comparing to Voter. That is, you are comparing THIS voter to another voter, since this class is the Voter class. The only thing you can do to make it obvious (this is not a requirement in Java except when variable masking), is to add 'this.' in front of getAge to indicate that you are looking at this instance and comparing it to another instance.
    You can tell as well that this is the case since the isOlderthan method is not declared as a static method. Static methods are tied to the class that defines it, and not to an object constructed from the class.

    My personal preference is always to use this. when accessing objects. This is only because I do mainly PHP work where $this-> is required, unlike languages like Java an C# where they are optional (except when masked).

    Now, as for a direct link to your question, there is no answer since your signature doesn't match the description of the signature:
    In the method isOlderThan() the message getAge() is sent to two different objects
    Code:
    public boolean isOlderThan(Voter aVoter)
    The signature for isOlderThan accepts only one parameter, and not two.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Hyp3r (11-28-2010)

  • #3
    New to the CF scene
    Join Date
    Jul 2010
    Location
    England
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Smile Result

    Therefore the message getAge is sent to the following 2 different objects;

    public boolean isOlderThan(Voter aVoter)
    private int getAge()
    The line of code could be modified to read:

    return this.getAge() > aVoter.getAge();
    Is this correct?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yeah, and I think I misinterpreted myself:
    In the method isOlderThan() the message getAge() is sent to two different objects
    I read as:
    In the method isOlderThan() the message getAge() is sent two different objects
    So chances are that signature is correct.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New to the CF scene
    Join Date
    Jul 2010
    Location
    England
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I understood what you meant. Really on the ball stuff.

    Thanks again.


  •  

    Posting Permissions

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