Exam 2

Release: 11am Friday May 6, 2022

Due: 11am Sunday May 8, 2022

Note that this is released after class Friday, and is due on Sunday. We will not accept late submissions.

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 the exam has concluded. DO NOT post clarification or other similar questions as staff will not be answering these types of questions during this time period. If there are broken links or otherwise strange parts of the writeup, you may post these concerns on piazza. If you have technical trouble creating a screencast (detailed below) feel free to reach out for assistance.

Do not use any online service (such as Discord) 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.

You can review the grading policy for exams in the syllabus. You will complete the programming task below and submit your work to the Exam2 Gradescope assignment.

Starter code is available here:

https://github.com/ucsd-cse11-sp22/cse11-exam2-starter

Submission checklist (see long descriptions below for full details):

The starter code has been marked with the following annotation:

// Task #.#: [Title]
// Your code here

You will only need to add code where you see this annotation.

Make sure to look at your Gradescope submission after submitting to see if all the required files are there.

Task 1-3 will be autograded and will make up most of the Exam 2 score. Task 4 will be manually graded.

Make sure that your submission passes autograder for your code to be properly graded.

If you are having issues with getting the autograder to run successfully, you may find it helpful to consult the Developing with the Gradescope Autograder in Mind guide.

If your submission passes the autograder, you should get full points (31/31 points) and you should see output similar to:

Be aware that the Sanity check does not check for code correctness, but rather that your code compiles.

Your submission will be graded after the deadline. You should test thoroughly yourself to make sure your program works as expected.

Clarifications

Can I use a Java feature/library/method that we haven’t covered in class?

Yes, just make sure it doesn’t break the autograder. The course staff is not responsible for fixing any submissions that fail the autograder during or after the exam.

Can we write more methods than specified?

Yes, you can write additional helper methods.

Can I use previous code that I wrote for a PA in my exam?

Yes.

What does thread mean for Tweets?

Use the definition in PA4

Task 1 – Arrays

You will be writing the majority of your code in ExampleArrays.java, however several tasks will require you to modify other files. For each task, we provided some basic test cases inside the Sanity.java file. You can just do ./run ProvidedArrayTests to run them. We commented out some of the test cases for other methods to make sure you won’t get any errors when trying to run the test case. Please uncomment them when you are finished implementing all the methods for Task 1. Feel free to use this file to test your code by adding more test cases. However, make sure that this file won’t crash the Autograder when you submit it to Gradescope.

Task 1.1

In the ExampleArrays class, you will use the design recipe to write a method called averageWithThreshold that takes a Number[] (an array of Number objects) and Number threshold as two arguments and returns a Number that represents the average of all the elements in the Number[] that are bigger than the threshold. There are essentially two steps in this task, you need to find all the Number that are bigger than the threshold value, then you will need to find the average of all these numbers and return that average.

If the input Number[] contains no numbers that are bigger than the threshold value, return a Number representing the integer 0. If the input Number[] is empty, also return a Number representing the integer 0. We will not test Number objects that represent negative numbers so you may ignore this case. The Number[] tested will not contain numbers that will cause an Integer overflow error.

To assist you in your task, you will modify the Number interface, located inside Numbers.java. In addition to all the methods that were written in PA4 (which you can just copy from your PA4), you will add an additional method to the Number interface and modify the classes that implement Number. That method will be called compare and it will take a Number as an argument and will return an integer.

Your compare method will behave as follows:

Task 1.2

In the file ExampleArrays.java, you will add a new class OUTSIDE the ExampleArrays class called Pair. It will have two fields of type int called a and b. Its constructor will initialize both fields to be the value as specified by the arguments given to the constructor.

In the ExampleArrays class, you will use the design recipe to write a method called findGoodPairs that will take a Pair[] as an argument and return Pair[]. Each element of the returned Pair[] will be a good pair. The requirement for a good pair is that its first number has to be smaller than the second number. For example, (1,3) is a good pair and (1,0) is not. If there is no good pair then you can return an empty array.

Task 1.3

In the ExampleArrays class, you will use the design recipe to write a method called mergePairs that will take 2 Pair[] p1, p2 and return a new Pair[]. Each pair in the returned Pair[] is “merged” together from elements in p1 and p2. Say we have Pair[] p1 and Pair[] p2, we want to merge p1[i] and p2[i], with i being any index number, based on the following rules: The first number of the merged pair is the smaller number between p1[i] and p2[i] The second number of the merged pair is the bigger number between p1[i] and p2[i]

For example, if p1 = [(1,4), (4, 6)] and p2 = [(2,3), (1,5)], then the returned pair array is [(1, 4), (1,6)].

Notes:

There are some tests that have been provided in Sanity.java for your convenience.

class ProvidedArrayTests {
    ExampleArrays ea = new ExampleArrays();

    void testAverageWithThreshold(Tester t) {

        Number[] test1 = { new WholeNumber(1) , new Fraction(4, 2), new WholeNumber(3) };
        Number threshold = new WholeNumber(1);

        t.checkExpect(ea.averageWithThreshold(test1,threshold).toDouble(), 2.5);
	}

    void testFindGoodPairs(Tester t) {
        Pair[] p1 = {new Pair(1,2), new Pair(4,3), new Pair(5,6)};
        Pair[] p2 = {new Pair(2,2), new Pair(4,3), new Pair(7,6)};

        Pair[] expect_p1= {new Pair(1, 2), new Pair(5, 6)};
        Pair[] result_p1 = ea.findGoodPairs(p1);

        Pair[] expect_p2= {};
        Pair[] result_p2 = ea.findGoodPairs(p2);
        
        t.checkExpect(result_p1, expect_p1); // Basic test
        t.checkExpect(result_p2, expect_p2); // Empty test

    }

    void testMergePairs(Tester t) {
        Pair[] p1 = {new Pair(1,4), new Pair(4,6)};
        Pair[] p2 = {new Pair(2,3), new Pair(1,5)};        

        Pair[] expect_p1_p2 = {new Pair(1, 4), new Pair(1, 6)};
        Pair[] result_p1_p2 = ea.mergePairs(p1, p2);

        t.checkExpect(result_p1_p2, expect_p1_p2);
    }

    void testMergePairs2(Tester t) {
        Pair[] p1 = {new Pair(1,4), new Pair(10,15)};
        Pair[] p2 = {new Pair(5,6), new Pair(4,5)};        

        Pair[] expect_p1_p2 = {new Pair(1, 6), new Pair(4, 15)};
        Pair[] result_p1_p2 = ea.mergePairs(p1, p2);

        t.checkExpect(result_p1_p2, expect_p1_p2);
    }    
}

Task 2 – Interfaces

We’ve provided code for Tweet, ReplyTweet, and TextTweet and several related classes that we’ve used for PAs in Tweets.java. Again, we provided some basic test cases inside the Sanity.java file. You can just do ./run ProvidedTweetTests to run them. We commented out this class so that you can run test cases for Task 1 without any compiling error. Please uncomment the ProvidedTweetTest inside Sanity.java before running it. Feel free to use this file to test your code by adding more test cases. However, make sure that this file won’t crash the Autograder when you submit it to Gradescope.

Task 2.1

In Tweets.java, you will add a method called latestTweetOnThread to the Tweet interface and all implementing classes. latestTweetOnThread takes no arguments and returns a Tweet representing the latest date on the thread as determined by the date. If two tweets have the same years then use their months. Return the Tweet with the latest date.

For TextTweet, return the this object itself. For ReplyTweet, follow the description above to return the Tweet with the latest date.

Note:

Task 2.2

In Tweets.java, you will add a method called longestUsernameOnThread to the Tweet interface and all implementing classes. longestUsernameOnThread takes no arguments and returns a User representing the author with the longest username on the thread. If the usernames have the same length, then return the author that appears later on the thread (Note that you don’t need to compare dates for this).

For TextTweet, return the author of the tweet itself. For ReplyTweet, return the author with a longer username, as described above, with this ReplyTweet as the latest tweet in the thread.

There are no specific test requirements for these methods other than the one listed in the video below; we will test them to ensure they are correct and you should test them thoroughly enough to be confident in their correctness.

There are some tests that have been provided in Sanity.java for your convenience.

class ProvidedTweetTests {
    User u1 = new User("greg", "Greg", 12);
    User u2 = new User("greg2", "Greg2", 12);

    Tweet t1 = new TextTweet("We're already halfway through with the quarter", u1, 12, "05-28-2022");
    Tweet t2 = new ReplyTweet("Yeah, can you believe it. It still feel like the beginning of the quarter", u2, 13, "04-28-2021", t1);

    void testLatestTweetOnThread(Tester t) {
        t.checkExpect(t2.latestTweetOnThread(), t1);
    }

    void testLongestUsernameOnThread(Tester t) {
        t.checkExpect(t2.longestUsernameOnThread(), u2);
    }
}

Task 3 – Main and Command-Line Arguments

Your task is to write a program in WordFilter.java that will filter palindrome words from an array of words.

Palindrome words are words that read the same backward as forward. For example: “kayak” is a palindrome word.

Notes: If there are no words provided as command line arguments, print Empty Array . Ignore the case with empty string, we won’t be testing those. If no word is a palindrome in the list, then don’t print anything. Assume that all words in the array are lowercase.

To test your program, you can do the following and the result should be the same as below.

$ javac WordFilter.java
$ java WordFilter cat dog kayak level
kayak level
$ java WordFilter cse11 classes students 
$ java WordFilter
Empty Array

Video Task

You will record a short video of no more than 6 minutes.

Tip: If you find yourself running out of time, you might be explaining your code too much. If the task does not ask you to directly explain your code, you don’t need to explain it.

Include: