AP Computer Science A -- References & Aliases

Parameters | Exceptions | Abstract Classes & Interface | Designing Classes | Sorts | Review

References & Aliases

Clark KentSuperman

Parameters - Value vs. Reference

Values that are passed to a method or constructor are referred to as arguments or actual parameters. The variables in the parameters list of the method declaration are the formal parameters.

When an actual parameter is a primitive type, its value cannot be changed in the method because only the value of the variable is sent to the method. An object reference variable holds the memory address of an object and two or more references that refer to the same object are called aliases of each other. Changing the object's state (its variables) through one reference changes it for all of its aliases.

Null Reference

nullWhen an object reference variable is created but not given the address of an object, it is considered to have a null reference. This simply means that it is supposed to know where the object lives, but it does not. When you create an object reference variable and do not assign it the address of an actual object, it has the value of null which means that is has no value. Null represents a reference that does not point to a valid object. You can also assign a reference variable the value of null to erase its memory of its former object.

This Reference

thisThe keyword this is a reference to the current object, or rather, the object whose method or constructor is being called. It always refers to the currently executing object. It is also referred to as the implicit parameter.

A common place to use the keyword this is when the instance variables have the same name as the parameters in a constructor. The keyword this is used on the left side of the assignment statement to assign the instance variable the values of the parameters. The left side is the instance variable; the right side is the parameter variable. Using this here say, "Hey, I understand that we have the same name, so these are my instance variables and not the parameters."

(The usage of the keyword this is not tested on the AP exam.)

example showing how to use the this reference

In the Whatever screenshot, parameters have the same name as the instance variables. The left side are the instance (class) variables and the right side are the parameters.

Static Modifier

staticStatic is one of the modifiers that determine variable and method characteristics. The static modifier associated a variable or method with its class rather than with an object.

There are times when a class want to have a variable that is shared by all the objects that comes from the class. If any one of the objects changes the value of this variable, the change is reflected in the state of ever object from the class. The variable is declared and assigned a value as though it were an instance variable; however it uses the keyword static in its declaration. For example:

private static int totalCoffeeServed = 0;

Static variables are also called class variables and like all instance variable, they are declared private.


exceptionsProblems in a Java program may cause exceptions or errors which represent unusual or invalid processing. An exception is an object that defines a problem that can usually be fixed. If a program does not handle the exception at all, it will crash and produce a message that describes the exception and where it happened. This information can help you track down the cause of a problem. When an Exception occurs, the normal flow of the program is disrupted and the program terminates abnormally, which is not recommended, therefore, these exceptions are to be handled.

Common exceptions include:

There are certain conditions defined for these exceptions and on the occurrence of those conditions they are implicitly thrown by JVM (java virtual machine). If a method does not handle a checked exception, the method must declare it using the throws keyword. The throws keyword appears at the end of a method's signature.

You can throw an exception, either a newly instantiated one or an exception that you just caught, by using the throw keyword.

Try to understand the difference between throws and throw keywords, throws is used to postpone the handling of a checked exception and throw is used to invoke an exception explicitly.

Here is a good resources to learn more about exceptions, throw, and throws...https://www.tutorialspoint.com/java/java_exceptions.htm

Assignment: Vocabulary Puzzle

Directions: Download the vocabulary puzzle. Define each word as it relates to Java and object-orientated programming in the crossword puzzle. Submit your puzzle to the assignment in itsLearning by scanning or taking a clear/readable picture or give to your instructor.

Assignment:Parameter Passing Worksheet

Directions: Review the Parameter Passing page in itsLearning and then complete the Parameter Passing worksheet in itsLearning.

Quiz: Vocabulary

Directions: Complete the vocabulary quiz as assigned by your instructor.


Assignment: Dream Vacation

Directions: Download the Dream Vacation assignment sheet.

Everyone fantasizes about taking a dream vacation. Handwrite a full DreamVacation class that contains the following:

Submit your handwritten solution to the itsLearning assignment or turn in to your instructor.

Abstract Classes & Interfaces

Interfaces are used to establish contracts through which objects can interface with each other without knowing the implementation methods. An interface definition cannot consist of any data fields or any implementation details such as method bodies.

The Comparable Interface

The Java standard class library contains a number of important interfaces include the Comparable interface. This interface is defined in the java.lang package and contains only one method, compareTo, which takes an object as a parameter and returns an integer. The compareTo interface gives us a way to compare one object to another. One object calls the method and passes another object as a parameter.

Assignment: Demolisher

Directions: Download the Demolisher lab.

Write a Demolisher class which implements the Locatable interface. Demolisher will have x, y, and rate variables. x, y, and rate are integer numbers. You must provide 3 constructors for class Demolisher. Class Demolisher must implement the Locatable interface.

Your response must be handwritten to receive credit for your work.

Designing Classes

A fundamental part of object-oriented software design is determining the classes that will contribute to the program. One way to identify potential classes is to identify the objects discussed in the program requirements. Objects are generally the nouns in a program description. Classes that represent objects should generally be given names that are singular nouns, such as Student, Dice, or Coin. Here are some helpful tips:

When designing classes, there are two pieces of information to take into consideration:

  1. State - how an object is represented
  2. Behavior - what an object does

The state becomes the instance variables of an object. The behavior becomes the methods. When thinking about behavior, you should think about how others might want to use the object.


Sorting is the process of arranging a list of items in order. For example, you may want to alphabetize a list of names. Many sorting algorithms have been developed over the years. In fact, sorting is considered to be a classic area of study in computer science and you should be able to write a sort for the AP test.

Quick Sort

Insertion Sort

The insertion sort algorithm sorts a list of values by repeatedly inserting a particular value into a subset of the list that has already been sorted. One at a time, each unsorted element is inserted into its proper position in the sorted subset until the entire list is in order. You can use this strategy to sort a hand of cards as it is being dealt. Each time a new card is dealt you pick it up and insert it into its proper position in your hand. In the insertion process the other values in the array shift to make room for the inserted element.

Selection Sort

Insertion Sort Steps

  1. begin with a sorted list of one value
  2. Pick up a new value and sort the first two values, swapping if necessary
  3. Insert a third value in the proper place
  4. Continue until all values are inserted into their proper places

Sorting an Array of Objects with Insertion Sort

This version of the method accepts an array of Comparable objects and uses the insertion sort

Selection Sort

Selection Sort

The selection sort algorithm sorts a list of values by successively putting particular values in their final, sorted positions. In other words, for each position in the list, the algorithm selects the value that should go in that position and puts it there.

Selection Sort

Selection Sort Steps

  1. Scan list to find the smallest value
  2. Swap the value with the value in the first position of list
  3. Scan the rest of list to find next smallest value to swap with value second position
  4. Scan the rest of the list to find next smallest, etc.
  5. Continue scanning and swapping until entire list is sorted

There are a lot of reasons for choosing one sorting algorithm over another. We might choose an algorithm because it is simple, fast, uses less memory, or works well on the kind of data we have. An algorithm that is easier to understand is also easier to write and debug. However, often the simplest sorts are not efficient. Efficiency is usually the first thing we look for. There are several algorithms that are more efficient than the insertion sort and the selection sort but they are also more complicated.


Lab: A Sorted Integer List

Directions: Download the A Sorted Integer List lab. File IntList.java contains code for an integer list class. Save it to your directory and study it; notice that the only things you can do are create a list of a fixed size and add an element to a list. If the list is already full, a message will be printed. File ListTest.java contains code for a class that creates an IntList, puts some values in it, and prints it. Save this to your directory and compile and run it to see how it works.

Now write a class SortedIntList that extends IntList. SortedIntList should be just like IntList except that its elements should always be in sorted order from smallest to largest. This means that when an element is inserted into a SortedIntList it should be put into its sorted place, not just at the end of the array. To do this you'll need to do two things when you add a new element:

Now you can insert the new element in the location you originally stopped on.

All of this will go into your add method, which will override the add method for the IntList class. (Be sure to also check to see if you need to expand the array, just as in the IntList add method.) What other methods, if any, do you need to override?

To test your class, modify ListTest.java so that after it creates and prints the IntList, it creates and prints a SortedIntList containing the same elements (inserted in the same order). When the list is printed, they should come out in sorted order.

Follow your teacher's instructions for submitting the code of the lab as well as a screenshot from BlueJ.

Assignment: Unit 6 Review Worksheet 1

Directions: Complete the review worksheet in itsLearning to review for the unit assessment.

Assignment: Unit 6 Review Worksheet 2

Directions: Complete the review worksheet in itsLearning to review for the unit assessment.



Presentation: Static Modifier

Presentation: Exceptions

Presentation: Abstract Classes & Interfaces

Presentation: Sorts (note: files referenced in the presentation correspond to the textbook Java Software Solutions)


Clark Kent photo: http://superman.wikia.com/wiki/Clark_Kent

Superman photo: http://superman.wikia.com/wiki/Superman