Programming Assignment 3

Due Date: Tuesday, January 31, 10:00PM Pacific Time

Learning Goals


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.

Starter Code

You can download the starter code for this assignment here:

Start Early!

Start working on the assigment early. Even an hour a day can be very helpful to make sure you finish before the deadline.

Submission Checklist

You should submit:

Task 1

Drill 1 (autograded)

Write three classes as described below. They do not need to have any methods or constructors. Save them in the file

Drill 2 (autograded)

In the file, add 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:

Drill 3 (autograded)

In the file, 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 constructor of two arguments that initializes those fields. In it, write the following methods:

(You may find some String methods in the Java documentation useful)

Also in, 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 constructor of three arguments that initializes these fields. In it, write the following methods:

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.

Drill 4 (autograded)

In a file called, write a class Drill4 with the following methods (you may find some Math methods in the Java documentation useful):

In addition, you must write at least three interesting tests for each method above, and include the expected values. 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. In PA4 we will explicitly require it in some cases to make sure everyone gets practice with it.

Task 2

Open-Ended 1 (manually graded)

Consider the following statements about Java programs:

For each statement, write a small Java program that demonstrates whether it is true or false. In the programs you should define some classes that reflect the statements. You can choose whatever names you want for the classes. Put the class definitions in the files,, and Inside the files, you should also include an Open1A/Open1B/Open1C class with some instances of the classes you just create.

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.

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 as your submission; you can upload screenshots as images clearly named with Open1A, Open1B, etc in the title, or copy-paste the text output into a comment in your code.

Open-Ended 2 (manually graded)

Create a class named R that has a field of type String and a field of type R. Give it a constructor that initializes both fields. Put the class in a file called Add an ExamplesR class to this file, and answer the following questions in that file:

  1. 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.
  2. 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 autograded will be graded automatically in Gradescope based on tests we wrote. The parts marked manually graded will be graded by the course staff after the due date.