Jump to page content Jump to navigation

College Board

AP Central

AP Exam Reader
AP Annual Conference - Save the Date
Siemens Awards for Advanced Placement

Print Page
Home > AP Courses and Exams > Course Home Pages > Java as a Pre-AP Strategy in Computer Science, Part V

Java as a Pre-AP Strategy in Computer Science, Part V

by James Aldridge, Ph.D.
Fort Worth Country Day School
Fort Worth, Texas

Object-Oriented Program Design
Utilization of Developing Student Skills
Engagement of Student Interest

This is Part V in a series of articles designed to take a computer science teacher in the Pre-AP years through the more difficult and confusing first components of teaching computer science in Java. In Part V we discuss the possible role of projects in a computer science course at this level. The final article in this introductory series will discuss methods of student assessment.

In the early part of a course, it seems appropriate to limit assignments to short, focused code that illustrates a specific concept. As students get more Java under their belts (caffeine-free, of course!), longer projects become both possible and, in my opinion, generally desirable.

Longer projects may work best in smaller classes and among highly motivated students. In larger classes, which can be a lot like herding cats, shorter projects may be better suited. Likewise, if students are less motivated to stay on task without frequent deadlines, shorter projects may also be in order.

Short or long, the projects need to be designed with three goals in mind: (1) the promotion of object-oriented program design, (2) the utilization of developing student skills, and (3) the engagement of student interest.

Object-Oriented Program Design
For beginning Java teachers, this may be the most elusive of the three goals. Solid object-oriented design takes a lot of experience for mastery, a prerequisite not met by a lot of secondary computer science teachers. Having faced that head-on, it must be said that careful attention in the design stages of a project will give the work the essentials of good object orientation. That will suffice: computer science for the Pre-AP years is, after all, an introduction to an introductory course, AP Computer Science.

You might begin with a small project to illustrate the essential nature of objects: the encapsulation of both form and function (fields and methods, respectively) into a class structure. Toss in the idea of data hiding and you're off to a good start. The primary motivation behind class structure is to promote easy code maintenance and frequent code reuse. Structuring code as a group of related classes makes it easier to follow, easier to modify, and easier to reuse parts in new, often quite different programs.

Last year I used a Spacecraft class to begin:
public class Spacecraft
{
   // private, hidden fields

   private String name;   // i.e.,  USS Challenger
   private int speed;   // m/s
   private int maxSpeed;   // m/s
   private int fuel;   // L
   private int maxFuel;   // L
   private double position;  // m traveled from 
                             //start of trip (like an odometer)
	
   private final int mpL = 2000;   // fuel mileage in m/L
	
   // a constructor ================================================

   public Spacecraft(String n, int maxS, int maxF)
   {
	   name = n;
	   speed = 100;   // default speed
	   maxSpeed = maxS;
	   fuel = 1000;   // default fuel
	   maxFuel = maxF;
	   position = 0.0;
   }
	
   // get methods ==================================================
	
   public String getName()
   {
	  return name;	
   }	
	
   public int getSpeed()
   {
	   return speed;	
   }
	
   public int getMaxSpeed()
   {
	   return maxSpeed;	
   }
	
   public int getFuel()
   {
	   return fuel;	
   }
	
   public int getMaxFuel()
   {
	   return maxFuel;	
   }
	
   public double getPosition()
   {
	   return position;	
   }
	
   // set methods ==================================================
	
   public boolean setSpeed(int s)
   {	
	   boolean resultFlag = false;
	
	   if (s <= maxSpeed)
	   {
		   speed = s;	
		   resultFlag = true;
	   }
		
	   return resultFlag;
   }
	
   // some other public methods ====================================
	
   public boolean addFuel(int f)
   {
	   boolean resultFlag = false;
	
	   if ((fuel + f) < maxFuel)
	   {
		   fuel += f;	
		   resultFlag = true;
	   }
	   else
	   {
		   fuel = maxFuel;	
	   }
		
	   return resultFlag;
   }
	
   public boolean travel(int t)   // travel time in s
   {
	   boolean resultFlag = false;

	   int requestedTravel = speed * t;   // in m
	   int maxTravel = fuel * mpL;   // in m

	   if (requestedTravel < maxTravel)
	   {
		   position += requestedTravel;
		   fuel -= ((speed * t) / mpL);
		   resultFlag = true;
	    } 
	   else
	    {
		   position += maxTravel;
		   fuel = 0;
	   }
		
	   return resultFlag;
   } 
	
   public String toString()
   {
	   String s1 = " Spacecraft name: " + name + "\n";
	   String s2 = "           speed: " + speed + " m/s \n";
	   String s3 = "   maximum speed: " + maxSpeed + " m/s\n";
	   String s4 = "            fuel: " + fuel + " L \n";
	   String s5 = "    maximum fuel: " + maxFuel + " L\n";
	   String s6 = "current position: " + position +
	   " m traveled so far.";
	   return (s1 + s2 + s3 + s4 + s5 + s6);
   }
}
And here is the associated test file:
public class TestSpacecraft
{
   public static void main(String args[])
   {
	   Spacecraft sc1 = new Spacecraft("USS Challenger", 10000, 4000);
		
	   System.out.println(sc1);
		
	   if (sc1.travel(1000))
	   {
		   System.out.println("\nTravel fully successful!\n");
	   }
	   else
	   {
		   System.out.println("\nTravel not fully successful.\n");
	   }
		
	   System.out.println(sc1);
   }	
}
These two classes cover a lot of ground. Some notes:
  • The final variables cannot be given a new value; they're essentially constants.
  • The get and set methods provide read and write access to private fields.
  • Most classes implement a toString()method that overrides the simple one inherited from Object (all Java classes are descendants of class Object).
  • TestSpacecraft's only method is static; this means that it can be used without creating an instance of the enclosing class; in this case, the Java Virtual Machine invokes main() without creating any instance of class TestSpacecraft.
We go on to play around by creating several instances of Spacecraft and then extending the class to introduce inheritance:
public class SuperSpacecraft extends Spacecraft
{
   // private, hidden fields
	
   private int photonTorpedoes;   // count
   private int phaserMegaJoules;   // in MJ

   // a constructor ================================================

   public SuperSpacecraft(String n, int maxS, int maxF,
   int torps, int phaserCharge)
   {
	   super(n, maxS, maxF);
		
	   photonTorpedoes = torps;
	   phaserMegaJoules = phaserCharge;	
   }	
	
   // get methods ==================================================
	
   public int getTorpedoes()
   {
	   return photonTorpedoes;
   }
	
   public int getPhaserCharge()
   {
	   return phaserMegaJoules;	
   }

   // some other public methods ====================================

   public void addTorpedoes(int t)
   {
	   photonTorpedoes += t;
   }
	
   public void addCharge(int c)
   {
	   phaserMegaJoules += c;	
   }
	
   public boolean firePhotonTorpedoes(int t)
   {
      // implementation here
	  
	  return true;
   }
	
   public boolean firePhasers(int p)
   {
	   // implementation here	
		
	   return true;
   }
	
   public String toString()
   {
	   String s1 = super.toString();
	   String s2 = "\n     photo torps: " + photonTorpedoes + "\n";
	   String s3 = "   phaser charge: " + phaserMegaJoules + " MJ\n";
	   return (s1 + s2 + s3);
   }
}

And here is the associated test file:

public class TestSuperSpacecraft
{
   public static void main(String args[])
   {   
	   Spacecraft craft[] = new Spacecraft[10];
		
	   for (int index = 0; index < 10; index += 2)
	   {
		   craft[index] = new Spacecraft("USS Enterprise " +
		   index, 5000, 12000);
		   craft[index + 1] = new SuperSpacecraft("USS Voyager " +
		   index, 5000, 12000, 50, 4525);
	   }
		
	   for (int index = 0; index < 10; index ++)
	   {
	   	   System.out.println("Craft " + index + "\n:"
		   + craft[index].toString() + "\n==========\n");
	   }
		
   }	
}
Some notes:
  • There's a superclass call in the constructor and in the toString() method.
  • The test file uses arrays and introduces polymorphism by using references to Spacecraft objects to refer to both Spacecraft and SuperSpacecraft objects.
After the various spacecraft are up and running, students are encouraged to use them in creative ways: some set up races using random travels, while others fashion games by adding old-fashioned "adventure-type" gaming commands. Some students extend travel to three dimensions and add planets and so forth. This is a good prelude to the AP Marine Biology Case Study.

After a series of small projects like this, students generally have made their way through most Java basics. Some additional topics for these small projects include simulations (we do one on simple harmonic motion and one on projectile motion), statistical analyses, probability simulations (rolling dice, card games), databases, simple text encryption/decryption, text analysis (e.g., "Did Shakespeare write this?"), and many others. The AP Computer Science Electronic Discussion Group is a great source for more inspiration.

As the year progresses and language basics are mastered, you can move on to more complex projects. I suggest at least one project in which portions are divvied up among several students. This enforces the need for clear design goals and well-communicated public interface specifications. Ideally, each student would have the responsibility to code one or several classes making up the project. Students are responsible for integration and testing the final application.

Utilization of Developing Student Skills
It is important that all assignments, including medium and large projects, key on developing student skills, especially early in the learning curve. As students master most of the basics of Java, more integrated projects become possible.

I make a list of skills that I would like to emphasize with each project. Some of these are design skills, some are language skills, and some are more algorithmic or problem-solving skills. Then I lay out the project and inventory the skills necessary to complete it. I compare the skills required to those taught and either do some more teaching or modify the project if the skill lists do not match. This sort of project assessment is crucial to the design of high-quality student projects.

Engagement of Student Interest
This can be a challenging one. Early on, code that deals with interesting scenarios like spacecraft can be helpful. Later in the course, I think it's important to introduce graphical user interfaces. Students expect applications to be GUIs, and rightly so. I also think it's important to let advanced or "gifted and talented" students investigate more sophisticated aspects of Java, including such topics as images, sound, networking, and Java3D. Through all of this, it's important to stay focused on the essentials of good program design. Teaching linked lists in the context of an MP3 player is a lot more interesting to students than using some arbitrary data. Java3D is based on a tree-traversal rendering model, and the advanced use of tree data structures can be studied in this context. Networking and animation provide good platforms for discussing the importance of multithreaded code. Art-oriented students will like playing with Java2D and Java3D; there's room for lots of didactic fun in those APIs. Enjoy!


Please note: Some topics covered in this series of articles are not included in the defined curriculum. Refer to the current official AP CS Course Description for the topics that will be tested on the AP Exams.

James Aldridge, Ph.D., is a science and computer science teacher at Fort Worth Country Day School in Fort Worth, Texas. He has taught computer programming for 10 years and currently teaches AP Computer Science using Scheme, Java, and C++. James also teaches AP Chemistry and has special computer science interests in the area of real time data acquisition and control subsystem hardware and software design.





  ABOUT MY AP CENTRAL
    Course and Email Newsletter Preferences
  AP COURSES AND EXAMS
    Course Home Pages
    Course Descriptions
    The Course Audit
    Sample Syllabi
    Teachers' Resources
    Exam Calendar and Fees
    Exam Questions
    AP Credit Policy Information
  PRE-AP
    Teachers' Corner
    Publications
  AP COMMUNITY
    About Electronic Discussion Groups
    Become an AP Exam Reader

Back to top