Tuesday, March 8, 2011

Lab 5 and 5a solutions

Sorry, I'm having a very busy week and won't be able to give feedback on lab submissions prior to the labtest.  However, here are solutions to Lab 5 and Lab 5a; hopefully this will help you out.

27 comments:

  1. quick question about the test: will it cover recursion?

    ReplyDelete
  2. Just to bring to your attention once again, there seems to be a problem (as you mentioned) with the name of the tester file being Tester.java, here is what happens in that case --

    http://i55.tinypic.com/1znohnq.jpg

    Could you please help us on that? I am even more interested in this because the error that comes up is pretty weird.

    ReplyDelete
  3. yea... especially considering it seems to be a simple .java file, and clearly the output is not consistent with the file itself. What is generating this output (I got the exact same output)?

    ReplyDelete
  4. In lab 5, under the bankAccount copy constructor, why don't you deep copy the Person object in your solution. If I change the person object of the original bank account, I change it in the copy.

    ReplyDelete
  5. The Tester error is due to having the Type API installed (back from CSE1020), which comes with a class called Tester. Because Java looks for classes there first, it takes priority over your own class.

    If you delete the jar file from the jre/ext folder it should work fine.

    ReplyDelete
  6. @5:44.

    The reason he didn't bother to copy the Person object is because a copy will be made anyway in the other constructor he called (line 14).

    Although I would argue it would be safer to make a copy in the constructor as well instead of relying on the other constructor to do so (and it doesn't hurt).

    ReplyDelete
  7. Or, you could just rename the file to something else(Tester2.java; change the class name in the code as well) and it should work fine.

    ReplyDelete
  8. Jonathan is right.

    I don't have the Type API on my computer and I don't get any error when running a class called "Tester".

    ReplyDelete
  9. Today was a tough test :'( I don't know if I only felt it or was it for every one. Mine didn't even compile

    ReplyDelete
  10. @ 3:01PM

    It was a bit difficult but it was doable and as the prof said that it's going to get difficult from now on so expect everything tough now. Wait till you get a real taste of Link List

    ReplyDelete
  11. Really? I thought it was fair. Of course I would think that.

    ReplyDelete
  12. I thought it test was fairly easy. I've the docs needed to specify the the constructor, throw an exception if the employee created has wage less than minimum wage, however.

    ReplyDelete
  13. Wow, I must've had a brain aneurysm when I wrote that last comment.
    The last post in (better) English:
    I thought the test was fairly easy. According to logic: the API documentation for the Employee class should have specified for the constructor to throw an exception if the employee created has wage less than minimum wage, however.

    ReplyDelete
  14. Couldn't you have just used .setWage(double wage) method?

    ReplyDelete
  15. I found it pretty easy as well.
    I would say it was a bit easier than the labs, which themselves weren't that hard.

    ReplyDelete
  16. @3:01

    The labtest was fair. Either you didn't practice enough (Practice labs were pretty much the same as the labtest) or you made one of those silly mistakes.

    ReplyDelete
  17. Sir, THANK YOU VERY MUCH for these solutions. REALLY REALLY help full to study for the lab test. really appreciate it, please do so for the upcoming labs as well.
    THANK YOU :)

    ReplyDelete
  18. We didn't cover Chapter 9 in class.

    ReplyDelete
  19. @dennis: I don't think so. "this.wage" is only declared, but doesn't have a value assigned to it. If you use the setWage method, the variable will never have a value if you pass a wage less than the MINIMUM_WAGE in the constructor. setWage method doesn't throw error exceptions either... it just changes the value if the wage is greater than MINIMUM_WAGE.

    ReplyDelete
  20. The labtest that we did today was short...or maybe I was missing something.

    We just had to add the areas of Circle or Square in the interface type array.

    ReplyDelete
  21. @Y: That's what i was thinking. You don't want to initialize your constructor with a negative wage right? Oh well, I guess I'll find out if I got it wrong when we get our lab test back

    ReplyDelete
  22. About the wage being less than the minimum wage in the constructor, I think that it doesn't really matter if you covered that case because it's not mentioned in the API. It's only mentioned in the setWage() method so that is the only place you need to handle it.

    We are not responsible for the logic of the programs outside of the API. We have to assume the creator of the API knew what they were doing regardless of how weird it seems (At least in this course). As far as I'm concerned, if it's not in the API then I am not responsible and in fact it could even be a negative thing to go off and assume you know what the creator of the API wanted.

    ReplyDelete
  23. @ Woody 8:06 PM

    yes, i think that was it.

    ReplyDelete
  24. A few questions about your solution to lab 5:

    1) When you wrote the getName() in the Person class, why does the method return name and not this.name?

    2) When you wrote the toString() in the Person class, you first define the String s = this.getClass().getName(). I have experimented with this line of code and I am rather confused by the results. I see that this.getClass() will not produce the expected output, while I see that including .getName() does produce the desired output. I cannot figure out why .getName() produces the desired output. Would it be possible to explain the logic behind this particular choice of syntax?

    ReplyDelete
  25. I think the class was generally confused by your lecture on the singleton class. You gave an extremely simple example of how to implement a singleton class, but you never explained what the purpose of a singleton class is, nor did you give examples of a situation in which a singleton class would be used.

    ReplyDelete
  26. @ 4:25 PM

    "return name" is the same as "return this.name;"

    I think "this.getClass().getName()"
    is the same as "this.getName" and "this.name"

    ReplyDelete