CIS 1068: Practice Final 1 [I assume you remember well the class Fraction dealt with in one of our homeworks]: 1. Assignments among primitive types and wrapper types Which of the following is a legal assignment? a. int x = 0; boolean y = x; b. char x = '1'; int y = x; c. double x = 1.0; int y = x; d. int x = 1; double y = x; e. Integer y = 3; f. int z = new Integer(7); 2. Multiple Choice For each question below, circle the best answer. i. What does the this keyword refer to? a. The class in which a method is defined. b. The implicit parameter of a method call. c. The method in which a Java statement is written. d. A local variable. ii. What is the main difference between data stored in a file and data stored in memory? a. Data stored in a file will remain after program execution, and after the computer is powered off. b. Data stored in memory will remain after program execution, and after the computer is powered off. c. Data stored in a file is much faster to access. d. There is no major difference between the two. iii. The protected access control limits access to a variable to a. The current class b. The current directory c. The current directory and the subclasses of the current class d. Only the subclasses of the current class 3. Evaluating Expressions and Statements For each expression write down its value. Be sure to list a constant of the appropriate type (e.g., 2 represents an int, 2.0 represents a double, and "2" represents a String). If the expression results in an error, write "ERROR". 2.5 + 2 / 5 - 7 % 3 "12".equals("1" + 9 % 7) (new Point(0,0)) == (new Point(0,0)) "hello".length() == "g'bye".length() new double[10][0] "rosalie".substring(2,3) The class Child extends the class Parent. Parent has the toString method that returns "parent". Child has the toString method that returns "child". If Parent a = new Parent(); Child b = new Child(); Parent c = new Child(); what is the value of a.toString() + b.toString() + c.toString() 4. Reviewing Some Concepts: Short Answer Questions a) What is a method's signature? b) In Java, method names can be overloaded. What does that mean? c) What is the scope of a variable? d) What are stubs and why do we use them? e) What is and why do we use "loop invariants"? f) What is "garbage collection"? 5. Assignments, Casting, and Constructors Let Moo be an interface, and let Foo and Goo be concrete classes that implement the Moo interface. For each statement below, write whether it is a legal or illegal Java statement. a) Foo x = new Foo(); legal? b) Moo y = new Foo(); legal? c) Moo z = x; legal? d) Moo u = new Moo(); legal? e) Foo w = z; legal? f) Goo v = x; legal? 6. Translating to Java simple English expressions. You can assume that x and y refer to integers, s and v to strings, a to an integer array, and c to a character a. x is even b. the length of s is 5 c. s and v are equal d. the third element of a is 5 e. a is empty (i.e. it has no elements) f. c is a digit (i.e. '0', or '1', .. or '9') 7. Classes and Objects: public class Pair { private int first; private int second; public Pair() {} public Pair(int x, int y) { first = x; second = y; } public Pair(Pair a) { first = a.first; second = a.second; } public int getFirst() { return first; } public int getSecond() { return second; } public boolean equals(Pair a) { // returns true iff a and the hidden parameter have the same //content} } The class Pair defines an object that contains a pair of integers. For the questions below, assume that the variables p1 and p2 are of type Pair. a) Write an expression that is true if the pairs referenced by p1 and p2 share the same storage (that is, they point to the same address in memory). b) Implement the equals method 8. File Processing Write a method called translateToFrench that reads in an English text file and translates certain words to French. Assume that the following variable is in scope for your method: String [ ][ ] eng2French = { {"yes", "oui"}, {"hello", "bonjour"}, {"cheese", "fromage"}, {"haha", "honhonhon"} }; Your method should read words from the text file and check to see if they are contained in the eng2French variable. If so, it should translate the words to the French equivalent. Otherwise, it should leave the word alone. Your method should accept two parameters, specifying the names of an input and output file. Your method should read from the input file, and write to the output file. If an input word does not appear in the eng2French variable, the method should simply print the input word to the output file. If it does appear, it should print the translated word. You must handle checked exceptions with the try .. catch .. finally mechanisms. You may wish to define helper methods, but it is not necessary to receive full credit. 9. Fractions Implement the method public static Fraction avg(Fraction[] a) that returns the average value of the fractions in a 10. String Matching Implement a static method distinctParts that returns the number of non- overlapping occurrences of String a in String b, where a and b are the arguments of the method. For example, if b = "babababa" and a = "aba", the result of distincParts(a, b); should be 2. public static int distinctParts(String a, String b) { } If your code contains a loop, specify its loop invariant. 11. Exceptions Implement the function public static int readInt() that prompts the user to enter an integer and returns it to the method caller. Use an exception handler to deal with the possibility that the user may by mistake enter something that is not an integer. Do not return to the caller until the user has actually entered an integer. 12. Path Class Below is a definition of a Point class: public class Point { public double x = 0; public double y = 0; public Point(double x, double y) (this.x = x; this.y = y; } public double distance(Point other) { double xDist = (other.x - x) * (other.x - x); double yDist = (other.y - y) * (other.y - y); return Math.sqrt(xDist + yDist); } } A Path is a sequence of Point objects. The abstract clas below describes the major operations required of a Path: public abstract class Path { protected ArrayList elements; // Constructor that takes an argument indicating the number of //points. public Path(int size) { elements = new ArrayList(size); } // A method to set the ith point along the path to p public abstract void setPoint(int i, Point p); // A method to insert the point p in the ith position public abstract void insertPoint(int i, Point p); // pathLength determines the length of the path by adding up the // distances between consecutive pairs of points on the path public abstract double pathLength(); } Write a class called ArrayPath that implements the Path interface using an array of length size initialed as follows: Each point in the path will have x and y coordinates between 0.0 and 5.0, chosen randomly. Use the Math.random() method, which returns a double value between 0.0 and 1.0. 13. Algorithms a) Here is an integer array {7,3,8,1,2} Show its content after each pass of insertion sort b) Given the integer array {2,3,5,8,12,15,16} list the position examined during the binary search for the value 6. c) Show the steps of the euclidean algorithm to compute the greatest common divisor of 327 and 63