• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

testing array equality

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I'm about to go crazy! I wrote a class that compares ClockTime - I set the class up using arrays. I want to compare two ClockTime objects and test for equality. My instance variable looks like:

private int[] myTime = new int[3];

My default constructor looks like:

public ClockTime()
myTime[0] = 0;
myTime[1] = 0;
myTime[2] = 0;

And my second constructor looks like:

public ClockTime(int hours, int minutes, int seconds)
myTime[0] = hours;
myTime[1] = minutes;
myTime[2] = seconds;

Say a user creates 2 ClockTime objects using either constructor, it doesn't matter which one. After the user creates these two objects, my equals() method needs to compare the two objects for equality - in other words, are they the same time or not.

I don't know if I'm having a brain fart or what, but I CAN'T FIGURE OUT HOW I'M SUPPOSED TO DO THIS. My assignment is due at 8:00 p.m. CST - will someone please help?!?!?!

THANKS!
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about a for loop that checks if each coresponding element is equal?

Layne
 
Amy Lee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sounds great, but two issues (for me at least):

1. How do I specify what I'm checking? When the user creates these two objects, they can name them anything they want. They can name the objects c1 and c2, or clock1 and clock2...etc. So how does my method know what objects to check?

2. My TA mentioned using the equals() method; i.e. c1.equals(c2) I've tried this method and it's not working.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to override equals(Object x) with your own version. Your two arrays are referenced through this and x. You need to check that the types are correct with instanceof and check that this.length == x.length.

Finally, loop over the array elements, checking equality.
[ November 06, 2004: Message edited by: Mike Gershman ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Amy Lee:
That sounds great, but two issues (for me at least):

1. How do I specify what I'm checking? When the user creates these two objects, they can name them anything they want. They can name the objects c1 and c2, or clock1 and clock2...etc. So how does my method know what objects to check?

2. My TA mentioned using the equals() method; i.e. c1.equals(c2) I've tried this method and it's not working.



As Mike says above, you need to create your own equals() method. When you call c1.equals(c2), the objects being compared are given "aliases" so to speak. Inside the equals() method, c1 can be referred to with the keyword "this" (without the quotes) and c2 can be referred to with the name of the parameter (i.e. x in Mike's example above). If this isn't clear, you probably need to review how methods work in general. This "aliasing" works no matter which method you call. It isn't specific to the equals() method described here.

HTH

Layne
 
Paper jam tastes about as you would expect. Try some on this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic