Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default Have JUnit tests, need help developing methods

    I could use some assistance, I have Java JUnit code and I need to develop methods for the test:

    Java Code:
    public class DoublePair {
    @Test
    DoublePair p = new DefaultDoublePair(2.3,4.5);
    DoublePair q = new DefaultDoublePair(4.5, 2.3);
    
    assertEquals(2.3, p.first());
    assetrEquals(4.5, p.second()):
    assertEquals("<2.3, 4.5>", p.toString());
    assertTrue(p.equals(p));
    assertFalse(p.equals(q));
    assertFalse(q.equals(p));
    assertFalse(p.equals(null));
    assertTrue(p.equals(q.reverse));
    assertEquals(p.hashCode(), q.reverse().hashCode);
    	}
    I started created the class:

    Java Code:
    public class DefaultDoublePair extends DoublePair {
    
    	public DefaultDoublePair(double d, double e) {
    		
    		
    	}
    	
    	public Object first() {
    		return null;
    		
    	}
    
    }
    But I could really use help in how to develop the equals, hashcode, toString methods. Any help would be most appreciated.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    equals: each object has 2 double values. Simply compare the matching doubles in each of the objects and return true or false if they are the same.

    hashcode: google on how to generate hashcodes.

    tioString: return a String representation of this object. The provided code gives a clue as to what the String should look like.

    Looks like you also need help with first method. Why is the return value Object and why and you returning null?

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    ?
    You mean you've been handed a unit test and are expected to develop a class from that without any actual requirements for the class (bar passing the test presumably)?

    There must be a definition of what the class should do, otherwise how would the test have been written?

  4. #4
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    We were given the tests and we are suppose to make the tests pass, I think p.first() and p.second() methods are suppose to determine what placement the order of the doubles are in. How do you determine position?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    The constructor takes in two arguments (doubles).
    If the first one is assigned to an attribute called first, and the second to an attribute called second, then you should now be able to see what's expected.

  6. #6
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    So would be something like this:

    public class DefaultDoublePair extends DoublePair {

    public DefaultDoublePair(double d, double e) {

    public first(){
    return d;
    }
    }

    }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Do you know about attributes?
    Java Code:
    public class MyClass {
        private int myAttribute;
    ...etc etc.
    }

  8. #8
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Still having a hard time here, can anyone help me:


    public class DefaultDoublePair extends DoublePair {

    public DefaultDoublePair(double d, double e) {

    }

    @SuppressWarnings("null")
    @Override
    public String toString(){
    Object d = null;
    Object e = null;
    return ("<" + d.toString()+ "," + e.toString() + ">");
    }

    }

    my toString fails, I have not even implemented the first() or second(), any help would be appreciated.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Where to start?

    How about you fix the constructor first. There are 2 double values passed to the constructor but you simply ignore them. What do you think you should do with them? A hint was provided above.

    The toString method is on the right track however WTF are the 2 Objects for? Just because they are called d and e it does not connect them in any way to the parameters in your constructor.

    The first and second methods are extremely simple, just return first value or the second value.

  10. #10
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Ok I got this:

    public class DefaultDoublePair extends DoublePair {
    private double d;
    private double e;

    public DefaultDoublePair(double d, double e) {

    }

    public Object first(){
    return d;
    }

    public Object second(){
    return e;
    }

    }

    in the DefaultDoublePair class but now it wants the first() in DoublePair class, why is that?

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    ?????

    Why do you have this fixation with Object? The value that is returned by a method should be the same as the return type in the method signatue.

    You still have not fixed the constructor. If you are not going to follow advice then why ask for it? Why should I bother wasting my time to help you?

    in the DefaultDoublePair class but now it wants the first() in DoublePair class, why is that?
    I don't know. I didn't set the assignment. Your teacher thought up a few test cases and now wants you to write the methods to enable those test cases to execute.

  12. #12
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Ok Ok, sorry, I will continue to work on it, Eclipse keeps throwing Object in there, I guess I am confused on where to put the first() and second() methods, in which class I mean

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    All the methods you have to write should be in the DefaultDoublePair class.

  14. #14
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    when I change the Object to double those two tests get a strike through it

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Don't read minds. Post your code and use code tags.

  16. #16
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Ok so I know I sound like a complete idiot, and I know that I have to do something in:

    public class DefaultDoublePair extends DoublePair {
    private double d;
    private double e;

    public DefaultDoublePair(double d, double e) {

    double first = d;

    public double first(){
    return d;
    }

    }

    this method, but when I try to add the public double first(){} method, Eclipse wants to add a annotation to first(), is first() suppose to be outside of that method?

    Also when I put the methods in DoublePair Eclipse wants me to add the method is in the other Class.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You really are not understanding any of this are you?

    The first method is inside the constructor. You cannot do that. Why have you declared a local variable inside the constructor? This variable will cease to exist as soon as the constructor exits. What about your instance variables? Do plan on using them at any stage?

  18. #18
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    yeah I am having a hard time, sorry, is this any closer:

    public class DefaultDoublePair extends DoublePair {


    public DefaultDoublePair(double d, double e) {

    double first = d;
    double second = e;

    }

    public double first(){
    return first;

    }

    public double second(){
    return second;
    }

    public Object reverse() {
    // TODO Auto-generated method stub
    return null;
    }


    }

    Eclipse says I need to declare first and second local variables but I thought I did that at the top in my instance variables, how to I make other methods read the same variables.

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Nearly. What I said about local variables still holds true. You need to declare first and second as instance variables (do some research if you do not understand). Then all you do inside the constructor is assign the values to the variables.

  20. #20
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    No time right now for research, have a quiz on this in 30 mins and he is going to give us an example like this and we have to construct the class by hand, so any insight on how to construct the methods would be most appreciated. i do appreciate all your help and patience.

Page 1 of 2 12 LastLast

Similar Threads

  1. Junit tests with ant
    By questioner in forum New To Java
    Replies: 3
    Last Post: 04-26-2010, 10:25 AM
  2. int Array Tests
    By Suzanne1187 in forum New To Java
    Replies: 16
    Last Post: 04-16-2009, 01:49 AM
  3. Remote Junit Tests
    By lord.ec in forum Eclipse
    Replies: 0
    Last Post: 12-09-2008, 08:15 AM
  4. Using ant to run JUnit tests
    By racerxadam in forum Advanced Java
    Replies: 0
    Last Post: 10-21-2008, 04:48 PM
  5. Replies: 1
    Last Post: 04-17-2008, 02:30 PM

Tags for this Thread

Posting Permissions

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