Wednesday, February 1, 2012

Discussion for Feb. 1

Today we touched on a large variety of topics. Let's see how much we know and can learn about them.

Discussion Topics (respond for participation credit):
We briefly talked about two related classes, InputStreamReader and BufferedReader. Google around a bit, look at the Java API and then describe what these classes do. What can they be used for? Are there other Reader classes?

Another word that came up today was Exception. Can you explain what an exception is? Why do these exist? How are they used? Why are these used?

At the end of the GuessingGame class we called the main method. There is a term for a method that calls itself, what is it? We said that when we do this "The Stack" grows. What does that mean? What is this magic "stack"? Read about it on wikipedia http://en.wikipedia.org/wiki/Call_stack. Then describe what it is doing in your own words.

We talked a little bit about how to efficiently solve the GuessingGame. The term Binary Search was mention. What is a Binary Search? What are its applications?

The keyword "final" was also mentioned today. What does final mean? Write a little example in java and share it via pastebin.com links.

Using methods to logically break up your code into manageable chunks is important. How do you know when you should break up your code? Why is this important?

Visibility and scope have also been tossed around quite a bit. What do these mean? What are we referring to when we say visibility? What are we referring to when we say scope?


32 comments:

  1. "At the end of the GuessingGame class we called the main method. There is a term for a method that calls itself, what is it?"

    A method which calls itself is recursive. A quick example: the shell command rm can be used to delete a directory and it's contents by adding the -r option, for recursion. In this case, rm calls itself on all the files in the directory before calling itself on the directory.

    ReplyDelete
    Replies
    1. "In order to understand recursion, one must first understand recursion."

      https://www.google.com/search?ix=hca&sourceid=chrome&ie=UTF-8&q=recursion

      Delete
  2. "We said that when we do this "The Stack" grows. What does that mean? What is this magic "stack"?"

    The stack is a structure that keeps track of where the "active point" in a program currently is, and where to go when the current routine is finished. It is a first in, last out type of ordering.

    Every time a new routine is called that needs to finish before other routines can continue on, the stack grows because the routine and associated memory cost is put onto the stack. If that same routine then calls other routines, the stack continues to have more routines/memory put on it.

    "The keyword "final" was also mentioned today. What does final mean? Write a little example in java and share it via pastebin.com links."

    I did an example involving the use of constants: http://pastebin.com/pjyMstGq

    ReplyDelete
    Replies
    1. Nice and simple explanation of the stack.

      Also, your demo for using final to make constants is clean. Where else can you put the final keyword? What does it mean in these different contexts?

      How about this code? http://pastebin.com/DFBhK0E5

      Don't compile or run it yet. First, answer: Does it compile? If yes, what does it do?

      Compile and Run (if possible). Explain what is going on here.

      Delete
    2. My pre-compile thoughts:

      I honestly don't know if Java will return an error if you try to change a final, or if that is something that throws an exception at run-time.

      My guess is that it will compile, then at run-time it will throw the exception. The code is printing out digits, then using the for loop to try and reverse the order of the digits.

      Delete
    3. So, I was wrong. It compiles and runs without a hitch. So arrays in Java aren't immutable.

      I think I figured out why this work with some quick Google-fu. There are lots of mentions on the internet that instantiating an object with the final keyword won't make the object immutable, but only the reference to that object immutable. My guess is that arrays are an object in Java, and thus final doesn't stop us from changing them.

      Delete
    4. An array is a special type of Object in Java. The final keyword is often thought to make things immutable. However, this is false. It simply states that once this variable is assigned, you may not reassign it. If you attempt to reassign a variable that is marked final, you will get a compiler error. If you fail to assign a final member variable in your constructor, you will also get a compiler error.

      Delete
    5. Can we make specific elements within an array final?

      Delete
    6. You can not do this with the final keyword. The final keyword only means that the reference can not be changed. For primitive types, this works well for making constants because they are not references.

      You could write a class called "ImmutableArray". How might you do this?

      Delete
    7. Also, can final only be used on variables? If not, what does it mean in these non-variable contexts?

      Delete
    8. You could create a class ImmutableArray by reusing Java's array. You would make the the constructor for the class take the arguments passed to it and place it in a private array.

      You would then have a method that returns a copy of the array, but no methods that can modify the array. This would effectively create an immutable array. I think.

      As for the final keyword: I know it can be used in a method declaration as well. The information on the internet that I found about it says that the final keyword, in this context, means the method can't be overridden by a subclass. I'm entirely unsure about how subclasses work though.

      Delete
  3. We talked a little bit about how to efficiently solve the GuessingGame. The term Binary Search was mention. What is a Binary Search? What are its applications?

    A binary search is a comparison of the middle term of an array with the value being searched for. If the middle term is less than the value you're searching for then the middle term of the upper half is compared. If the middle term is more than the value you're searching for then the middle term of the lower half is compared. The key to this technique is the array must be sorted. Without it being sorted the middle term doesn't mean anything. The advantage of this method is speed for large arrays, although the need to sort could nullify the speed.

    ReplyDelete
    Replies
    1. This is an okay answer. This shows one use of a Binary Search. However, conceptually it is wrong. Do you need an array for a Binary Search? What is the most generalized definition you can give?

      Delete
    2. I'd say a more general definition would be a search that starts in the middle and checks if if what you're searching for is higher or lower. Then it checks the middle of that half and chacks again. Repeat in smaller halves until the value is found.

      Delete
  4. Using methods to logically break up your code into manageable chunks is important. How do you know when you should break up your code? Why is this important?

    You should break your code up when either 1) you're retyping the same thing a lot. You can just cut all of it out, put it into a method, and call that method every time you need it. That helps to organize your code and make it more readable/manageable. You should also make a method 2) when parts of your code are doing specific things that can be generalized. If you break code up into pieces that perform specific things, it's useful for readability. Also, if you need that code in the future for a different project, you can just copy it directly into your new project and it's ready to go.

    ReplyDelete
    Replies
    1. This is pretty good. My general rule of thumb is if a method is more than 15 lines of code you should refactor and break it up into logical components.

      Delete
  5. Scope is when where a variable exist in a stack or "block of code". For example varible called name nstantiated in a method called setName() it can only live in setName() and no where else. So the scope of the varible is where the variable lives. The visibilty is where it can be accessed from other methods or classes.

    ReplyDelete
    Replies
    1. This is a fairly clear answer. However, be careful with your choice of words. You can not instantiate a variable. The correct term is declare.

      You are not 100% correct though. You only give an example of scope within a method. What's another potential scope? Feel free to post code as an example.

      Delete
  6. When you declare a variable "final" in Java, you create a variable that is constant. Once you've declared the value, it cannot be changed or edited at all when the program is run.

    For example, if you wanted to declare pi with an accuracy of two decimal places, and you did not want ANYTHING to make changes to the value this variable, you could write something like:

    private static final double pi = 3.14;

    Are there any other instances that you'd recommend us using "final"? I've only used it with variables in the past...

    ReplyDelete
    Replies
    1. Hmmm... Zach had a similar response (see above for a spoiler). You are correct that it makes primitive types constant. What else does it do?

      Where else can you put the final keyword? What does it mean in these different contexts?

      How about this code? http://pastebin.com/DFBhK0E5

      Don't compile or run it yet. First, answer: Does it compile? If yes, what does it do?

      Compile and Run (if possible). Explain what is going on here.

      Delete
    2. No, i don't think it will compile. You change the value of 'digits' in the for loop so I believe it will give you an error.

      Delete
    3. Well to my surprise, it did compile. I'm a bit unsure as to what is happening. Are values stored in final arrays an exception to the rule?

      Delete
    4. The reasoning is posted above in the comments. Basically, when you make a variable final, it simply means that that variable can not be reassigned.

      final int x = 7;
      x = 8; //This is not allowed

      This effectively makes primitive data types constants. However when you make an Object reference final, you can still use all of their methods. It only means that the variable can not be reassigned later.

      final int[] data = {1,2,3};
      data = {2,3,4}; //This is not allowed

      Does that make sense?

      Delete
    5. Yeah, it does. Thanks for clarifying this for me.

      Delete
  7. For exceptions, when you know your code will have a problem if someone passes it the wrong argument and you don't want it to crash, you throw an exception.
    An example is the throw and catch loops. If you are saving a file, and it can't save it for some reason (like there isn't a path where you are trying to save it), you can put the piece of code in a try/catch and if it can save, the code will insure the program won't crash but instead throws the exception, prints a message, and lets you try again.

    ReplyDelete
    Replies
    1. You state, "when you know your code will have a problem if someone passes it the wrong argument and you don't want it to crash, you throw an exception." This is almost correct. You actually want your program to stop when this happens. And if you throw an exception (and no one catches it) your program will halt and a stack trace will be printed where the exception occurred. Why is this a good thing? It forces whoever passed in an illegal argument to immediately know what they did wrong. If you set some "default" value instead, you are much more likely to run into a weird bug somewhere else in the code. The person then debugging must figure out exactly where this bug occurred. This could prove to be quite difficult if the program simply ignores an invalid input and sets some magic value.

      Your example is also very close to being correct. You say "throw and catch loops" I think you mean "try, catch, finally block". You then talk about saving a file. "If you are saving a file, and it can't save it for some reason (like there isn't a path), you can put the piece of code in a try catch". Again, this is almost correct. You do want to put any sort of IO in a try/catch. But, couldn't you just check to make sure the path exists first? There is a method called "exist()" in File that returns true if the path exists. The answer is yes but, the directory could still not be there at the time you save or the permissions of the file could change, or the file could be deleted before you actually save. How does this happen? Your code never does any of these things. What is going on here?

      Delete
  8. A binary search answer:

    http://en.wikipedia.org/wiki/Binary_search_algorithm

    Can be used to find a specific element within a sorted array.

    Final means that the value never changes and cannot be changed within the program.

    A good way to determine whether you need to break up your code into chunks is if you find yourself repeating code or if you have a method doing more than one or two things. This is important for finding bugs.

    Visibility is about access modifiers. Public variables are visible to everyone. Scope refers to where something sits in the hierarchy. Fields or member variables have a larger scope than local variables.

    ReplyDelete
    Replies
    1. Again, I question your Binary Search answer the same way I did Scott's: "Do you need an array for a Binary Search? What is the most generalized definition you can give?"

      I like your answer for when you want to break your code into methods. How does this help you find bugs?

      You state, "Scope refers to where something sits in the hierarchy." Can you define what you mean by "hierarchy"?

      Delete
  9. Using methods to logically break up your code into manageable chunks is important. How do you know when you should break up your code? Why is this important?

    When I started to program I would just start building code in the main method. This worked for a while until my programs got longer and more complex. I couldn't even read my own code, much less debug it. I have since found the benefit in creating methods. Once I started to break up the program into sections(methods) based on the operations and tasks, debugging became easier and it made my program shorter resulting in more effeciency and more controll of my program.

    If I know I am going to need to do the same operation more than once I make a method.
    If a operation is something I feel is too long, I make a method for it. This way if I need to use it again its there.

    ReplyDelete
  10. Dean Dominguez:

    Stack refers to stacking of frames or dynamic storage. it's a way to shift context from one method to another. LIFO was mentioned earlier in the thread, but it just keeps stacking. First in, lets say main(), then you push in m1, then m2, then lets say m2 finishes it pops out, so now only main() and m1 exist. You return to m1, and lets say you call m3, so m3 gets pushed in etc, etc, etc. So the stack grows each time you push something in.

    Binary search refers to sorting an ordered list, with ordered being the keyword. If a list has a madness to it, ie 0-20, you keep splitting your query in half. Start in the middle, and adjust based on less than or greater. Rinse repeat. This saves significant time if your list is very large.

    We talked about breaking our code up in class when Prof Hayes conceded he had too much going on in his main. He used the word modularity and said we need to break up our code logically. It's been mentioned, but dont repeat code. Have repeating code be its own method. Also, the code needs to be broken up logically, and dont over do it. This is important when checking for bugs.

    ReplyDelete
  11. An exception is a way of handling a user error error caused by input like dividing by 0 or a string where the program is expecting an integer.

    ReplyDelete