Popcorn Hack 1

// Original Code

// Does stuff with numbers
public int doSomething(int[] nums) {
    int result = 0;
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] > 0 && nums[i] % 2 == 0) {
            result += nums[i];
        }
    }
    return result;
}

/**

  • Calculates the sum of all positive even integers in the given array.
  • This method iterates through {@code nums} and adds each element to the total
  • if and only if it is positive and even.
  • </p> *
  • Preconditions:

  • {@code nums} must not be {@code null}.
  • </ul> *
  • Postconditions:

  • Returns the sum of all positive even integers in {@code nums}.
  • Returns {@code 0} if there are no positive even integers.
  • </ul> *
  • @param nums the array of integers to analyze (must not be {@code null})
  • @return the sum of positive even integers in {@code nums} *
  • Example:

  • 
      
  • int[] numbers = {1, 2, 3, 4, -6};
  • int result = doSomething(numbers); // result = 6
  • </pre> */

/**

  • Manages and calculates student grades based on weighted categories.
  • The {@code GradeBook} class allows instructors to store grades by category
  • (e.g., Homework, Tests, Projects), assign weights to each category,
  • include extra credit, and generate final reports.
  • </p> *
  • Key Features:

  • Add assignments and scores under various categories
  • Set custom weightings for categories
  • Apply extra credit to final grade
  • Generate readable grade reports
  • </ul> *
  • Example Usage:

  • 
      
  • GradeBook myGrades = new GradeBook();
  • myGrades.setCategoryWeight(“Homework”, 0.30);
  • myGrades.setCategoryWeight(“Tests”, 0.70);
  • myGrades.addAssignment(“Homework”, “HW1”, 95.0);
  • double finalGrade = myGrades.calculateFinalGrade();
  • </pre> *
  • @author Your Name
  • @version 1.0
  • @since 2025-10-13 */

/**

  • Sets the weight for a grading category. *
  • @param category the category name
  • @param weight the category weight (e.g., 0.25 for 25%)
  • @precondition {@code weight} >= 0 and total weights ≤ 1.0
  • @postcondition Updates the category weight mapping */

/**

  • Calculates the final weighted grade, including extra credit. *
  • @return the computed final grade as a percentage */

/**

  • Generates a text report of categories, scores, and final grade. *
  • @return a formatted String summarizing grades and weights */

/**

  • Demonstrates a simple addition operation using a static helper method. *
  • This program defines two integers, adds them together using the {@code add} method,
  • and prints the result to the console. *
  • Example Output:
  • ans is 15 *
  • @author Your Name
  • @version 1.0
  • @since 2025-10-13 */

/**

  • Adds two integers and returns their sum. *
  • @param a the first integer
  • @param b the second integer
  • @return the sum of {@code a} and {@code b} *
  • Example:

  • 
      
  • int result = add(5, 10); // result = 15
  • </pre> */

/**

  • Attempts to enroll a student in a specific course for a given semester.
  • This method checks several conditions to ensure the enrollment is valid:
    • The student and course must exist.
    • The course must have available seats.
    • The student must not have schedule conflicts or missing prerequisites.
    • The total credit hours must not exceed 18.
  • </p> *
  • Preconditions:

  • {@code studentId} and {@code courseCode} are valid and non-null.
  • Both the student and course exist in the system.
  • </ul> *
  • Postconditions:

  • The student is enrolled in the course if all checks pass.
  • Enrollment transaction is recorded successfully.
  • Returns {@code true} if enrollment succeeded, otherwise {@code false}.
  • </ul> *
  • @param studentId the unique ID of the student
  • @param courseCode the code identifying the course
  • @param semester the semester number or identifier
  • @return {@code true} if enrollment is successful; {@code false} otherwise *
  • Example:

  • 
      
  • boolean enrolled = enrollStudent(“S123”, “CS101”, 1);
  • if (enrolled) {
  • System.out.println(“Enrollment successful!”);
  • } else {
  • System.out.println(“Enrollment failed.”);
  • }
  • </pre> */

popcorn hack 3

Reflection Question 1

Why is documentation more important in team projects than solo projects?

Documentation is crucial in team projects because multiple people interact with the same codebase. It helps others understand why code was written a certain way, what constraints exist, and how to safely use or modify it. Without documentation, collaboration becomes confusing and error-prone.


Reflection Question 2

Give an example of when a method SHOULD be documented and when it SHOULD NOT.

Should be documented:
A complex method like calculateFinalGrade() that involves multiple inputs, weightings, or special rules should include detailed Javadoc.

Should not be documented:
A trivial method like getName() that simply returns a field does not need documentation because its behavior is self-evident.

Markdown cell → Popcorn Hack #1 doc

Code cell → doSomething() Java code

Markdown cell → Popcorn Hack #2 docs

Code cell → GradeBook Java class

Markdown cell → Homework Hack Part 1 docs

Code cell → Calculator Java class

Markdown cell → Homework Hack Part 2 docs

Code cell → enrollStudent() Java method

Markdown cell → Reflection Answers

Challenge Problems (Extra Credit)

Challenge 1: Document a Recursive Method

Write complete documentation for a recursive method, ensuring the following are included:

  • Base Case: Clearly describe the stopping condition for the recursion.
  • Recursive Case: Explain how the method reduces the problem size and progresses toward the base case.
  • Complexity Analysis: Provide an analysis of time and space complexity, including the recurrence relation.

Challenge 2: Team Documentation Standard

Create a documentation style guide for a team project. Include the following sections:

  • When to Document (and When Not To): Define which methods, classes, and modules require documentation and which do not (e.g., trivial getters/setters).
  • Required Tags for Different Method Types: Specify mandatory tags (e.g., @param, @return, @throws) for constructors, utility methods, and complex algorithms.
  • Example Templates: Provide templates for common documentation scenarios, such as class-level Javadoc, method-level Javadoc, and inline comments.
  • Common Mistakes to Avoid: Highlight pitfalls like vague descriptions, outdated comments, and missing edge case explanations.

Challenge 3: Documentation Detective

Find a poorly documented open-source project. Select one class and:

  1. Write improved documentation for the class, including a detailed description, method summaries, and examples.
  2. Submit a comparison showing the original documentation versus your improved version, highlighting the enhancements made.