Results 1 to 20 of 21
- 03-23-2011, 02:47 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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:
I started created the class: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); }
But I could really use help in how to develop the equals, hashcode, toString methods. Any help would be most appreciated.Java Code:public class DefaultDoublePair extends DoublePair { public DefaultDoublePair(double d, double e) { } public Object first() { return null; } }
- 03-23-2011, 02:57 AM #2
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?
- 03-23-2011, 08:48 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
?
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?
- 03-23-2011, 01:05 PM #4
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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?
- 03-23-2011, 01:40 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 03-23-2011, 03:16 PM #6
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
So would be something like this:
public class DefaultDoublePair extends DoublePair {
public DefaultDoublePair(double d, double e) {
public first(){
return d;
}
}
}
- 03-23-2011, 03:21 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Do you know about attributes?
Java Code:public class MyClass { private int myAttribute; ...etc etc. }
- 03-23-2011, 09:32 PM #8
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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.
- 03-23-2011, 10:01 PM #9
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.
- 03-23-2011, 10:12 PM #10
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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?
- 03-23-2011, 10:19 PM #11
?????
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?
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.in the DefaultDoublePair class but now it wants the first() in DoublePair class, why is that?
- 03-23-2011, 10:22 PM #12
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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
- 03-23-2011, 10:24 PM #13
All the methods you have to write should be in the DefaultDoublePair class.
- 03-23-2011, 10:24 PM #14
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
when I change the Object to double those two tests get a strike through it
- 03-23-2011, 10:26 PM #15
Don't read minds. Post your code and use code tags.
- 03-23-2011, 10:32 PM #16
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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.
- 03-23-2011, 10:38 PM #17
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?
- 03-23-2011, 10:48 PM #18
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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.
- 03-23-2011, 10:53 PM #19
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.
- 03-23-2011, 10:56 PM #20
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
Similar Threads
-
Junit tests with ant
By questioner in forum New To JavaReplies: 3Last Post: 04-26-2010, 10:25 AM -
int Array Tests
By Suzanne1187 in forum New To JavaReplies: 16Last Post: 04-16-2009, 01:49 AM -
Remote Junit Tests
By lord.ec in forum EclipseReplies: 0Last Post: 12-09-2008, 08:15 AM -
Using ant to run JUnit tests
By racerxadam in forum Advanced JavaReplies: 0Last Post: 10-21-2008, 04:48 PM -
Help with testing methods and classes in JUnit, using BlueJ
By Jonasse in forum New To JavaReplies: 1Last Post: 04-17-2008, 02:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks