From leavens@cs.iastate.edu Sun Jan 26 17:18:56 2003 Date: Sun, 26 Jan 2003 17:11:28 -0600 (CST) From: Gary T. Leavens To: kwendzel@iastate.edu Cc: cs362s@cs.iastate.edu Subject: Re: Questions about Homework 2 Hi Kevin, On Sun, 26 Jan 2003 kwendzel@iastate.edu wrote: > I am having a Very hard time trying to get homework 2 to work. I have a > couple question on the TimeOfDay class (haven't even tried Appointment yet) > and was wondering if you could help me. I am using Eclipse. > > 1. The compiler tells me that "The declared package [cal] does not match the > expected package." My code is in the "cal" directory and I believe my > classpath was set up correctly by Eclipse. What else could be wrong? Is the cal directory set up as a package in Eclipse? Are you using the right case for the package name and directory name (cal vs. Cal or "CAL")? Is it under another package? For example, if cal were underneath "foo", then the package name expected would be "foo.cal". Send more information if that doesn't help. > 2. I get several errors throughout my code telling me that it "Cannot make a > static reference to the non-static field" and then the name of my variable. > These variables are all private variables that are declared right after the > line "public /*@ pure @*/ class TimeOfDay implements Comparable {". I am > trying to assign them a value in a static method given the variables sent by > the calling function. How to I prevent this error? As the message says, you can't assign to a non-static field of "this" inside a static method. So the way to fix this is either to make the method in question not be static, to make the fields be static, or to create or use an instance of the type instead of trying to refer to fields of "this". In your case, you are trying to refer to the fields of "this" directly from your "am" and "pm" methods, which are static. What you need to do is to call a constructor or get a hold of an instance in those methods (and if necessary refer to that instance's fields). > 3. How do I return a variable of type "TimeOfDay" in a method in > the "TimeOfDay" class? Making a TimeOfDay object won't let the program > compile because I'm trying to make an object of a class that hasn't been > successfully compiled yet. Literally speaking, you can't return a variable from a method, you can only return the value of a variable: an object or a basic value. Your code in the TimeOfDay can certainly call constructors and other methods in the TimeOfDay class, even if it doesn't yet successfully compile; those calls won't add any (more) errors to the compilation. So you can, for example, do things like: public TimeOfDay meth(...) { return new TimeOfDay(...); } All of the declarations we gave you in the TimeOfDay.java file are correct. That is, they use the right syntax for declaring the return types of the methods you are to write. > 4. In a number of the methods, one of the parameters is an Object O. What is > this and how do I operate on this? All reference types in Java are instances of some subclass of the class java.lang.Object, which is usually written Object for short. So saying something is an Object just means it's an object, as opposed to a value. These are often used in situations where you want a argument of any type, such as in collections and for standard methods like equals and compareTo. See the book The Java Programming language for more details. You can look at the javadocs for Object, but essentially you don't have many methods you can call on them. Mostly what you do with an object is either hang on to it's reference (in a variable) or pass it along to some method. You can also test its type, using getClass or instanceof, and if it has some more specific tyep you want, such as TimeOfDay, you can downcast it to that type, as in: if (o instanceof TimeOfDay) { TimeOfDay t = (TimeOfDay)o; ... } else { ... } > Also, what is the difference between " > public int compareTo(Object o)" and "public int compareTo(TimeOfDay t)"? These are static overloads; that is, methods with the same name but different argument types. When you have a call t.compareTo(x) the method that is called depends on the static type of "x". (Static type means the type that the variable or expression is declared with; this is different than the dynamic type, which is the actual type of the object that x refers to.) If x has static type Object, then the method with argument type Object is called. If x has static type TimeOfDay, then the method with argument type TimeOfDay is called. Try an experiment. Put some tracing output in these two methods and do... Object x = new TimeOfDay(...); t.compareTo(x); t.compareTo((TimeOfDay)x); to see what happens. See the book The Java Programming language for more details on static overloading. > 5. One of the methods is "public int hashCode()". What is this thing and > what am I supposed to do with it? It's to return an integer from the object, such that, whenever t1.equals(t2), then t1.hashCode() == t2.hashCode(). It's used in hash tables. See the javadocs for java.lang.Object and the book The Java Programming language for more details. -- 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 Mon Jan 27 20:28:25 2003 Date: Mon, 27 Jan 2003 20:22:20 -0600 (CST) From: Gary T. Leavens To: Carl Cc: Staff for Com S 362 Subject: Re: Hw2 Question (strange error) Hi Carl, On Mon, 27 Jan 2003, Carl wrote: > In testing Hw2 I am getting a strange error. The test class keeps telling > me that it expected something, and it says it got that exact something. The > failure looks like this: > 1) testAppointment(test.AppointmentTest)junit.framework.AssertionFailedError: > expected:<08:00> but was:<08:00> > at test.AppointmentTest.testAppointment(AppointmentTest.java:62) > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) > at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) > at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) > at test.AppointmentTest.main(AppointmentTest.java:25) > > I am getting 4 of these errors, each one deals with a time of day. Any ideas. First, note that the output in the error trace: at test.AppointmentTest.testAppointment(AppointmentTest.java:62) means that this occured in the class test.AppointmentTest (although that should be in the package "cal" instead of "test", did you change the package?) in the method testAppointment, at line 62. I don't see the code on line 62 (it's a blank line in my copy), but if you modified it, that's the line that is detecting the problem. The message probably comes from the line, which is line 57 in my copy, assertEquals(new TimeOfDay(8,00), a.getStart()); as that would produce the expected output. It means that new TimeOfDay(8,00).equals(a.getStart()) is false. Since it's supposed to be true, this probably means that your code for .equals in TimeOfDay is incorrect. I suggest you test that first before proceeding to testing Appointment. -- 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 Mon Jan 27 20:31:22 2003 Date: Mon, 27 Jan 2003 20:31:06 -0600 (CST) From: Gary T. Leavens To: David A Avila Cc: Staff for Com S 362 Subject: Re: hw 2 question David, On Mon, 27 Jan 2003, David A Avila wrote: > For homework 2 it says to hand in all the codethat we wrote. Does this > mean just turn in what we wrote for the two functions that we need to > modify or the entire code. Eh? I thought we asked you to write more than 2 methods for this. I think you mean 2 classes? In any case, please turn in a printout of all of the code for both the TimeOfDay and Appointment classes. -- 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 Mon Jan 27 20:44:25 2003 Date: Mon, 27 Jan 2003 20:43:54 -0600 (CST) From: Gary T. Leavens To: kwendzel@iastate.edu Cc: cs362s@cs.iastate.edu Subject: Re: Questions about Homework 2 Hi Kevin, Sorry this has taken me a while to answer; I've been on a plane most of the time since you sent it... On Mon, 27 Jan 2003 kwendzel@iastate.edu wrote: > Sorry, just realized I sent it to leavens@cs.iastate.edu instead of > cs362s@cs.iastate.edu. So I'm sending this again: Ok. > I have a few more questions about the homework. > > 1. I am still getting the error message "The declared package [cal] does not > match the expected package." The folder "cal" appears as the root folder on > the Package Explorer pane in Eclipse, so I would assume the directory is set > up as a package. No, your assumption is wrong; this is your problem. You need to have an Eclipse project directory, and within that a package named "cal". The project looks like a folder (with a J in it), and a package should look like a small squarish orangish or yellowish icon with 2 crossing black lines (it's supposed to look like a postal parcel wrapped in string I think). > The folder is called cal (all in lowercase). The only other > information that I know to give you about this is that if I look at the > Properties for cal, then the Java Build Path, the source tab is empty, the > Projects tab is empty, and the Libraries tab has one entry, rt.jar. Under the > Order and Export tab, I see two entries. The first one is the folder cal. > The second is the library rt.jar. I haven't bothered putting junit.jar in yet > as I haven't gotten that far. Underneath all of these tabs, the Build output > folder is listed as "cal". What other information can I give you that may > help? Instaed of this you should have a project/folder named something else (say Calendar) with a package under that named cal. The project appears in the build path in the places you mention, not the package, because the paths are naming the directory *above* the package, not the package itself. > 2. In the TimeOfDay class, I understand what hashCode is, but I have no idea > how to implement it. The only way I know to get a hash code is to call the > hashCode function defined by java.lang.Object. But by creating a hashCode > method, I'm overriding the hashCode method defined by java.lang.Object, so in > short, calling hashCode within a hashCode function will cause an infinite > loop. How else can I implement hashCode? I also have the same question about > the clone function in Appointment.java. For TimeOfDay, you just need hashCode() to return an integer such that if t1.equals(t2) then t1.hashCode() == t2.hashCode(). If you have two integers (an hour and a minute), then perform some arithmetic operation to combine them and return that. As long as it's repeatable, it will have this property. What clone does is to make a new object with the same state. Please read the documentation in the javadocs for java.lang.Object for both of these and also see the book "The Java programming language 3rd ed". > 3. When I compile Appointment.java, it gives me errors everytime I make or use > an object of type TimeOfDay. I assume that this is only because TimeOfDay > won't compile successfully, but if not, what else could be the problem? That is probably it. > 4. In Appointment.java, I'm confused about the function setEnd. If you change > the end time of the meeting, then either the start time object or the length > has to change also. Yes, that's right. > I would assume that length has to change and start time > is supposed to stay the same, but am I correct in that reasoning? Right, we didn't specify that exactly in the comments or the JML specification. But that's what I want to happen, and I think the test cases bear that out. -- 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 Tue Jan 28 11:27:59 2003 Date: Tue, 28 Jan 2003 11:27:44 -0600 (CST) From: Gary T. Leavens To: Carl Cc: Staff for Com S 362 Subject: Re: Appointment.setEnd() Hi Carl, On Mon, 27 Jan 2003, Carl wrote: > Should the setEnd method change the length of the appointment? It appears > as though that is what you want from the test classes,(I could be missing > something) but the contract says nothing about changing length. If this is not > how it is supposed to be, where does length get changed in testSetEnd(), when > the assertEquals() numbers are changed? Yes, this is something I didn't specify in setEnd()'s specification. You should change the length, not the start time. The fact that the contract doesn't say it has to change the length doesn't mean it can't change. But I should have been clearer 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 Tue Jan 28 13:33:13 2003 Date: Tue, 28 Jan 2003 13:20:00 -0600 (CST) From: Gary T. Leavens To: Joe Lahart Cc: cs362@cs.iastate.edu, Gary Leavens Subject: Re: hw2 setLength and setEnd fro Appointment Hi Joe, et al., On Tue, 28 Jan 2003, Joe Lahart wrote: > I was wondering why in the Appointment class we have a setLength() and a > setEnd(). It seems that these two methods could contradict each other. Say > you setLength(60) and setStart(8,0) and setEnd(10,0). Should one take > precedence over the other? (Several students have asked me this, so I'm cc'ing the class as a whole. I didn't make this clear at first.) The end time of a meeting is dependent on its length. When you change the end time, the length should also change and vice versa. Thus if you do: appt.setLength(60); appt.setStart(8,0); appt.setEnd(10,0); then appt.getLength() == 120 && app.getEnd().equals(new TimeOfDay(10,0)); The idea is to make it convenient for clients of Appointment. Clients don't have to know which has priority and which is actually represented in the data fields of an appointment. I just now changed the file /home/course/cs362/public/homework/hw2/cal/Appointment.java (also from the web) to have the following javadoc comments and specifications for the methods in question: /** * Sets the length of the meeting. * The start time of the meeting is unchanged, * but the ending time of the meeting is changed * to take the new length into account. * @param minutes The length of the meeting. */ //@ requires minutes > 0; //@ ensures getLength() == minutes; //@ ensures getEnd().equals(getStart().addMinutes(minutes)); //@ ensures getStart().equals(\old(getStart())); public void setLength(int minutes) { } /** * Sets the start time of the meeting to the given time. * The length of the meeting stays the same, * but the end time may change to compensate. * @param start The starting time desired */ //@ requires start != null; //@ ensures getStart().equals(start); //@ ensures getLength() == \old(getLength()); //@ ensures getEnd().equals(start.addMinutes(getLength())); public void setStart(TimeOfDay start) { } /** * Sets the end time of the meeting to the given time. * Note that the end time must be no earlier than the start time. * The length of the meeting is changed to take the new ending time * into account. * @param end The ending time desired */ //@ requires end != null && end.compareTo(getStart()) >= 0; //@ ensures getEnd().equals(end); //@ ensures getStart().equals(\old(getStart())); //@ ensures getLength() == end.minutesFrom(getStart()); public void setEnd(TimeOfDay end) { } Please update your copy of Appointment.java to include these. BTW, I'm back from my trip, so if you have more questions, we can talk anytime this afternoon (and others can as well). (See also the Q & A page available from the course web page, or send email if you can't make it in today.) -- 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 Tue Jan 28 19:42:47 2003 Date: Tue, 28 Jan 2003 19:42:24 -0600 (CST) From: Gary T. Leavens To: Emily Ericson Cc: cs362s@cs.iastate.edu Subject: Re: Hw2 Hi Emily, On Tue, 28 Jan 2003, Emily Ericson wrote: > I was wondering if the .addMinutes() function modifies the object that calls > it. I have been writing my code that way and I realized that that might not > be the case. Right, the addMinutes() method in TimeOfDay is not supposed to modify the receiver. Thus after executing TimeOfDay start = new TimeOfDay(11,0); TimeOfDay end = start.addMinutes(50); we can assert that: start.getHour() == 11 && start.getMinutes() == 0; This is the meaning of the "pure" annotation on the class TimeOfDay, and the reason why addMinutes returns a result instead of having type void. JML has ways to say that the receiver isn't modified by the call, but I wanted to keep the specification notation simple for now, so I left those out. Does that help? -- 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 Tue Jan 28 19:47:30 2003 Date: Tue, 28 Jan 2003 19:47:07 -0600 (CST) From: Gary T. Leavens To: Emily Ericson Cc: cs362s@cs.iastate.edu Subject: Re: Hw2 cloning On Tue, 28 Jan 2003, Emily Ericson wrote: > I'm sorry I just sent you an e-mail, but I forgot to ask about > something. No problem > In > the clone function of Appointment I get an error saying I need to handle a > possible CloneNotSupportedException so I wrote an Exception handler for the > fuction, now however I'm getting an error that says: > > Error This method must return a result of type > java.lang.Object Appointment.java cal/cal line 170 in > Appointment.clone() > > How do I have the exception handler return a result of that type if the error > is thrown? What you can do is turn the exception into an internal error. Something like: public Object clone() { try { ... } catch (CloneNotSupportedException e) { // cannot happen throw new InternalError("Should not happen"); } } See page 92 of The Java Programming Langauge, 3rd ed. for more details on this. -- 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 Tue Jan 28 22:27:34 2003 Date: Tue, 28 Jan 2003 22:25:32 -0600 (CST) From: Gary T. Leavens To: Emily Ericson Cc: cs362s@cs.iastate.edu Subject: Re: Hw2 accessing private fields Hi Emily, On Tue, 28 Jan 2003, Emily Ericson wrote: > I was wondering how you compare TimeOfDay to an Object. The documentation > says that the Object will be an instance of TimeOfDay, so can I call TimeOfDay > Functions on it? Yes, if you first cast it to be a TimeOfDay object. Something like: public boolean equals(Object o) { if (o instanceof TimeOfDay) { TimeOfDay t = (TimeOfDay)o; ... here you can call methods of TimeOfDay on t, e.g. t.getHour() ... } else { ... } } > Or does it have access to private variables? Within the class TimeOfDay the code has full access to other objects of type TimeOfDay, even to the private instance variables of the method's arguments. In the above you can write t.X where X would be replaced by the name of a private variable declared in class TimeOfDay. These rules are the same as in C++. > I am trying to > make these comparisons for: > > public boolean equals(Object o) > > and > > public int compareTo(Object o) To see more, look at the javadocs for java.lang.Object and read the details of the equals method. Also look at the javadocs for the Comparable interface to see details for the compareTo method. -- 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 Tue Jan 28 23:51:46 2003 Date: Tue, 28 Jan 2003 23:51:25 -0600 (CST) From: Gary T. Leavens To: Emily Ericson Cc: Staff for Com S 362 Subject: Re: Hw2 null pointer exceptions Hi Emily, On Tue, 28 Jan 2003, Emily Ericson wrote: > I'm running the JUnit Appointment Test and I have Null Pointer Errors on all > of my functions. Is there something major that I might have forgotton that > would cause this? I'm not sure. Look at the stack backtraces from the JUnit errors. That should tell you want line of the test causes the error in your code. Then look at the receivers of the messages in those places. Are they null? If not, then what messages are sent by the method called? Are any of those receivers null? Are any fields of null variables accessed? It might be that you are returning null from some places or that you have forgotten to initialize some fields of the objects in Appointment's constuctor. I can't guess otherwise. Eclipse also has a debugging mode, which you can use when running JUnit tests. That will allow you to see what is null if you aren't sure, but usually it's faster to track it down by thinking about it rather than trying to use the debugger. > TimeOfDay Compiles and Tests without any errors or failures. That's good. -- 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 Wed Jan 29 08:47:00 2003 Date: Wed, 29 Jan 2003 08:38:52 -0600 (CST) From: Gary T. Leavens To: Emily Ericson Cc: Staff for Com S 362 Subject: Re: Hw2 null pointer exceptions Hi Emily, On Tue, 28 Jan 2003, Emily Ericson wrote: > All of my [null pointer exception] errors seem to refer to the same line: > > public Appointment() { > Start.am(8,0); > ... > End = Start.addMinutes(Length); <---This Line > ... > } > > Can I set End equal to another TimeOfDay variable this way? Yes, the assignment to End can't cause a null pointer exception in itself. It must be the expression Start.addMinutes(Length) that is causing it. This could be because Start is null, or because something in the addMinutes method is causing the null pointer exception. What do you think is happening with the line Start.am(8,0); in your code? This doesn't initialize the Start variable, and the result of the call to TimeOfDay.am(8,0) is thrown away. It's a "feature" of Java that you can make a static method call by using a variable of the type. However, you probably want: Start = TimeOfDay.am(8,0); which initializes the Start variable and doesn't throw away the result. Incidentally, it's considered bad style to use a capital letter for the start of the name of a field. So I'd prefer "start" instead of "Start" for the field name. -- 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 Wed Jan 29 08:47:39 2003 Date: Wed, 29 Jan 2003 08:46:32 -0600 (CST) From: Gary T. Leavens To: Joe Lahart Cc: cs362s@cs.iastate.edu Subject: Re: hw2 throwing exceptions in compareTo Hi Joe, On Wed, 29 Jan 2003, Joe Lahart wrote: > I'm having a problem causing the exception in the compareTo method. Am I > even on the right track here? > > /** Compare this to the given object. > * @see java.lang.Comparable#compareTo(Object) > * @param o the object to compare to > * @return -1, 0, or +1 when o instanceof TimeOfDay > * @throws ClassCastException when !(o instanceof TimeOfDay) > */ > //@ also > //@ ensures o instanceof TimeOfDay; > //@ signals (ClassCastException) !(o instanceof TimeOfDay); > public int compareTo(Object o) { > int result = ...; > try{ > // if (o instanceof TimeOfDay) > // { > if (((TimeOfDay)o).minutesPastMidnight() ...) > { > ... > } ... > } > // } > catch(ClassCastException e){} > return result; > } Well, it's a reasonable idea to just cast the object o in the int compareTo(Object o) method; if it's not a TimeOfDay, that will cause a ClassCastException. But you are catching it in the try {...} catch (ClassCastException e) {} statement which defeats the purpose of that. Take out that try-catch statement, and it should work. Or leave in the commented out if-statment and add an else clause that does throw new ClassCastException(...); to actually cause the exception. Also, I noticed in the code that you are repeating the expression ((TimeOfDay)o) several times, and that the code in the int compareTo(Object o) method repeats a lot of the code in the int compareTo(TimeOfDay t) method. -- 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 --------------------------------------------------------