Advanced Placement Computer Science


Course Overview
Course Policies
Computer Science AP
   Syllabus 
   Class Schedule
   Homework
   Labs
   Projects
   Extra Credit
   Study Material
   Grades
Computer Science 2 AP
Get Software
Useful Resources
Teacher Information


Labs for Computer Science AP



  • Lab 26 - Due at the start of class on 3/23/04
Do problems 13.2 & 13.3.  To do this you NEED to read section 13.1.  Note: it defines the selection sort in the OPPOSITE order of how it was defined in class.  (both are valid definitions of the algorithm)  This is an individual lab and is worth 2 points.
  • Lab 25 - Due at the start of class on 3/11/04
Write a text editing program. 

Put more clearly, you need to write a GUI program that has
  • a text field to allow the user to input the file name (section 10.3 of the book)
  • a button to open the file
  • a button to save to the file (use the same text field to tell you which file to actually read and save to)
  • a button to allow the user to exit the program (call System.exit(0); in its actionPeformed method)
  • you also need another button to tell the user how many tokens exist in the file (this should count the total tokens in the file, and then display a JOptionPane screen that tells the user how many exist). 
You also need a JTextArea to actually display all of the text, and to allow the user to modify the text (section 10.7 of the book).  When the user clicks save, grab the text to save from that JTextArea, and when the user opens the file for reading, place the contents of that file into the JTextArea.  (add the read in lines together with "\n" strings between them to keep the formatting the same)
Hint: when you are outputting the text from the JTextArea to the file, use the print method, not the println method to avoid adding a extra line feed.
If the user tries to open a file that does not exist or save to a file that does not exist, you should use JOptionPane to display an error message.

Feel free to use existing code on THIS site as a template for a GUI program.

To be done individually and is worth 8 points

  • Lab 24 - Due at the start of class on 2/17/04
Define an interface called Hero that requires all classes that implement it to define a method called destroyenemy that takes no parameter and has a return type of void.  It should also have a constant value called MaxScore that is set to 30
In a seperate class, implement the interface you defined above, declare an instance integer to track the score and implement a destroyenemy method that increases the score by one (but make sure that you do not exceed MaxScore)

To be done individually on paper, and is worth 3 points.

  • Lab 23 - Due at the start of class on 2/17/04 (NOTE:  Lab 24 will ALSO be due on this date)
Write a program that asks the user what sort of data they want to enter into an array.  (Prompt them with something that says "Hit 1 for integers, 2 for Strings" and then create an array of type Object of size 10.  Read in 10 integers or 10 Strings into the Array.  (depending on what the user selected).  Once you have it in the array, call the method defined below with the name of the array as its parameter:

public static void sort(Comparable[] array) {
        Comparable temp;
        for (int b = array.length-1; b > 1; b--) {
            for (int a = 0; a < b; a++) {
                if (array[a].compareTo(array[a+1])> 0) {
                    temp = array[a+1];
                    array[a+1] = array[a];
                    array[a] = temp;
                }
            }
        }
    }

And after that print out the array elements one by one.  (remember some are integers, and not Strings)

Big tip:  Remember, you can NOT directly put integers into Objects, you need to put them in a wrapper class first... (section 11.4)

This is a partnered lab and worth 4 points.
  • Lab 22 - Due at the start of class on 2/11/04
Do book problems 11.6 & 11.7, worth 3 points and this is a partnered lab. 
  • Lab 21 - Due at the start of class on 2/9/04
Implementing the Comparable interface (read 6.3 for information) implement a class that handles fractions.  (you should store the fractions as two instance variables (integers), a numerator and a denominator).  You need the following methods in the code:

Any methods that Comparable requires
public void setValues(int numeratorValue, int denominatorValue)
a method that simply sets the internal instance variables
public int getNumerator()
 a method that returns the numerator
public int getDenominator()
 a method that returns the denominator
public void reduceFraction()
a method that will try to simplify the fraction as much as it can.  (eg: make 6/3 into 2/1 or 12/8 into 3/2)  Just try to find the biggest number that divides into both numbers without leaving a remainder. 

Hint:  you might want to try to find the GCD by counting up from 1 until the number is bigger than the denominator or the numerator.  While counting, see if the number divides into both the denominator and numerator without leaving a remainder.  (hint: %)  The last number that you find that evenly divides into both is what you want to divide the numerator and the denominator.

For the sake of comparing one fraction to another, you probably want to convert the values of it to a double.  (ie: actually divide the numerator by the denominator and store it in a double in that method)  You can also assume that the Object parameter in the method Comparable requires MUST be of type Fraction.  (you still need to cast it though)

This is a partnered lab and worth 4 points.
  • Lab 20 - Due at the start of class on 1/28/04 (at West Torrance) or 2/3/04 (at Peninsula)
Do the problems here on paper.  This is an individual lab, and worth 4 points.

  • Lab 19 - Due at the start of class on 1/27/04 (at West Torrance) or 2/2/04 (at Peninsula)
Write a program that opens a file the user selects and prints out the number of lines that exist in the file.   This is a partnered lab and worth 3 points. 
  • Lab 18 - Due at the start of class on 1/23/04 (at West Torrance) or 1/29/04 (at Peninsula)
Revise the Tic Tac Toe project (a functional copy of the full project's code is here) so that it has appropriate try/catch clauses for all of the places the program could reasonably generate exceptions. Make certain the program recovers from every exception that it reasonably can.  (if the user types a blank into a JOptionPane, you should be able to recover and prompt the user for another input)

This is a partnered lab and worth 4 points.



last updated on 4/5/04
Copyright (C) 2004 Jim Casaburi