Home » Archive

Articles in the 2009 AP CS A Category

2009 AP CS A »

[31 Jan 2010 | No Comment | ]

I would like you to comment these using the precondition/postcondition commenting style we have recently seen.  What must be true in order for the method to run as promised?  What will be true after the method has run?
Remember that method that modify arrays don’t need to return them, necessarily…
Double method
Write a static method doubleAll that takes an array of integers as a parameter and doubles each of its elements.
Reverse method
Write a static method reverse that takes an array as a parameter and reverses its content.  Yes, we will have done …

2009 AP CS A »

[31 Jan 2010 | No Comment | ]

We’ve talked a little bit about how variables that refer to objects don’t actually hold the objects themselves but instead a reference to one. That hasn’t really meant a lot to us yet, though. With arrays, it starts to be very important to pay attention to that distinction. Why?

When one variable of an object type is assigned to another, the object is NOT copied but instead, both variables refer to the same object
Modifying the value of one variable affects ALL references

Look at the following code:
int[] grades = …

2009 AP CS A »

[31 Jan 2010 | No Comment | ]

Wow, you guys! You have a semester of programming under your belts! I am so impressed with each and every one of you. Working with you has been an incredible joy — you are all curious, fun and hard-working. I’m really looking forward to another semester with all of you.
Exams also looked really good — it’s nice to see all of the knowledge you have come together. I know you grumble at paper tests but I promise you that they’re practicing different and important skills …

2009 AP CS A »

[24 Jan 2010 | One Comment | ]

Arrays from user input
Write a static method favFoods that first asks the user how many favorite foods he or she has then prompts him or her for those foods and stores them in an array.  Your method should return that array.  Sample execution (user input underlined):
How many favorite foods do you have? 3
Food name? Lasagna
Food name? Artichokes
Food name? Cake
The array returned to main should have the following content: ["Lasagna", "Artichokes", "Cake"].  What is its type?
Parallel arrays
It is sometimes useful to use multiple arrays in which data at the same index …

2009 AP CS A »

[19 Jan 2010 | No Comment | ]

You are to write a series of static functions to gain experience working with arrays.  You can have one big ol’ ugly main for this one!  These are great computational thinking problems.  Feel free to jump around as you wish.
Remember to test each method thoroughly!
1) Write a static method that returns an int array of length 3 containing the first 3 digits of pi, {3, 1, 4}.
makePi() → {3, 1, 4}
2) Write a static method that, given an array of ints, returns true if the array is length 1 or …

2009 AP CS A »

[19 Jan 2010 | No Comment | ]

Think back to the Student class you wrote recently.  A student has a name as well as some test scores.  In the initial implementation we made, our Student objects could only keep track of up to 3 test scores.  Not very flexible, is it?  Beyond that, many of you noted that the code to set test scores was really redundant.
When dealing with multiple related values, arrays are the way to go.  You can think of them as lists of values that all have the same type.  For example, I could …

2009 AP CS A »

[11 Jan 2010 | No Comment | ]

This program will give you practice with while loops, Random and using objects.  Since this project doesn’t have clear objects to create like dice or games, you will be writing everything using static methods.  I want you to get a feel for when it’s more appropriate to use classes that can be instantiated and when it’s more appropriate to use static methods, so make sure you’re thinking about this.  Optionally, you can create a RandomWalker class.  This will allow you to have more than one RandomWalker on the screen, which …

2009 AP CS A »

[5 Jan 2010 | No Comment | ]

In a system like The Source, student information is stored in a database.  Every time student information is requested, an object is most likely built from the database information.  We’re going to design and build a Student class.
This is a little unusual (as in it’s probably not the kind of thing your boss would ask you to do), but should serve as good practice — I am giving you the client code from which you need to write the class from which to build appropriate objects.  Download StudentGrades.java and write …

2009 AP CS A »

[3 Jan 2010 | No Comment | ]

I know that the shift to object-oriented programming was a little tricky and with the break we just had you’re bound to feel a little unsure of how to start.  Hopefully this guide will remind you how to get started and be a useful reference to you as you write more and more complex classes.  This text is HEAVILY inspired by Cay Horstmann’s Big Java although I do things in a slightly different order.  Any errors or bad advice are my own.
As you follow this tutorial, write the code and …

2009 AP CS A »

[3 Jan 2010 | No Comment | ]

I hope everyone had a fantastic break and is excited for 2010!
I got some really fun programs from your break homework — thanks for taking the time to do these!  All of the ones I’ve seen have been fantastic, from the program that calculates my expected life span based on the length of my name to the program that spells out words using CS concepts.  I’ve also gotten a few graphical submissions I thought I’d share.  Click to enlarge — a few are animated!  I’ll have to show you the …