Programming Assignment 1

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

Learning Goals


Permission denied issue while trying to run lecture code (Mac/Linux)

If you are working on Mac or Linux machines and you want to run the lecture code (or PA1) by using the run script, you might encounter an error that says

-bash: ./run: Permission denied

To solve this issue, run the following command first:

chmod +x run

before running the command ./run FirstExample.


Different assignments in this course have different collaboration policies.

On this assignment, you can collaborate with anyone in the course, including sharing code. Feel free to offer help to other students, converse about the *assignment, and so on.

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.

Start Early!

Research into deadlines and earliness of study work (i.e., when students start working on exercises) has provided some insight into work behavior and how that relates to study performance. Edwards et al. [11] and Parson and Seidel [38] observed that starting early led to better outcomes when compared to starting late, and Leinonen et al. [28] observed that students who started early tended to have better grades than those who started late

In general, the lack of time management skills among students has been found to be related to lower academic performance [34], which may manifest through a variety of ways. For example, poor time management can manifest as procrastination, where tasks are delayed until they can no longer be completed at an expected level (or at all) [14]. Similarly, poor time management skills can lead to poor study strategies including plagiarism [7], and can cause stress and anxiety [37].

On the other hand, good time management is linked with higher academic performance [34]. For example, students who start their work earlier are likely to perform better (e.g., [9, 11, 36, 38]) and distribute their work over multiple days (e.g., [8, 28]); this spacing of work over multiple days can already have a significant effect on learning [10, 17].

Experiences With and Lessons Learned on Deadlines and Submission Behavior

Submission Checklist

Here is a list of items to check for before submitting PA1.

Part 1 - Setting Up

If you get stuck at any point, do any one of the following:

Running Code

Download the starter code for this assignment by downloading this repository using the “Download ZIP” button:

PA1 download

Unzip the directory you downloaded, and open it in Visual Studio Code.

You will be creating a simple program from scratch by following the following steps.

  1. Create a new file named

  2. Create a new class named FirstExample

  3. Create a new field of type int named theNumberFive and assign it the value of 5

After completing these steps, you should have something similar to the following piece of code.

class FirstExample {
  int theNumberFive = 5;

Open the terminal, then run your program using ./run FirstExample (Mac, Lab) or .\run.bat FirstExample (Windows). You should see output similar to the following.

PA1 FirstExample

Let’s explore this program a little. We see that the code is encased inside class FirstExample { ... }. This means that we have created a new class using the class keyword and named it FirstExample. The { and } denote the beginning and end of the FirstExample class. Code we write will be contined inside classes. Classes in Java can contain many different types of information, two of which we will be focusing on in Part 2. They are Fields and Methods. The program we just wrote contains just one field.

Fields are named variables that belong to a class and are written outside of Methods. The class FirstExample contains a single field of type int named theNumberFive with an assigned value of 5. Notice the line int theNumberFive = 5; and how it takes the form of [Type] [Variable Name] = [Assigned Value];. Additionally notice that it belongs to the class FirstExample and not to any method.

By using the run command, we are able to confirm several aspects of our program. We are able to identify our class via the line new FirstExample:1( ... ). The FirstExample class contains 1 field named theNumberFive and its value is 5. We can use the run command to check that our program is working as you expect it to.

Part 2 - Compile Time Errors has two methods and a test for each method. You can run the code with:

Mac and Lab: ./run CompileTimeErrors

Windows: .\run.bat CompileTimeErrors

The code has several compile time errors. Fix the errors so that the code satisfies the following:

  1. method rightTriangleArea()
    • takes a double called base and a double called height as its parameters
    • returns the area of a right triangle as double type calculated using the base and height given
  2. method squareNumber()
    • takes an int called myNumber
    • returns the square of myNumber as int type
  3. field answer1: tests the result of rightTriangleArea() with arguments 3, 5
  4. field answer2: tests the result of squareNumber() with arguments 5

Part 3 - Writing Methods

(Several of these examples are borrowed from How to Design Programs, and its Supplemental Material)

Problem 1: Perimeter

Develop a method named perimeter that when given an integer width and integer height of a rectangle will return its perimeter.

The rubric we will use to grade this method is:

Problem 2: Border Area

Develop a method named borderArea that given 2 integers, width and height, describing a rectangle, and another 2 integers, width and height, describing a rectangle cut out of the center of the other, returns the area of the region between them. For example, the blue area in this shape:


Problem 3: Converter


Develop a method (with a name of your choice!) that takes one int parameter, converts it to another int value, and returns the new int value. For example, your function could convert:

Write a comment for your method, describing what it does and what parameter it takes. If you use any outside sources for the conversion, be sure to cite those in the comment as well.


Call your method on at least 2 different examples. Compare each result against a reference converter (Many search engines have built-in converters, or you can use a calculator) and add a comment above the function call to discuss differences (if any) that you get between your program and the reference.

Problem 4: Combiner


Develop a method (with a name of your choice!) that takes at least two int parameters, combines them to another int value, and returns the new int value. For example, your function could combine:

Write a comment for your method, describing what it does and what parameters it takes. If you use any outside sources, be sure to cite those in the comment as well.


Call your method on at least 2 different examples. Can you call your method with arguments that run, but produce an incorrect output? Call your method with at least one such pair of arguments, and add a comment above it explaining why the output is incorrect. If you cannot find such a pair of arguments, write a comment explaining why you believe no such input exists.


Your submission will include the following:

  1. A code submission of your program with the methods you wrote for part two, at PA1 gradescope assignment. You should select and upload the files, and transcript.txt

A more detailed list of requirements can be found here


There are multiple oppurtunities to get feedback for PA1. By submitting before the deadline, you will receive feedback after it is graded shortly after the dealine. You may also submit to the Late/Resubmit for PA1 one time after the deadline to earn additional feedback.

For more information about the grading policy, visit the course syllabus.