Final Exam

Release: 9am Saturday December 4, 2021

Due: 9am Tuesday, December 7, 2021

This page details a take-home exam that you will complete over the next few days. You can’t communicate with anyone about the content of the assignment until you receive your grade. You can message us privately on Piazza, but the course staff will not give programming advice or answer most questions about the problems. If you have technical trouble creating a screencast (detailed below) feel free to reach out for assistance.

Do not use any online service other than Piazza to ask questions about the assignment. Do not search for, solicit, or use solutions to the problems that you find elsewhere for the exam. These are all violations of academic integrity that students have committed on exams like this in the past.

You can make use of any course notes, online resources about Java and its libraries, Java tools, and so on to complete the exam, including re-using code from class notes or your own past PAs.

You can review the grading policy for exams in the syllabus.

The exam is split into three parts. Per the grading policy for the course, you only need to complete the parts for which you didn’t receive full credit during the quarter. For example, if you scored a 2 on the first exam, a 1 on the second, and a 2 on the third, you should only submit part 2.

Each part has a separate Gradescope assignment to submit to with a partial autograder. Passing the autograder tests does not mean that the submission will get a full score on that part; it just means it passes basic tests and doesn’t have any typos in class or method names that cause compile errors, and works with the format of tests we are using. Check the results when you submit to make sure the correct files are included.

Exam 1 Material

Starter code:

https://github.com/ucsd-cse11-f21/Final-Part-1-starter

Exam 1, Task 1

In the class Exam1Task1Examples in Exam1Task1.java, write a method called rectanglePerimeter that takes two int arguments representing the width and height of a rectangle, and returns an int representing the perimeter of the rectangle. For example, a rectangle with width 4 and height 10 has a perimeter of 28.

Exam 1, Task 2

In the file Exam1Task2.java, create three classes at the top level:

Video Task

Create a video of no more than 3 minutes. In it:

Submission

Upload to Final-Part-1 on Gradescope:

Exam 2 Material

Starter code:

https://github.com/ucsd-cse11-f21/Final-Part-2-starter

Exam 2, Task 1

In the file Exam2Task1.java, in class Exam2Task1Examples, write a method called averageBeforeValue that takes an array of doubles and another double called stopAt. It should return the average (mean) of the numbers in the array that appear at indices before the first index where stopAt appears in the array. If stopAt doesn’t appear in the array, the entire array should be averaged. If the array is empty or stopAt is the first element, the method should return 0.

Exam 2, Task 2

Write a class called WordQuery in WordQuery.java with a main method. It should take any number of command line arguments. The first argument should be either "greater" or "less". The remaining arguments should be in the format accepted by Double.parseDouble. If the first argument is "greater", the program should print all the numberic arguments that are strictly larger than the first numeric argument. If it’s "less", it should print all the numbers that are strictly smaller than the first numeric argument. If only "less" or "greater" is provided, or only a single number, or no numbers match, the program should print nothing. If the first argument isn’t "less" or "greater", or no arguments are provided, the program should print Use "less" or "greater" as the first argument. and nothing else.

Example:

$ javac WordQuery.java
$ java WordQuery greater 1.0 5.6 3.2 0.5
5.6
3.2
$ java WordQuery greater
$ java WordQuery equals 1.0 3.0
Use "less" or "greater" as the first argument.
$ java WordQuery
Use "less" or "greater" as the first argument.

Exam 2, Task 3

In ToText.java, you can find the Region classes we’ve used in lecture.

Add a method toText to the interface and each class that takes no arguments and returns a String. It should have the following behavior:

Video Task

Make a video of no longer than 5 minutes. In it:

Submission

Submit to Final-Part-2 on Gradscope:

Exam 3 Material

Starter code:

https://github.com/ucsd-cse11-f21/Final-Part-3-starter

Task 1

Consider this interface:

interface Query<T> { boolean matches(T t); }

In the class CountExamples in the file Counts.java, write a static generic method counts. It should have a single type variable T and take two arguments: List<Query<T>> called queries and a List<T> called values. It should return a List<Integer> of the same size as the list queries. Each index in the returned list corresponds to the count of values for which matches returned true for the query at that index in queries.

For example:

class LongerStringQuery implements Query<String> {
  int length;
  LongerStringQuery(int length) { this.length = length; } 
  public boolean matches(String s) { return s.length() > this.length; }
}
class EqualStringQuery implements Query<String> {
  String s;
  EqualStringQuery(String s) { this.s = s; } 
  public boolean matches(String other) { return s.equals(other); }
}
class CountExamples {
  /* your definition of counts here */ 

  void testCounts(Tester t) {
    Query<String> q1 = new LongerStringQuery(2);
    Query<String> q2 = new EqualStringQuery("banana");
    Query<String> q3 = new LongerStringQuery(3);
    List<Query<String>> qs = Arrays.asList(q1, q2, q3);
    List<String> docs = Arrays.asList("banana", "pea", "apple", "tomato", "pear");
    // All 5 values are longer than 2
    // 1 value is equal to "banana"
    // 4 values are longer than 3
    t.checkExpect(counts(qs, docs), Arrays.asList(5, 1, 4);
  }
}

Task 2

Write a class called FileSearch in a file called FileSearch.java. It should have a main method that expects several command line arguments. The first should be the name of a file that has, on each line, the name of another file. The rest of the command-line arguments are words to search for in those files.

The program should print a message with the word that appeared in the most files, and the document that matched the most words

files.txt:

doc1.txt
doc2.txt
doc3.txt
doc4.txt

doc1.txt:

This is a document about java generics

doc2.txt:

java is really a kind of coffee

doc3.txt:

collections of generics are common in many languages, such as java

doc4.txt:

this doesn't contain any search queries at all
$ java FileSearch files.txt java generics collections groundhog
Most relevant search term: java
Most relevant document: doc3.txt

java: 3 [doc1.txt, doc2.txt, doc3.txt]
generics: 2 [doc1.txt, doc3.txt]
collections: 1 [doc3.txt]
groundhog: 0 []

doc1.txt: 2 [java, generics]
doc2.txt: 1 [java]
doc3.txt: 3 [java, generics, collections]
doc4.txt: 0 []

In general, FileSearch takes a file and one or more query terms as command-line arguments. It expects that the first file contains names of files to search, and that the other arguments contain search terms to look for in those files.

The program prints out the search term that matched the most documents, and the document that had the most search terms match. Then it prints out the detailed information about matches of the terms and documents:

A search term matches a document if it appears in it somewhere. It still counts as a match if it’s a substring of some other word – “bat” would match a document containing “bath,” for instance. A term can only match each document once; don’t count multiple matches of the same word within one document.

If there is a tie for the most relevant term or most relevant document, report the one that appears earlier in the file (for document names) or earlier on the command line (for query terms).

If no term matches any document, print the message No matches found. and no other information.

You can assume:

Recall that PA7 has some useful provided code that is good for reading files across different platforms.

Video Task

Create a video of no more than 5 minutes:

Submission

Submit to Final-Part-3:

Study

We are conducting a study related to remote exams. The details are embedded below; if you choose not to opt out your exam submission may be used as part of a research project.