Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-28-2010, 03:26 PM   PM User | #1
Hyp3r
New to the CF scene

 
Join Date: Jul 2010
Location: England
Posts: 9
Thanks: 4
Thanked 0 Times in 0 Posts
Hyp3r is an unknown quantity at this point
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:

Quote:
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..
Hyp3r is offline   Reply With Quote
Old 11-28-2010, 06:32 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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:
Quote:
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Hyp3r (11-28-2010)
Old 11-28-2010, 08:36 PM   PM User | #3
Hyp3r
New to the CF scene

 
Join Date: Jul 2010
Location: England
Posts: 9
Thanks: 4
Thanked 0 Times in 0 Posts
Hyp3r is an unknown quantity at this point
Smile Result

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

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

Quote:
return this.getAge() > aVoter.getAge();
Is this correct?
Hyp3r is offline   Reply With Quote
Old 11-28-2010, 08:58 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yeah, and I think I misinterpreted myself:
Quote:
In the method isOlderThan() the message getAge() is sent to two different objects
I read as:
Quote:
In the method isOlderThan() the message getAge() is sent two different objects
So chances are that signature is correct.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 11-28-2010, 09:21 PM   PM User | #5
Hyp3r
New to the CF scene

 
Join Date: Jul 2010
Location: England
Posts: 9
Thanks: 4
Thanked 0 Times in 0 Posts
Hyp3r is an unknown quantity at this point
I understood what you meant. Really on the ball stuff.

Thanks again.
Hyp3r is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:07 PM.


Advertisement
Log in to turn off these ads.