CSE 11 Programming Assignment 1

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

Learning Goals

FAQs

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.

Collaboration

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.

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: https://github.com/ucsd-cse11-w22/cse11-pa1-starter

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 FirstExample.java.

  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 - 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:

Q2_example

Problem 3: Converter

Implementation

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.

Testing

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

Implementation

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.

Testing

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.

Submission

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 DesignRecipeExamples.java and transcript.txt

A more detailed list of requirements can be found here

Grading

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 up to 3 times after the deadline to earn additional feedback.

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