Jump to content

AP Computer Science/Object-Oriented Programming

From Wikibooks, open books for an open world

The basic task of object-oriented programming is to connect the data with the methods that can operate on that data. Before the introduction of Object-Oriented Programming, many other types of paradigms were used such as procedural programming. While object-oriented programming can be used to describe a procedural process, it is more powerful than the traditional procedural programming paradigm.

The AP Computer Science exam makes use of the Java programming language, which is a class-based object-oriented language. In this module we will introduce the main topics of object-oriented design.

Objects

[edit | edit source]

The basic object is a collection of data and the methods that can be used with them. We will abstract away the actual implementation of the classes and instead will present only diagrams.

The diagram presented here represents a bank account. The bank account does not need to be described in all detail, we only need to know the amount of money that is currently in the account and how to identify the account. The operations that can be used on the bank account are create, deposit, withdraw, and close. We do not allow the client to withdraw a dollar and cents; they can only withdraw an integral value.

The object presented here would be translated into Java as the following:

 public class Account {
   Integer balance = 0;
   String accountIdenitifier;
   public Account(Integer balance, String accountIdentifier) {
     ..
   }
   public void deposit(Integer amount) {
     ..
   }
   public boolean withdraw(Integer amount) {
     ..
   }
   public Integer close() {
     ..
   }
 }