Guide to Studying Computer Science

By Mike
Share on facebook
Share on twitter
Share on linkedin

This is a guide for students studying computer science. It covers some of the ideas I teach to my students every day. Here, I’m giving you these ideas for free. It’s my sincere hope that you benefit whether you choose to hire me or not.

You’re faced with a difficult question and you have no idea what to do

To keep this guide short, I want to go right to the heart of the issue: I want to help you in those situations when you are asked to write an algorithm and you have no idea what to do. (This might happen in the AP CS test, for example.)

If you’re stuck, you know you have to take at least the first step. But how do you find it?

It seems difficult, because all the pieces of your code need to work together. How do you choose them, step-by-step, starting from the beginning, so that all of them work together in the end?

We’ll look at these 4 strategies.

  1. Write the test data first
  2. Ask how you would solve the problem as a human
  3. Look for simple solutions you’ve seen before
  4. Write the final algorithm in any order you need to
Writing the test data first

Say your program needs to find the maximum number in a list. First you’ll create some test data.

int[] arr = {1, -10, 20, -2, 17, 34, 2}
Asking yourself how you would solve the problem

Ask how you would solve the problem, as a human. I don’t mean how you would program it, but how you would do it with your own brain.

Look for the biggest number in our list and notice what you’re doing with your eyes and your brain.

Most students tell me something like, “I’m just looking at the page and I see the biggest one.” We often feel that we’re able to process many details at once and just “see” the answer. That’s fine, but then we need to consider how a program does that.

Move your eyes in slow motion to discover the steps

A program works step-by-step. So you need to slow down your process: Again, look at the list of numbers and move your eyes in slow motion. It will soon be clear that your eye has to move across each number, one by one.

Think about how you find the maximum number. If you think about it a little bit, you’ll probably realize that you keep mental track of the largest number you’ve seen so far. Say the first number is 1. As you scan your eyes to the right, you might spot a number bigger than 1, like 20. So the biggest number so far is 20. Keep scanning. You see numbers like -2 or 17, which you know you can ignore because they are less than 20. You keep scanning and replacing the biggest number so far until you reach the end of the list. At the end of my example list, you’ll have 34.

What you discover about yourself becomes the algorithm

How you did it can become how the computer will do it. And scanning is something a program can do easily with a step-by-step algorithm.

In Java, our full solution might look something like this. Don’t worry too much about understanding it yet; we’ll build up this full solution piece-by-piece.

    int[] numbers = {1, 8, 56, -100, 23, 40};
    int max = numbers[0];
    for (int i = 1; i < numbers.length; i++) { 
        if (numbers[i] > max)
            max = numbers[i];
    System.out.println(“The maximum number is: “ + max);

Okay, let’s write this code by using my strategies.

Write the test data first

Write a Java array of numbers we can use as the test data. It will look something like this:

int[] numbers = {1, 8, 56, -100, 23, 40};
Try simple solutions you’ve seen before, like a loop

If you’re midway through a computer science class, you know that a loop can work here. It can step through a list of numbers.

No need to invent that yourself. So now, write the outline of the loop without worring about the details.

for (int i = 0; i < numbers.length; i++) 

[we’ll figure out what goes here]


Write the code in any order

I’m kicking off this code by writing the outline of the loop first. This is just one possible order. You can experiment in any order you like. I’m a believer in the fact that however you write it, you’ll eventually come around to the solution, even if you have to make a few corrections along the way.

Note that I’m starting the loop at index 0. This fact will come back later.

Fill the inside of the loop

Next you need a way to keep track of the maximum number. In the full solution above, the variable ‘max’ does that.

So, we need to fill in the inside of the loop. I realize I’m going fast here, but if you’re in a computer science class, the teacher will have given you many opportunities to learn these things and the overall pace will be slower. The important thing is to have some way of piecing together the information you’re learning in class and I hope my ideas here can help.

for (int i = 0; i < numbers.length; i++)
    if (numbers[i] > max)
        max = numbers[i];   

You know the middle of the loop needs to address this idea of tracking the max number seen so far. You have probably seen something like the above.

Notice that I haven’t declared or initialized ‘max’ yet. At this point, I’m not sure yet if having the variable ‘max’ is the right solution. I just put that in there to see what ideas I come up with next.

Declare and initialize variables

Now I’ll think above how to declare and initialize ‘max’. Notice how this is one of the first lines of code in the order of the program, but I’m getting to it last.

int[] numbers = {1, 8, 56,-100, 23, 40};
int max = numbers[0];
for (int i = 1; i < numbers.length; i++)
    if (numbers[i] > max)
        max = numbers[i];

Remember when you used your eyes to do this, you started with the first number and scanned to the right? That’s why I initialized ‘max’ to the first number, ‘numbers[0]’.

And then I modified the loop. Because I’m already starting with the first number, I can skip it in the loop; that is, I start the index of the loop at ‘1’ (which is the second number in a zero-origin language like Java).

Things to remember
  1. Write the test data first. Experiment with your test data as if you were doing the same job as the computer.
  2. Look for simple solutions you’ve seen before. Like a loop.
  3. Try filling in the code in a non-linear order. You don’t have to write from the beginning to the end.

Enjoy studying computer science! Contact me

Copyright 2020, Mike Mossey
College Board® and AP® are trademarks registered by the College Board, which is not affiliated with, and does not endorse, this site.
PO Box 94611, Pasadena, CA, 91101