MP2: Functions

Functions are an important part of how we structure computer programs. Breaking your code into well-documented functions makes it easier to understand, easier to test and debug, and easier for others to use.

MP2 comprises four small functions that exercise your growing programming abilities in several different ways. You’ll get practice working with strings and arrays, and a gentle introduction to two-dimensional arrays.

MP2 is due Monday 9/24/2018 @ 5PM. To receive full credit, you must submit by this deadline. Note that the deadline is right before the first CS 125 midterm, but we expect that MP2 will help you prepare for the midterm exam. In addition, 10% of your grade on MP2 is for submitting code that earns at least 20 points by Friday 9/21/2018 @ 7PM. Note that, unlike MP1, you have to do a bit more work on MP2 to get to 20 points.

As usual, late submissions will be subject to the MP late submission policy.

1. Learning Objectives

MP2 aims to provide practice with defining, documenting, and implementing functions—the basic building blocks of computer programs. You’ll begin to learn how to:

  1. declare and implement functions

  2. write Javadoc comments for your new functions

  3. read Javadoc documentation to determine how to implement a function, and

  4. use simple two-dimensional arrays.

We’ll also continue to reinforce the learning objectives from MP0 and MP1.

2. Assignment Structure

MP2 consists of four short programs:

  1. Anagram.java: determines if two strings are anagrams. You will need to complete this function.

  2. CheckPassword.java: checks if a password meets certain requirements. You will need to declare and complete this function.

  3. StringSplitter.java: splits a string whenever the character changes. You will need to declare, complete, and document this function.

  4. TimesTable.java: produces a times table given two numbers. This file doesn’t even exist yet! You’ll have to create it, and then declare, complete, and document generateTimesTable. You’ll also need to add a main method if you want to test it without using test suite.

You will need to use our official MP2 online documentation to understand what the functions are called and what they are supposed to do, since we have not always provided documentation—or even a file to start with at all!

2.1. Obtaining MP2

Use this GitHub Classroom invitation link to fork your copy of MP2. Once your repository has been created, import it into IntelliJ following our assignment Git workflow guide.

2.2. Your Goal

At this point you should be familiar with the requirements from MP0 and MP1. MP2 is similar. However, unlike MP0 and MP1 you no longer get free points for your code compiling. In the real world it doesn’t matter if your code compiles—it has to work!

3. Fixing Problems

All of the MP0 and MP1 about how to approach the MPs still apply. But as usual, we’ll furnish your continued development as programmers with some additional advice—this time about fixing problems.

We’ve all been there. My stupid code is not working! Why not? I’m angry, I’m frustrated, I want to throw my computer through a window or stomp it to bits. What should you do?

3.1. Calm Down

Well, the first thing to do is calm down. This can be a good time to get a cup of coffee, take a walk, engage in some YouTube browsing, catch up on email, or whatever 1 Get your head out of what you are doing for a few minutes and you’ll be fresher when you get back.

3.2. Accept Responsibility

Second, accept that something is wrong with your code. If it’s not doing what you want, then that’s obvious. And for CS 125, the test suites are the arbiters of correctness. So if you aren’t passing them, then your code is incorrect. Period.

3.3. Do A Slow, Close Read

Now that you’re calmer and ready to find your mistakes, go through your code carefully line by line. This can be a good time to do some cleanup—add comments where needed, improve your variable names, get all of your indentation and formatting worked out, etc. Sometimes looking at your code in a slightly different way causes a bug to become apparent. And you might as well be doing other useful tasks as you do your close read.

During your first slow read it can be helpful to actually read code out loud, or at least in your head so that you don’t bother others around you. For example, if you’re examining the code below:

You might say:

  1. "For every index value of searchArray" (you want to develop a shorthand for loops)

  2. "If the index member of anotherArray is equal to what I’m search for"

  3. "Then break."

  4. "But wait, why am I using an index from searchArray to look in anotherArray? That’s probably wrong."

This is a place where it really helps to use variable names that aren’t i and j, and to pull the current value of arrays into a local variable immediately or use the enhanced for loop syntax.

3.4. Add Instrumentation

Once you’ve completed one pass, start over and begin adding some System.out.println statements. This is sometimes called printf debugging, after the C language printf statement—it’s equivalent to System.out.println in Java. In Java, of course, you would use System.out.println to do this.

You will find that there are snobs out there that look down on printf debugging. Hook up a real debugger, they’ll say. Or rewrite your entire project using a functional programming language where bugs are impossible—as if that’s a real possibility.

And then there are the rest of use that just want to build cool and useful things as quickly as possible, and who will happily take the chance to add instrumentation as needed. printf debugging is not powerful enough to solve every problem, and there are times that you will need better tools. But use it when you can. And remind the snobs what none other than Brian Kernighan says:

The most effective debugging tool is still careful thought, coupled with judiciously placed print statements.

Usually the right approach is to start at the top of the block of code that you are examining and use System.out.println to examine things that you are unsure are working properly. This is particularly useful when used in conjunction with test suites.

3.5. Consider A Different Approach

If you are really stuck, it might be time to try a different approach. This is particularly true when you can acknowledge that your current approach has some, shall we say, limitations. Maybe you thought that this particular way of doing things would work out well, but then you had to add one hacky bit, and another. And now it’s rapidly approaching spaghetti code that you’re not even sure that you understand anymore.

In that case, by all means start over! You’re learning, and you’ll learn more by completing a few alternative implementations than you will by clinging to an ugly solution. Feel free to talk to another student—In English—about how they approached the problem. Maybe they found a much cleaner way that you can try.

3.6. Getting Help

The course staff is ready and willing to help you every step of the way! Please come to office hours, or post on the forum when you need help. You should also feel free to help each other, as long as you do not violate the academic integrity requirements.

4. Grading

MP2 is worth 100 points total, broken down as follows:

  1. 20 points: Anagram.java

    • 20 points for passing the test

  2. 20 points: CheckPassword.java

    • 20 points for passing the test

  3. 20 points: SplitString.java

    • 20 points for passing the test

  4. 20 points: TimesTable.java

    • 20 points for passing the test

  5. 10 points for no checkstyle violations

  6. 10 points for submitting code that earns at least 20 points before Friday 9/21/2018 @ 7PM.

Note that, unlike MP1, you do need to do a bit of work to get to 20 points, since there are no free points for just getting things to compile.

4.1. Test Cases

Like MP0 and MP1, we have provided exhaustive test cases for each part of MP2. Please review the MP0 testing instructions.

4.2. Autograding

Like MP0 and MP1, we have provided you with an autograding script that you can use to estimate your current grade as often as you want. Please review the MP0 autograding instructions.

5. Submitting Your Work

Follow the instructions from the submitting portion of the CS 125 workflow instructions.

And remember, you must submit something that earns 20 points before Friday 9/21/2018 @ 7PM to earn 10 points on the assignment.

5.1. Academic Integrity

Here’s how you’ll feel if we catch you cheating in CS 125:

CS 125 is now CS 124

This site is no longer maintained, may contain incorrect information, and may not function properly.


Created 10/24/2021
Updated 10/24/2021
Commit a44ff35 // History // View
Built 10/24/2021 @ 21:29 EDT