CSE 11 Programming Assignment 4
Due Date: Thursday, August 18, 10:00PM Pacific Time
Learning Goals
- practice writing interfaces and classes
Collaboration
Different assignments in this course have different collaboration policies. On this assignment, you cannot share code publicly on Piazza or with any other students in the course. If you need to share code, ask in a private Piazza post or in 1-on-1 hours. Still do ask any public code questions about lecture code, quizzes that are past, or other conceptual questions, just no code from this PA.
You can get the starter code at
https://github.com/ucsd-cse11-su222/cse11-pa4-starter
Submission Checklist
- Tweets.java
- ExamplesNumber.java
Tweets
In a file Tweets.java, write an interface called Tweet with three
methods:
public boolean isStartOfThreadBy(String author);public int totalLikes();public String unrollThread();
Then, write two classes:
TextTweet, implementsTweetand has three fields:contents, aStringlikes, an intauthor, aString
This class should implement the methods as follows:
isStartOfThreadByshould returntruewhen the given author is the same as theauthorof thisTextTweet,falseotherwise.totalLikesshould return the number of likes on thisTextTweetobject-
unrollThreadshould return a string in the following format:<author> <n> likes <contents>where
<author>is replaced by the author’s name,<n>is replaced by the number of likes on theTextTweet, and<content>is replaced by the contents of the Tweet. The string should end in a new line ("\n"character).
ReplyTweet, which should implementTweetand has four fields:contents, aStringlikes, an intauthor, aStringreplyTo, aTweet
This class should implement the methods as follows:
isStartOfThreadByshould returntruewhen the given author is the same as theauthorof thisReplyTweetand thereplyTotweet is also the start of a thread by the same author.totalLikesshould return the total number of likes on thisReplyTweetobject plus the total likes of itsreplyToTweet. For example, a thread of tweets that is 4 replies long should sum the likes on all 4 of those tweets.-
unrollThreadshould return a string in the following format:<replyTo contents> <author> <n> likes <contents>where the bottom three parts are the same format as in
TextTweet, and the first part is the unrolled version of thereplyToTweet. Like forTextTweet, this should end in a new line character.
You can, but don’t have to, use an abstract class to avoid duplicated work as you see fit. Add constructors as appropriate to initialize the fields on objects of these classes (whether or not you use an abstract class).
For each method, write at least two tests for it in a class called Tweets
using the Tester library. A “test” is a use of checkExpect that
checks the results of the method call against an expected value.
Since there are 6 total method implementations, you should have at least 12 tests. Tests are graded manually, your implementation is graded automatically.
Numbers
This code will go in the file ExamplesNumber.java, any tests in a class called
ExamplesNumber that you add to that file.
We saw in our reading that representing fractional numbers like 0.6 with
doubles can be fraught. Some languages and libraries do support exact
fractions, and we can implement classes that act like them in Java. We won’t
be able to use the built-in + and * operators, because these are only
defined for numbers and strings, but we can define methods for the operations
we care about. We can represent numbers with an interface:
interface Number {
int numerator();
int denominator();
Number add(Number other);
Number multiply(Number other);
String toString();
double toDouble();
}
(We could specify more methods, but for the purposes of this assignment, these six will be sufficient.)
Your task is to create two classes that implement the interface above. One
should be called WholeNumber and represent whole integers (including
negative numbers). The other should be called Fraction and represent mixed
numbers.
WholeNumber should have:
- A field
int nand a constructor that takes a singleint - An implementation of all the methods above.
numeratorshould return the value ofndenominatorshould return1addshould return a newNumberthat represents adding this whole number to the one provided as an argument. Note that the argument could be either aFractionor aWholeNumbermultiplyshould return a newNumberthat represents multiplying this whole number to the one provided as an argument. Note that the argument could be either aFractionor aWholeNumbertoStringshould return the value ofnas aString, so ifnis500, it should return"500"toDoubleshould return the value ofnas adouble
Fraction should have:
- A field
int nrepresenting the numerator - A field
int drepresenting the denominator - An implementation of all the methods above:
numeratorshould return the value ofndenominatorshould return the value ofdaddshould return a newNumberthat represents adding this fraction to the one provided as an argument. Note that the argument could be either aFractionor aWholeNumbermultiplyshould return a newNumberthat represents multiplying this fraction by the one provided as an argument. Note that the argument could be either aFractionor aWholeNumbertoStringshould return aStringin the format"n/d"wherenanddare the corresponding fields. So ifnanddwere1and2, this should be"1/2"toDoubleshould return the value ofn/das adouble. So ifnis 1 anddis 2, this should return0.5
A reminder about arithmetic and fractions:
Exploration
At the end of the ExamplesNumber class in a place marked clearly with a
comment that says // Exploration, write code to perform four calculations:
- The result of
0.1 + 0.2 + 0.3using built-indoublearithmetic in Java - The result of
0.1 + (0.2 + 0.3)using built-indoublearithmetic in Java - The result of (1) using your exact fractions, showing the result via
toString() - The result of (2) using your exact fractions, showing the result via
toString()
Submission
Then you will submit all of your files to the pa4 assignment on Gradescope:
- On the Gradescope upload screen, you can keep clicking “Browse Files” to select more than one file for your upload; you can select them one at a time or use your operating system’s multi-select (Shift-Click usually works) to select them all and drag them onto the upload area, or other options that you find that work.
- You can also make a zip archive of all of your files and upload them all at once if you prefer.
We will automatically grade the correctness of the methods and classes you write. Tests and exploration sections will be graded manually. In addition, we may give you feedback on any part of the code, including automatically graded parts, that we want you to respond to after grading.
Extra Challenges (not for credit)
Challenge (not required for credit): Many fractions, like \(2/4\) or
\(27/6\), are not in their simplest form. Make it so that the constructor for
Fraction always creates a fraction object with numerator and denominator in
their most reduced form.
Challenge (not required for credit): Create a ReplyTweet that is a reply
to itself. Do you think this is possible on Twitter?
FAQ
- Why is the autograder producing this error “…” for me?
- As a general reminder, it would help us a lot if your provide your submission link when it’s a Gradescope-related question (just copy the URL from the URL bar when looking at your submission and other students won’t be able to see your code from a Gradescope link).
- In addition, you should have your own tests, and you should write your own tests and try things out to make sure you understand what your code does before submitting it. You can share your tests with us privately on Piazza and we can discuss what’s happening in them as a way to debug as well.
- My code does not run on Gradescope because it’s still WIP. How can I check that the part I finished is correct?
- For the methods you haven’t finished yet, you can put implementations for them that intentionally return the wrong answer, like an empty string or false, to make it so all the tests will run.
- VSCode is red underlining the
import testerstatement. Is this an error?- Before trying to assess the error, try compiling your code and running it. Sometimes VSCode will erroneously underline imports that actually do work. If the code does not compile, make sure that the file that is trying to import the tester is in the same folder as
tester.jar.
- Before trying to assess the error, try compiling your code and running it. Sometimes VSCode will erroneously underline imports that actually do work. If the code does not compile, make sure that the file that is trying to import the tester is in the same folder as
- For
unrollThreadmethods inTweets.java, I cannot pass the tests on Gradescope, but the expected result and my program result look exactly the same.- This is probably because of a missing or extra newline character or space character that is hard to spot with our eyes. The autograder has been updated to give a hint:”(Hint) If your result looks the same as the reference output but still gets an error, there may be extra space characters or newline characters in your result. You may try to print the length of your output String locally to see if it matches your expectation.”
- I wrote test methods with the
Tester, but./runis telling me that no tests ran.Testermethods have to start with “test” at the beginning! likeboolean testAdd(Tester t) { .... }
- Am I required to write tests in the
ExamplesNumberclass?- We won’t grade tests written there, but we encourage you to write them to gain confidence in your code!
- Should the
unrollThreadmethod ofReplyTweetstart with just the contents of thereplyTotweet, or the entire unrolled tweet?- It should contain the entire unrolled tweet, not just the
contentsfield.
- It should contain the entire unrolled tweet, not just the
- In class
ReplyTweet, should be fieldreplyTobe aTextTweetobject instead of aTweet?- replyTo field should be of type Tweet. Since TextTweet and ReplyTweet both implement the Tweet interface, TextTweet and ReplyTweet both have a shared type called Tweet. (Like for example in the class we saw CircleRegion and SquareRegion were both of type Region.). Having replyTo as a Tweet object helps us achieve the “thread” function, where you can reply to your’s own Tweets repeatedly.
Schedule
Calendar
Syllabus
Questions
Material
Assignments
Help Hours