From leavens@cs.iastate.edu Tue Sep 7 11:42:31 2004 Date: Tue, 7 Sep 2004 11:42:32 -0500 (CDT) From: Gary T. Leavens To: Ted Skjei Cc: cs362s@cs.iastate.edu Subject: Re: hw2 questions Hi Ted, On Mon, 6 Sep 2004, Ted Skjei wrote: > > What should the error message be for the ClassCastExcetion? Let me answer this more directly. Your code should not have any ClassCastExceptions at runtime. Such exceptions indicate bugs in your code. > Also, I am having > trouble understanding what to do with the hashcode() method. I've read up on it > a little and understand its connection with the equals() method, but I still do > not understand what to do with this method. You have just to return an integer that is such that any other equals object would have the same integer returned. It's fine to have the same hash code for objects that are not equals, but for performance of hash tables, it's also best (but not required) that you return a large range of integers. For example, if we wrote the hashCode method for Integer as : public int hashCode() { return 0; } This would be correct, but it wouldn't lead to good performance of hash tables. Better is the actual implementation, which returns the integer's value. Experiment with calling hashCOde on different String objects, for examples. Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 --------------------------------------------- From leavens@cs.iastate.edu Thu Sep 9 01:55:33 2004 Date: Thu, 9 Sep 2004 01:55:33 -0500 (CDT) From: Gary T. Leavens To: Kun Liang Cc: Ted Skjei , "cs362s@cs.iastate.edu" Subject: Re: another hw2 question Hi Kun, Ted, Kun is right. It's the difference beetween .equals, which is tested by assertEquals for ojects, and ==, which is object identity. The clone method is supposed to give an object that is .equals to the receiver, but a fresh, new, distinct object. On Wed, 8 Sep 2004, Kun Liang wrote: > Ted, > > Although they have the same field, they are different object(instance). > > For example, you and other guy both take CS362, both know java, both live in Ames, that doesn't mean you are the same person:P > > Correct me if I understand it wrong. > > Kun Liang > >> Hi, >> >> I have a question concerning the testing of the Appointment class from the >> AppointmentTest class. In AppointmentTest.testClone() it first tests whether >> all of the fields of the two Appointment objects are equal through several >> AssertEqual statements: >> >> " >> assertEquals(receivers[i].getStart(), //line 155 >> ((Appointment)receivers[i].clone()).getStart()); >> assertEquals(receivers[i].getLength(), >> ((Appointment)receivers[i].clone()).getLength()); >> assertEquals(receivers[i].getLocation(), >> ((Appointment)receivers[i].clone()).getLocation()); >> assertEquals(receivers[i].getWho(), >> ((Appointment)receivers[i].clone()).getWho()); //line 163 >> " >> >> Then, at the end of the function, it tests whether the two classes are equal >> through an AssertFalse statement: >> >> " >> assertFalse(receivers[i] == receivers[i].clone()); //line 164 >> " >> >> I cannot get this assertFalse statement to return true. I am confused by this >> test because you test that all of the fields ARE equal above, then you want to >> make sure the classes themselves ARE NOT equal. If all of the assertEquals >> statements above this one return true (and are suppost to), why would you test >> that the classes ARE NOT equal? If all of the fields are all equal in two >> classes wouldn't the classes therefore be equal? Sorry if I missed something. >> >> Thanks, >> Ted Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 ------------------------------------------------------ From leavens@cs.iastate.edu Fri Sep 10 10:03:01 2004 Date: Fri, 10 Sep 2004 10:03:02 -0500 (CDT) From: Gary T. Leavens To: Matt Hall Cc: cs362s@cs.iastate.edu Subject: Re: hw questions Hi Matt, On Thu, 9 Sep 2004, Matt Hall wrote: > When i see a //@ is that something I am supposed to do, or is that an > assumption. For example it says //@reqiures etc. Is the required part saying i > am supposed to check for nessacary conditions or is it saying the person > calling this must do the correct thing? The second one is right. That is, the "requires" introduces a precondition, which means that you can *assume* that the caller makes the condition true when you are implementing the method. The caller must establish the truth of these preconditions. An "ensures" clause (or postcondition) is something you have to make true. > Also do you have any hints for the > homwork, I am having trouble getting started with the code. I think i have the > basic getHour() type methods correct. Thanks, I'm not sure how to help on this; it would be best to talk to someone in person about it... Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 From leavens@cs.iastate.edu Fri Sep 10 10:16:38 2004 Date: Fri, 10 Sep 2004 10:16:40 -0500 (CDT) From: Gary T. Leavens To: Craig Yarker Cc: Staff for Com S 362 -- Kun Liang , Matthew Ring , Gary Leavens Subject: Re: My Homework 2 Questions Hi Craig, On Thu, 9 Sep 2004 cyarker@iastate.edu wrote: > > 1.) What do the > 'pure' comments mean > in the Appointment > class written in the > method delaration of > such methods as > getLength(), > getStart(), > getEnd()? A pure method is one without side-effects. That is, it does no I/O and assigns to no fields that existed when the method was called. Only pure methods can be used in JML assertions, and it's purpose is mostly to say what methods are "query" methods. > > 2.) Are we > supposed to check > the variables coming > into methods to make > sure > they fit the > parameters or are we > assuming the user > will always input > the > correct stuff? > Example: Setting > an end time before > the start time of an > appointment. You can assume that callers always call methods with the preconditions (requires clauses) true. For example, the setEnd method of appointment rules out making the end time before the start time, due to the precondition: //@ requires end != null && end.compareTo(getStart()) >= 0; > > 3.) What does > "ensures \result != > null && > \result.length() >= > 27;" in the > Appointment.toString() > method mean? What > needs to be greater > than or equal to 27? It means that the returned value has to be a string object, not null, and that the result of callin gthe length method on that string object is at least 27. So it's the length of the string that has be longer. See AppointmentTest.java. > > 4.) How do I > return a cloned > object for the > Appointment.clone() > function? > Do I just return > 'this' or create a > new Appointment with > all the same values > and > return that? Your program can't just return this as the result of clone(). oOu should create a new object with the same values. > > 5.) There are no > comments for the > HashCode at all. I > don't understand > what > it is there for or > what I have to do > with it. ??????? I think this was discussed in a previous q&a or in class. Let me know if you want to see that again. > > 6.) For the > TimeOfDay.toString() > method I can > outputing the time > by using > return > (String.valueOf(hour) > + ':' + > String.valueOf(minutes));. > Will the > valueOf methods work > ok? Yes, they should. > > 7.) How do I > throw a > ClassCastException > for the > TimeOfDay.compareTo(object > o) method? I have > written that if the > object is not of > class TimeOfDay, > "throw > ClassCastException(o)", > and I get an error > that says its > undefined??? use throw new ClassCastException(...). Another way is to just cast an object in a way that fails: (Appintment)(new Object()) will cause such an exception. That may be more convenient. Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 ---------------------------------------------------------------------------- From leavens@cs.iastate.edu Fri Sep 10 10:21:35 2004 Date: Fri, 10 Sep 2004 10:21:37 -0500 (CDT) From: Gary T. Leavens To: William Gerald Hoffa Cc: cs362s@cs.iastate.edu Subject: Re: hw2 Hi Bill, On Thu, 9 Sep 2004, William Gerald Hoffa wrote: > I was just wondering what we should return in the: > public int compareTo(Object o) function of the TimeOfDay class when o is not > the same object as this. It says we return a -1, 0, or 1 if it is the same > object, but nothing if it is not, just that we need to a ClassCastException. > Let me know what I should do for this. The general contract for compareTo is that a result of -1 means that the receiver was less than the argument, 0 means they have the same value, and +1 means that the receiver is greater than the argument. Here's JML's specification for it (which is inherited by TimeOfDay): /*@ public behavior @ requires o != null; @ ensures (* \result is negative if this is "less than" o *); @ ensures (* \result is 0 if this is "equal to" o *); @ ensures (* \result is positive if this is "greater than" o *); @ signals (ClassCastException) @ (* the class of o prohibits it from being compared to this *); @ also @ public behavior @ requires o != null && o instanceof Comparable; @ ensures definedComparison((Comparable)o, this); @ signals(ClassCastException) @ !definedComparison((Comparable)o, this); @*/ /*@ pure @*/ int compareTo(/*@ non_null @*/ Object o); See also the javadocs for java.lang.Comparable. Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 -----------------------------------------