AP Computer Science A -- Unit 6


Objects Revisited | Writing Classes | Encapsulation & Visibility Modifiers | Methods | Sorts | Review


Enhancing Classes

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 or to your instructor.

Quiz: Vocabulary

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

vacation

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.

References

Object references play an important role in a program. Objects and object references are two different things.

Null Reference

When 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. You can also assign a reference variable the value of null to erase its memory of its former object.

This Reference

The keyword this is a reference to the current object, or rather, the object whose method or constructor is being called. 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 variables and the right side are the parameters.

Static Modifier

Static 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.

Exceptions

Problems 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

Interface

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 properties. 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.


Sorts

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.

Quick Sort

Insertion Sort

The insertion sort algorithm sorts a list of vlaues by repeatedly inserting a particular vlaue 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 submet unti the entire list is in order. you canuse 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

Selection Sort

The selection sort algorithm sorts a list of values by successively putting particiular 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

Thre 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 the insertion sort and the selection sort but they are also more complicated.

integers

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

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

Review


Resources

presentation:

presentation:

presentation: