This programming assignment will help you practice writing classes and conditionals. You’ll also get a chance to do some open-ended exploration of Java features.
You will upload several Java files for this assignment, detailed below. The starter code is available here:
https://github.com/ucsd-cse11-s20/pa3
Different assignments in this course have different collaboration policies. On this assignment, you can collaborate with anyone in the course, including sharing code. In your submission, give credit to all students and course staff who helped you with this assignment by noting their name and how you used their ideas or work. Note that using someone’s work without giving credit to them is a violation of academic integrity.
We’ve added a file called CREDITS.txt
for you to write these credits into.
How do we test our methods in PA3? Do we need to use the tester package provide? A: You can test them in the same way as we did in class and previous two PAs. You can use the tester package if you want, but we’re not grading for it. PA4 we will explicitly require it in some cases to make sure everyone gets practice with it. [Piazza @269]
For hasMention() method in class TextTweet, I think I correctly searched for “@username”, but why did some tests on my Gradescope submission fail?
A: There are some interesting cases for this method. For example, to check if we have the username “dummy” in “@dummy1 @dummy2”, hasMention() should return false in this case because username dummy1 and dummy2 is not the same as dummy, while “hello @dummy world” and “CSE 11 is a cool class @dummy” should return true.
For firstMention() method in class TextTweet, do we need to include the “@” (at character) or “ “ (space character) in the resultant substring? A: The idea of this method is to return just the username of the first mention. This means the “@” and “ “ are to be omitted.
Write three classes as described below. They do not need to have any methods or
constructors. Save them in a file called Drill1.java
.
A
with two fields, int f1
and String f2
B
with two fields, A field1
and String field2
C
with three fields, B fieldB
, A fieldA
, int field3
Create a file called Drill2.java
containing the following class definitions
(you can copy/paste them from here):
class C1 {
C2 other;
C1(C2 other) {
this.other = other;
}
}
class C2 {
int x;
C2(int x) {
this.x = x;
}
}
Then add a class definition called Drill2
with the following fields:
first
of type C2
with its x
field equal to 10second
of type C1
. It’s value should be a reference to a
C1
object with its other
field set to any C2
object other than the
one stored in first
(you can create another C2
object for this).third
of type C1
. It’s value should be a reference to a
C1
object with its other
field the same C2 object as the one stored in
the first
field.In a file called Drill3.java
, write a class called TextTweet
that has two
fields: one field called contents
of type String
, and one field called
likes
of type int
. Give it a default constructor of two arguments that
initializes those fields. In it, write the following methods:
String
methods!hasMention
which takes a String
called username
and checks if the
string @
followed by that username apppears in the Tweet contents,
returning true
if it does and false
otherwise.hasLike
which takes no arguments and returns true
if the tweet one or
more likes, false
otherwise.firstMention
which takes no arguments and returns a String
containing the
substring between the first appearance of the @
character in the
contents
and the first space character after that. If there is no space
after the @
, or if there’s no @
, the empty string ""
should be
returned.Also in Drill3.java
, write a class called ReplyTweet
that has three fields:
one called replyTo
of type TextTweet
, one called contents
of type
String
, and one called likes
of type int
. Give it a default constructor
that initializes these fields. In it, write the following
methods:
morePopularReply
which takes no arguments and returns true if this
ReplyTweet has more likes than the TextTweet
it is replying toallLikes
which takes no arguments and returns the sum of the likes on this
ReplyTweet and on the TextTweet it is replying tohasMention
which takes a String
called username
and checks if the
string @
followed by that username apppears in this ReplyTweet
’s contents
or in the TextTweet
that is being replied to.We highly recommend adding a class to this file called Drill3
that has any
tests or examples you used to help verify that your methods worked as you
expected.
In a file called Drill4.java
, write a class Drill4
with the following
methods. For each, write at least three interesting tests, and include the
expected values.
phaseOfWater
which takes an int
and returns "vapor"
if the number is
greater than or equal to 100, "liquid"
if the number is less than 100 and
greater than 0, and "solid"
if the number is less than or equal to 0.maxDifference
which takes three int
s and returns the largest absolute
difference between any two of them. For example, maxDifference
applied to
1
, -1
, and 5
should return 6
because the difference between -1 and
5 is 6 which is greater than 2 or 4 (the differences between -1 and 1, and
between 1 and 5).
Math
class!ringArea
, which takes two double
s representing the radius of an inner
circle and an outer circle, and returns the area of the ring between them as
a double
.
Assume both inputs are positive and that the first number is smaller than the
second. Recall that the area of a circle is πr2. You can use
Math.PI
, a field conveniently defined for us by Java, as a constant for the
value of π.Consider the following statements about Java programs:
For each,
write a small Java program that demonstrates whether it is true or false. Put
them in the files Open1A.java
, Open1B.java
, and Open1C.java
.
To show that a statement is true, write a Java program that matches the
statement, doesn’t produce an error when run, and produces some meaningful
output when ./run
. This means you may want to include an Examples
class
with some instances of the classes you create to demonstrate this.
To show that a statement is false, write a Java program that matches the statement and produces an error when run, demonstrating that Java programs cannot do what the property says.
Include both the program and the output of running the program without error
as your submission; you can upload screenshots as images clearly named with Open1
, Open2
, etc in the title, or copy-paste the text output into a comment in your code.
Open2.java
to ExamplesR.java
to make it easier to
have the class name match the file name.Create a class named R
that has a field of type String
and a field of type
R
. Give it a default constructor that initializes both fields. Put the class
in a file called ExamplesR.java
. Add an ExamplesR
class to this file, and
answer the following questions in that file:
Construct an example R
object. Were you able to? Explain your example if
you were able to, and explain why you think it’s not possible if you weren’t.
On Twitter, it’s possible to reply to a reply to a Tweet (that’s not a typo,
it’s a reply to a reply). This is true of many systems, like email, Facebook
comments, Piazza followups, and so on. With the class structure in Drill3
with ReplyTweet
and TextTweet
(that is, without changing the fields as
described above), could you construct an example of a reply to a reply to a
Tweet? Why or why not?
You will submit all of your files to the pa3
assignment on Gradescope:
The parts marked (auto)
will be graded automatically in Gradescope based on
tests we wrote. The parts marked (manual)
will be graded by the course staff
after the due date. 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.