Release: 5pm Wednesday November 3, 2021
Due: 9am Friday November 5, 2021
Note that this is released after class Wednesday, and is due before class, not due in the evening.
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.
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-f21/Exam2-starter
Submission checklist (see long descriptions below for full details):
Tweets.java
ExamplesArrays.java
Region.java
WordFilter.java
WordFilter-transcript.java
(not actually graded, but useful)video.*
(*
means whatever extension you have; we really prefermp4
, which is what Zoom produces. If you use an extension other than mp4, check that it plays in Gradescope!)
Make sure to look at your Gradescope submission after submitting to see if all the required files are there.
If you have a problem uploading your video to Gradescope, you can upload it here https://docs.google.com/forms/d/e/1FAIpQLSeK0_OQpd0ufiNqmeh0HlQ6Aci3WUFp8bthczJkfIxeeQxLCA/viewform. Do this only if, for some reason, you get an error (usually due to file size) when uploading to Gradescope.
Clarifications
Can we write more methods than specified?
Yes, you can write additional methods (we call them helper methods sometimes) if it helps you complete the task.
What is the WordFilter method?
The original version of the WordFilter task referred to a WordFilter
method.
It should have said class, and is fixed below.
Where should Tweet tests go?
The original starter file Tweets.java
asked you to write tweet tests in
ArrayExamples
; this is wrong, and should have said TweetExamples
. The
comment in the file has been fixed.
Can I use X feature of Java in my code?
Same answer as in exam1
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
In the ExamplesArrays
class, you will use the design recipe to write several methods.
averageWithoutLowest
– takes an array of doubles and returns the average (mean) of them, leaving out the lowest number. So, for example, the average of1
,2
, and3
according to this scheme is2.5
. If there are no elements to average, the method should return0
. If there is a tie for the lowest, leave out only a single one of the tied lowest numbers.sumOfPairs
– write a class calledPair
with the usual field-initializing constructor and twoint
fields calleda
andb
outside theExamplesArrays
class.sumOfPairs
should take an array ofPair
and return an array ofint
, where each element of the returned array is the sum of thea
andb
fields of the correspondingPair
.regionsWithPoint
– takes an array ofRegion
and aPoint
(provided inRegion.java
, you do not need to copy or move that code) and returns a new array of thoseRegion
s in the input that contain the givenPoint
.
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.
Here are some tests to get you started that you can use as you like (and to help make sure we agree on how these methods work).
void testAverageWithoutLowest(Tester t) {
double[] unique = {1.0,2.0,3.0};
t.checkExpect(averageWithoutLowest(unique), 2.5);
};
void testRegionsWithPoint(Tester t) {
Region[] regions = {new CircleRegion(new Point(0, 0), 5), new CircleRegion(new Point(0, 0), 10)};
Region[] result = {new CircleRegion(new Point(0, 0), 10)};
t.checkExpect(regionsWithPoint(regions, new Point(9, 0)), result);
};
void testSumOfPairs(Tester t) {
Pair[] pairs = {new Pair(1, 2), new Pair(3, 4)};
int[] result = {3, 7};
t.checkExpect(sumOfPairs(pairs), result);
};
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
.
Your task is to add two methods to the interface and all implementing classes:
lengthOfLongestTweetInThread
, which takes no arguments and returns the length (number of characters) of theTweet
in the thread with the longestcontent
among Tweets in that thread.timesAuthorPostedInThread
, which takes aUser
argument and returns the number of tweets that user made 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.
Here are a few tests to get you started (and make sure we all agree how this should work):
class ExamplesTweets {
User joe = new User("joepolitz", "Joe Gibbs Politz");
User greg = new User("gregory_miranda", "Greg Miranda");
Tweet t1 = new TextTweet(this.joe, "Java 17 has a cool feature called records", 77);
Tweet t2 = new ReplyTweet(this.greg, "Hmm I wonder if we could use it for CSE11", 12, this.t1);
void testLongestTweetInThread(Tester t) {
t.checkExpect(this.t2.lengthOfLongestTweetInThread(), 41);
}
void testAuthorPostedInThread(Tester t) {
t.checkExpect(this.t1.timesAuthorPostedInThread(joe), 1);
t.checkExpect(this.t1.timesAuthorPostedInThread(greg), 0);
}
}
Task 3 – Main and Command-Line Arguments
Your task is to write a program WordFilter.java
that has a WordFilter class
with a main method with the following behavior:
$ javac WordFilter.java
$ java WordFilter an apple banana panama orangutan pizza
banana
panama
orangutan
$ java WordFilter not-a-substring apple banana panama
0 words contained "not-a-substring"
$ java WordFilter app apple banana app panama orangutan pizza
apple
app
$ java WordFilter
Provide at least one command-line argument
$ java WordFilter something
0 words contained "something"
That is, it treats the first command-line argument specially – it searches for
that word in the other words after it the command-line, and prints those words
out, one on each line. If there are no words found, it prints the message 0
words contained "str"
where str
is the search term. If no command-line
arguments are provided, it prints Provide at least one command-line argument
There are no requirements on writing specific tests for this program, but
include a WordFilter-transcript.txt
to help us understand what you did (we
won’t grade it, but it helps us know what happened if something goes wrong).
Video Task
You will record a short video of no more than 6 minutes. Include:
- Show only your face and a picture ID (your student ID is preferred but any picture ID with your name on it will work) for a few seconds at the beginning. You don’t have to be on camera the whole time, though it’s fine if you are. Just a brief confirmation that it’s you creating the video/doing the work attached to the work itself is what we want. If you do not have a webcam, take a picture of yourself (and your picture ID) with your phone and display that picture at the start of your screen share.
averageWithoutLowest
- Choose an example of calling
averageWithoutLowest
with an array of at length 4 where the lowest element is not the first or last element in the array. - Write down a table corresponding to one loop you wrote in the body of the method that shows, for each variable, what its value is at the start and end of each loop iteration. Write your table in a comment next to the loop, as shown in the example below.
- Pre-write the header row of the table, but you must fill in the contents of the table on the video. As you fill in the “end” value for each variable, indicate which statement(s) in the program caused its value to change.
- If a
return
statement happens in the loop, note that in the row as well.
int z = 0; for(int i = 1; i < 6; i += 2) { z += i; } /* i start i end z start z end 1 3 0 1 3 5 1 4 5 7 4 9 */
- Choose an example of calling
- Choose a test for
lengthOfLongestTweetInThread
where the thread has at least 2ReplyTweet
s and 1TextTweet
, where the object is stored in a field ofExamplesTweets
. Run the program and show the output for that object, and the objects it references, at the terminal. Then, next to the example, describe the stack in a comment, where in each line you indicate:- The class of the method called
- The name of the method called
- The reference of the
this
parameter (use the reference numbers shown in the output of./run
) - The return value of that method call
Put the call that happens first on the last line of your stack trace (so it’s at the bottom, as we’ve drawn in problem sessions and in the notes and videos). Only include calls to the
lengthOfLongestTweetInThread
method.As an example, consider this program, output, and test:
interface I { boolean m(); } class C { public boolean m() { return false; } } class D { I i; D(I i) { this.i = i; } public boolean m() { return !this.i.m(); } } class Examples { C c = new C(); C c2 = new C(); D d1 = new D(this.c); D d2 = new D(this.c2); D d3 = new D(this.d1); boolean ans1 = this.d2.m(); boolean ans2 = this.d3.m(); /* class method reference return value C m :2 false D m :4 true D m :6 false */ } $ ./run Examples Tester Library v.3.0 ----------------------------------- Tests defined in the class: Examples: --------------------------- Examples: --------------- new Examples:1( this.c = new C:2() this.c2 = new C:3() this.d1 = new D:4( this.i = C:2) this.d2 = new D:5( this.i = C:3) this.d3 = new D:6( this.i = D:4) this.ans1 = true this.ans2 = false) --------------- No test methods found.
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.