CSC 1051 Algorithms and Data Structures I Final Examination December 18, 2015 Name: Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the spaces provided. Please be legible. If you make a mistake or need more space, use backs of pages - clearly indicate where the answer can be found. Good luck and best wishes for the holidays!. /.\./..\. /.oxo.\./.*..x.\. /.oo..oo.\./.oxo..***.\. /.*.oo..*.oo.\.
1. ( / 10) What gets printed? Please show output as it will appear, or indicate NO OUTPUT, or show some of the output followed by INFINITE LOOP. int a = 5; Output: do System.out.println(a); a--; while (a < 5) Output: for (int a = 4; a <= 6; a++) System.out.println(a * 2); String[] notes = "do", "re", "mi"; for (String s1: notes) for (String s2: notes) System.out.println(s1 + s2); Output: int a = 5; while (a < 5) System.out.println(a); a--; Output:
2. ( / 10) a) Suppose you implemented a graphical object representing an emoji using an instance variable happiness to control the smile/frown, using an if/else statement, as follows: if (happiness) else page.drawarc (x+15, y+30, 20, 10, 180, 180); page.drawarc (x+15, y+30, 20, 10, 0, 180); Rewrite the above code using the conditional operator. (i.e., in a single line of code, without using the if/else), b) Suppose you would like to generate a random inspirational message. You start by creating an array containing a list of possible messages: String[] mycliche = "Life is beautiful", "Keep calm and carry on", "Live, laugh, love", "Today is the first day of the rest of your life" ; // add your own, if you feel like it! Write a code fragment that uses a a randomly generated index into this array to print one of the above messages. Your choice whether to use an object from the Random class or the random() method from the Math class. Be sure your code works fine if you add more messages to the array (for example, if you add a fifth message in the array above).
3. ( / 10) Consider the following program: import java.util.scanner; import java.io.*; public class FileOutputFinalF15 public static void main(string[] args) throws IOException Scanner infile; PrintWriter outfile; infile = new Scanner (new File("data-in.txt")); outfile = new PrintWriter("data-out.txt"); while (infile.hasnext()) String token = infile.next(); outfile.print(token.length() + " "); outfile.close(); data-out.txt data-in.txt Down came the rain and washed the spider out. a) Suppose the file data-in.txt is used as the input file. Show the contents of the file data-out.txt after execution of the program. b) List two examples of situations that could cause IOExceptions in the above code. c) Suppose you want to catch and handle the IOExceptions using the following catch clause: catch (IOException e) System.out.println("Problem with file IO. Running interactively."); infile = new Scanner(System.in); outfile = new PrintWriter(System.out); (i.e., keep running, but issue a warning and do interactive I/O instead) Show how to incorporate this in the above program: 1) Show the statements in the above code that need to be included in the try block 2) mark the position where you would insert the catch code.
4. ( / 10) Consider the following program: import java.util.scanner; import java.io.*; public class FinalF15 public static void main(string[] args) throws IOException Scanner infile = new Scanner (new File("data.txt")); String[][] table = new String[3][4]; String[] labelthis = new String[4]; String[] labelthat = new String[3]; String line = infile.nextline(); Scanner scan = new Scanner(line); scan.usedelimiter(","); scan.next(); // skips something for (int i = 0; i < table[0].length; i++) labelthis[i] = scan.next(); for (int i = 0; i < table.length; i++) line = infile.nextline(); scan = new Scanner(line); scan.usedelimiter(","); labelthat[i] = scan.next(); for (int j = 0; j < table[i].length; j++) table[i][j] = scan.next(); data.txt ignore this,this is good,this is bad,this is ok,this is excellent My Documents,apple,orange,asian pear,avocado My Downloads,eenie,meenie,miney,mo My Pictures,Betty Ann,Socrates,Vijay,Olivia (continued on next page)
(continued from previous page) // printing array contents: System.out.println("\n\nlabelThis"); for (String x: labelthis) System.out.print(x + " "); System.out.println("\n\nlabelThat"); for (String x: labelthat) System.out.print(x + "&&&"); System.out.println("\n\ntable"); for (int i = 0; i < table.length; i++) for (int j = 0; j < table[i].length; j++) System.out.print(table[i][j] + "$"); System.out.println("*****"); System.out.println("Bye Bye"); Show the output produced using the input file data.txt as given on the previous page. Output
5. ( / 10) Write a Java method palindrome() with one parameter, a String that returns a boolean true if the String is a palindrome (i.e., it reads the same forward and backward) and false if it is not a palindrome. For example, palindrome("anna") should return true, whereas palindrome("mamamia") should return false. Note that the method should not print anything.
6. ( / 10). Trace through the following code and show what gets printed. int[] a = 100, 200, 300, 400; int[] b = 1000, 2000, 3000, 4000; int[] c = b; for (int i=0; i<a.length; i++) b[i] = a[i]; Output: a[1] = 44; b[2] = 55; c[3] = 66; for (int x: a) System.out.print(x + " "); System.out.println(); for (int x: b) System.out.print(x + " "); System.out.println(); for (int x: c) System.out.print(x + " "); System.out.println(); (c) Suppose an int array ratings contains values in the range 0-3. Write a code fragment that creates an array count that contains the frequency of occurrences of 0 s, 1 s, 2 s and 3 s in the ratings array, and then prints this information. Example: if the array ratings has these contents: ratings 0 1 2 3 4 5 6 2 3 2 1 0 2 2 Your code should create the array count with the following contents: count 0 1 2 3 1 1 4 1 and the output would be: Count for 0: 1 Count for 1: 1 Count for 2: 4 Count for 3: 1 Write your code fragment below. Assume the array ratings is already initialized to some values. Your code should work for any size array.
7. ( / 10) We studied the problem of repeatedly obtaining input and performing a calculation, for example, computing the circumference of a circle given its radius, using the following algorithm: Variables: radius, circ Rewrite this algorithm, modifying it so that it uses a while structure to repeat the processing of each input in two different ways. a) Keep computing circumferences and ask each time whether to keep going. Variables: Algorithm: Algorithm: input radius circ = 2 * radius* PI print circ b) Keep computing circumferences until user inputs -1 for the radius (sentinel value) Variables: Algorithm: c) Compute the circumference of 5 circles (exact count). Variables: Algorithm:
8. ( / 10) Fill in code for an Employee class, following guidelines in comments. public class Employee // instance variables String name; String position; double hourly; // hourly wages int yearhired // constructor: Construct object with w, x, y, and z as // name, position, hourly pay rate, and // year hired, respectively. // tostring(): Returns a String corresponding to object. // getyearhired(): Accessor for yearhired // wages(): Given the number of hours worked (a value of type double, // returns the wages of this employee, calculated based on // hourly rate, for up to 40 hours and 1.5 overtime of // hourly rate for hours over 40.
9. ( / 10) Using the Employee class from the previous question: a) Draw a UML diagram for the Employee class. b) Write client code that uses the Employee class: Instantiate an Employee object with name Lucia Rodriguez, with position software engineer, hourly rate $42.50, hired in 2013. Assign it to a variable named coderboss c) Write client code that uses the Employee class: Suppose you have three Employee objects e1, e2 and e3 and that e2 worked 44.5 hours last week, whereas e1 and e3 both worked 40 hours. Write some client code to calculate and print: 1) the wages for each employee and 2) the average of their wages. (Note: it is NOT necessary to format as currency). d) Write client code that uses the Employee class: Suppose you have two Employee objects e1, e2. Write some client code that uses the getyearhired() and tostring() methods of the Employee class to print the information for the Employee who has been with the company the longest (i.e., hired earlier).
10. ( / 10) Suppose you look up a class in the Java API and find something that looks like the following (NOTE: this is a made-up class.): java.exam Class Mystery java.lang.object java.exam.mystery public class Mystery extends Object This is a made-up class. It does not matter what it actually does, I am just trying to see if you know how to use it. Constructor Summary Mystery(double x) Creates a new Mystery object. Method Summary boolean decider(int x, String y) Mystery method 1. void updator(double x) Mystery method 2. a) What import statement do you include in your program in order to use this class? b) Write some code to declare variables for two objects of this class, named thing1 and thing2 (use any values of the appropriate type in the constructor). c) Consider the following client code: Valid Java? Yes No thing1.updator(5.3); thing1.updator(double x); Mystery.updator(7.2); thing2.decider(int x, String y); System.out.println("answer= " + thing2.decider(5, "d")); System.out.println("answer= " + thing2.updator(7.2)); if (thing2.updator(0) == 2.0) System.out.println("ok"); if (thing2.decider(5, "d")) System.out.println("ok");