cs151 Fall 1999 Exam 1 Solutions Part One: vocabulary (10 points) A logical error is sometimes called a semantic error because it pertains to the meaning of the program. Any kind of expression can be used as an argument, even a method invocation. Just like a local variable, a parameter only exists inside the method where it is declared. A class is a named collection of methods, and a method is a named collection of statements. Part Two: debugging (6 points) For each of the following code fragments, identify any errors and explain in a sentence or two what the problem is. There may be more than one error in a given fragment, or none! public static void printIntTwice (int fizz) { System.out.print (int fizz); System.out.println (int fizz); } When you provide arguments, you don't have to specify their type. If you do, the compiler gets upset, because you are insulting its intelligence. int x = 5; String y = x + "banana"; String z = x + Math.cos (x); In both cases, the addition is legal. The first converts x to a String and then does String concatenation. The second converts x to double and then does floating-point addition. But in the second case, the assignment of a double to a String is illegal. int a; int b; a = 7; b+5 = a; System.out.println (b); The left side of an assignment must be a variable, because it specifies the location where the result will be stored. Part Three: programming (8 points) A friend of yours shows you the following method and explains that if {\tt number} is any two-digit number, the program will output the number backwards. He claims that if {\tt number} is 17, the method will output {\tt 71}. int number = 17; int lastDigit = number%10; int firstDigit = number/10; System.out.println (lastDigit + firstDigit); The problem is that the + sign here will perform integer addition rather than the String concatenation that would be desired. One solution is to add an empty String to the expression, as shown below: Encapsulate your corrected program in a method named printBackwards: public static void printBackwards (int number) { int lastDigit = number%10; int firstDigit = number/10; System.out.println (lastDigit + "" + firstDigit); } The key to this question is to realize that part of what I mean by "encapsulation" is the definition of the interface of the method, which in this case includes one parameter, the number we are printing backwards. Part Four: flow of execution (8 points)} Write the number 1 next to the first statement of this program that gets executed. You don't have to number the rest of the statements. As always, the first statement in main is the first statement that gets executed. When clink invokes zoop, what is the value of the second argument? 4 What is the output of this program? just for any not more It's breakfast ! I was hoping that someone would see that output and decide that the right answer must be, "It's not just for breakfast any more!" but no one did. Part Five: programming (10 points) Please write a method named checkFermat that takes four integers as parameters---a, b, c and n---and that checks to see if Fermat's theorem holds. If n is greater than 2 and it turns out to be true that a^n + b^n = c^n the program should print ``Holy smokes, Fermat was wrong!'' Otherwise the program should print ``No, that doesn't work.'' public static void checkFermat (int a, int b, int c, int n) { String message = "No, that doesn't work."; if (n <= 2) { System.out.println (message); return; } if (raiseToPow (a, n) + raiseToPow (b, n) == raiseToPow (c, n)) { System.out.println ("Holy smokes, Fermat was wrong!"); } else { System.out.println (message); } } One key to this problem was to read the problem specification clearly. It says that we should print the "Holy smokes" message if (n>2) AND (a^n + b^n = c^n), and that it should print the other message OTHERWISE. In this case, OTHERWISE means "if either of the specified conditions are false." Since the same message gets printed twice, I put it into a temporary variable. Part Six: short answer (8 points) 1) Why do methods have parameters? Parameters make methods more general. Instead of solving a specific problem, they allow a method to solve a general class of problems. Many people answered a different question, which is "Why do you have to provide arguments when you invoke a method?" The answer is that you have to provide the information the method needs to do its job, but that answer does not address the question. 1) When people write contracts and laws, they use a stilted form of English that is like a formal language in many ways. What are the characteristics of a formal language that make it useful for legal applications? Formal languages are unambiguous, which is very desirable for contracts and laws. The clearer the law/contract, the less likely that it will lead to disputes. They tend to be more concise than natural languages, although it is questionable whether that is true of legalese. Formal languages are hard to understand, which may be a good thing from the point of view of a lawyer, since it provides job security. Formal languages do not evolve over time the way natural languages do, so there is less chance that the meaning of a law will drift. For this question, many people mentioned the attributes of formal languages but failed to relate them to the application domain in question. In other words, it is not enough to say that formal languages are literal---you have to say why that is a good thing for legal applications.