Programming Assignment 1

Due Date: Wednesday, January 17, 8:00AM Pacific Time

Learning Goals

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.

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

Part 1 - Setting Up

You are all required to install Java and VSCode on your systems. Using VSCode as your source code editor is not strictly required, but highly recommended for this course. You can follow the following instructions to install Java and VSCode on your own personal computer, or if you working in the CSE lab:

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

Writing your First Java Program

Download the starter code for this assignment by downloading this repository using the “Download ZIP” button: https://github.com/ucsd-cse11-w24/cse11-pa1-starter

PA1 download

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

Next, you would create and compile your own java program through 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;
}

Let’s explore this program a little:

Note: Running this program through the Run Button in VSCode will fail, and complain about the missing main method. You can ignore this for now. We will show you how to run the program in Part 3 of this assignment. All parts are supposed to be done sequentially. Skipping part 2 of this assignment, and moving directly to Part 3 will cause your compilation of this program to fail!

Optional Reading - What are Methods

This is just optional reading if you need to know more about Methods. You may skip this and directly move to Part 2 of the assignment!

Method is a block of code that performs a specific task and can be executed when it is called. Methods are used to organize code into reusable blocks, making it easier to manage and maintain. An example of a method is as follows:

int addNumbers(int a, int b) {
    // Method body
    int result = a + b;
    
    // Returning the result
    return result;
}

Let’s break down the components of this method:

You can call the method using the following:

int sum = addNumbers(5, 10);

This will call the method addNumbers with the values 5 and 10. The method will add the numbers and return the result of addition, that would be saved to a field sum of type int.

Part 2 - Compile Time Errors

IMPORTANT NOTE: COMPLETE THIS BEFORE PROCEEDING TO FURTHER PARTS!

First, we will look at a Java program that has compile time errors and try to resolve them.

Compile-time errors occur during program compilation and prevent the creation of an executable file. Common issues include syntax errors, type errors, and declaration problems. These errors are detected by the compiler before program execution, and you must address them by fixing the code to ensure syntactic correctness.

In the directory, you should navigate to CompileTimeErrors.java. CompileTimeErrors.java has two methods and a test for each method. You can run the code with:

./run CompileTimeErrors

Note for Windows Users: If the above doesn’t work for you, you can try running the code with the following command:

.\run.bat CompileTimeErrors

On an initial run, you would get the following output:

Compile Errors

As shown, 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

On a successful run, you should get the following output:

Compile Success

You can safely ignore the warnings here.

Part 3 - Running your first Java Program

After fixing the Compile time errors in part 2, you should be ready to run your code in Part 1.

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

Congratulations! You are now able to run your first Java Program on your computer!

Part 4 - Writing Methods

Note for Windows Users: If the above doesn’t work for you, you can try running the code with the following command:

.\run.bat DesignRecipeExamples

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

Problem 1: Volume

Develop a method named volume that when given three integers: length, width and height of a cuboid will return its volume.

The rubric we will use to grade this method is:

Problem 2: Quadratic Equation

Develop a method named quadraticEquation that when given integers: a, b, c and x, describing a quadratic equation of the form:

y = ax² + bx + c , calculates and returns the value of y.

The rubric we will use to grade this method is:

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 Checklist

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

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 CompileTimeErrors or ./run FirstExample.

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, CompileTimeErrors.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 to earn additional feedback (after the Late/Resubmit deadline).

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