Wednesday, January 25, 2012

Lab Assignment - Integer Calculator

Post any thoughts or questions you have about the Integer Calculator lab assignment here.

Questions and Answers from Pre-lab Assignment:
I have tried to encapsulate all of the questions asked on the pre lab assignment that were relevant to this lab. If your question was not answered, please post it in the comments.


Q: If the user inputs bad data, do you want us to exit or show error messages and restart the program?
A: The answer to this is in the write up specification under the Requirements section: "If the user enters too few or too many arguments or an invalid argument the program prints the usage." Also, because you are receiving input from the command line it does not make sense to restart the program this would result in the same input.

Q: What does isOp() return?
A: Because it is not specified in the write up specification it may return anything you want. However, it is good practice to have methods starting with isX and hasX to return booleans.

Q: Once the program prints usage does it end or restart the program?
A: Your program should exit if invalid input is given to it. It does not make sense to restart the program because you are receiving the input from the command line.

Q: Are we only calculating ints?
A: Yes.

Q: Should we print a statement for each error?
A: This is not specified in the write up. It is generally good practice to do this so the user knows what they did wrong. However, you will not be docked points for not doing this.

Q: What if the user adds spaces in between the arguments of the String?
A: Because we are taking input from the command line you *should* receive 3 Strings if the input is correct. Each of these Strings should not contain any spaces. If they do, that would be invalid input.

Q: What is parsing?
A: To interpret something. In our case, we are parsing Strings to ints using the Integer.parseInt() method. Also, we must parse a String to see if it contains a valid operation

Q: What is op?
A: This is just an abbreviation for operation.

Q: What does it mean to be a static method?
A: A static method is associated with the class rather than the instance of the class. For the purposes of the Integer Calculator assignment just assume all methods need this.

16 comments:

  1. Does anyone know how to save your Java file from the terminal to your own machine? I think it has something to do with the copy command, but I'm not sure...

    ReplyDelete
    Replies
    1. I remotely copied by using scp myname@moons.cs.unm.edu:/nfs/student/.... /mycomputer

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Jennifer, if you are running on a mac or a Linux machine Rogers suggestion will work. However, Windows does not provide a service to do this. You will need to get an FTP service such as CoreFTP to connect and transfer files over the internet.

    Best of luck!

    ReplyDelete
    Replies
    1. Well, poo. I have windows. Oh, well, life happens. Is there any way that I can copy it locally from the CS Unix machines? I have a portable hard disk drive that I can use.

      Delete
    2. Tried that, and it didn't work. The command prompt said "sftp" wasn't a reconizable code. Unless you meant the terminal? Anyways, I'll just download the file manually from the Unix machines. I appreciate the advice, though.

      Delete
    3. An easy and free tool you can use for is WinSCP. It has a GUI and everything and will allow you to get the files.

      Delete
  4. Hello,
    my question is when creating the isInt method to check if the number is an integer I see in the demo code that a scanner was used, since we are using Ineger.parseInt(); do with still need to use a scanner?

    a scanner is just used to read in user input correct?

    ReplyDelete
    Replies
    1. to add to this the other idea i had for this was using Integer.parseInt() within a try catch block

      Delete
    2. Good questions Derek. Although you are not required to use the isInt() method that was provided for you, I highly recommend it. This method merely checks to see if the String you provided contains an int at the start of it before any spaces. This is one of the easier ways to check if a String is an int. However, there are other ways.

      You *can* use a try/catch block to do this. However, it is very bad practice to use a try/catch block for flow control. I realize that this is the #1 suggestion given if you google how to check if a String is an int and I have to say that it is a sloppy way to do it. Basically, try/catch blocks should only be used if there is no other way.

      Again, I recommend using the isInt() method provided to check if it is an integer then use parseInt to actually retrieve the int value.

      Delete
    3. alright thats what i was thinking as well thank you. I will keep working with the isInt()

      Delete
    4. That's what I did. I used the isInt method to make sure that my first and third argument were integers. If they weren't both integers I printed usage and exited right there. My Integer.parseInt() came just after that. I felt like that provided smooth code and less processes before going through everything else.

      Delete
  5. hi again,

    so in the demo code I see you used System.exit[0]; im assuming that the '0' is just a normal exit. my question is what is and abnormal exit like'1'?

    ReplyDelete
    Replies
    1. I should actually be using non 0 exit when an error occurs. Basically, you return a non-zero "code" that is supposed to explain what error occurred.

      Delete
    2. Is that so? Then why have we always used the 0 for exit codes? Is it because we weren't ready to be introduced to the error code concept?

      Delete
    3. The documentation for the method states, "Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination."

      Generally, you should only use 0 if the program didn't have an error.

      Delete