AP Computer Science A -- Unit 4

The intent of an object-oriented programming language is to provide a framework that allows a programmer to manipulate information after storing it in an object.


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


Object Revisited

Writing Classes

coinsRecall that is a class is a blueprint of an object. It has no memory space for data. Each object has its own data space, this its own state.

A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. Think of a method as a subprogram that acts on data and often returns a value. Each method has its own name. A method name should be a verb and follow lowerCamelCase style.

Let's examine a Coin class and a CountFlip program. You can also download and import Coin.java and CountFlip.java to closer examine. Download the Coin Flip Program code to follow along with the video.

In our Coin class we could define the following data:

We might also define the following methods:

A program will not necessarily use every service provided by an object. Once an object class has been defined, we can use it again in other programs as needed.

Assignment: Writing Classes Vocabulary

Directions: Download the vocabulary sheet. Define each word as it relates to Java and object-orientated programming. Submit your definitions to the assignment in itsLearning.

Assignment: Characteristics of an Object

Directions: Review the definition of state, behavior, and identity listed below.

Determine which of the descriptors are state, behavior, or identity for a car like the example shown below. Complete the worksheet in itsLearning.

car

grades

Lab: Tracking Student Grades

Directions: A teacher wants a program to keep track of grades for students and decides to create a student class for his program as follows:

Download the full Tracking Student Grades lab. Submit your code and a screenshot of the output from the BlueJ terminal window to the itsLearning assignment. Be sure that you have included comments at the top of your files that include your name and the current date.

Lab: Bank Account

piggy bank

Directions: Recreate the code listed in the Bank Account lab. Save it to your directory and study it to see what methods it contains. Then complete the Account class as described below. Note that you won't be able to test your methods until you write ManageAccounts as directed below.

  1. Fill in the code for method toString, which should return a string containing the name, account number, and balance for the account.
  2. Fill in the code for method chargeFee, which should deduct a service fee from the account.
  3. Modify chargeFee so that instead of returning void, it returns the new balance. Note that you will have to make changes in two places.
  4. Fill in the code for method changeName which takes a string as a parameter and changes the name on the account to be that string.

File ManageAccounts.java contains a shell program that uses the Account class above. Save it to your directory, and complete it as indicated by the comments.

Modify ManageAccounts so that it prints the balance after the calls to chargeFees. Instead of using the getBalance method like you did after the deposit and withdrawal, use the balance that is returned from the chargeFees method. You can either store it in a variable and then print the value of the variable, or embed the method call in a println statement.

Submit the code and a screenshot from the BlueJ terminal window. Be sure that you have included comments at the top of your files that include your name and the current date.

Encapsulation & Visibility Modifiers

Data encapsulation is the act of hiding the values of the instance variables from other classes.

Declaring instance variables as private encapsulates them. Declaring instance variables as public allows instance access to them using the dot operator.


Methods

Methods that have the same name but different parameters lists are called overloaded methods. The computer examines the method's signature (name and parameters) and decides which of the overloaded methods is the correct method to use based on the parameter list.

Assignment: Anatomy of a Method Worksheet

Directions: Identify the parts of a method in the itsLearning worksheet.

Quiz: Anatomy of a Method Quiz

Directions: Review the parts of a method and be prepared to take the quiz as assigned by your instructor.

A return statement is the return reserved word followed by an optional Expression. After the statement executes, control is immediately returned to the calling method, returning the value defined by Expression.

Overloaded methods must have the same name and may have a different return type; however, they must differ by one of these ways:

Assignment: Writing Methods in Java Worksheet

Directions: Complete the Writing Methods in Java worksheet in itsLearning.

Assignment: Classes Worksheet

Directions: Open the Exam class and answer the questions regarding the class in the worksheet in itsLearning.

The Band

Lab: Band Boosters Class

Directions: In this exercise, you will write a class that models a band booster and use your class to update sales of band candy.

Download the full Band Booster Class lab. Submit your code and a screenshot of the output from the BlueJ terminal window to the itsLearning assignment. Be sure that you have included comments at the top of your files that include your name and the current date.

Lab: Marshmallow Monster

Directions: Using the supplies in your baggie, construct a Marshmallow Monster. You do not have to use all of the supplies but your monster but have zero or more arms, legs, eyes, buttons, and name. Use the two marshmallows to form the head and the body. Use the toothpicks and icing (glue) to connect the head the body (mouth and nose optional).

Marshmallow Monster Selfie

Then write the class, constructors to construct the monster, and tester. You will write a default Marshmallow monster object with no values for all of the data members. Then write an overloaded MarshallmallowMonster constructor for the arms, legs, eyes, buttons, and name (use your name for the monster name). You will write and call mutator (setter) methods and accessor (getter) methods. You will also need to define a toString to print out "I created a marshmallow monster named Mrs. Rush who has 2 arms and 2 legs with 2 eyes and 4 buttons."

Make a copy of your Monster (in your tester program) representing the monster created by each person at your table (using their name as the monster name.

Submit your code, a screenshot from the BlueJ terminal window and a selfie with your monster to the assignment in itsLearning. Please update the comments at the top of your files that include your name and the current date.

Download the full Marshmallow Monster lab. Submit your code and a screenshot of the output from the BlueJ terminal window to the itsLearning assignment. Be sure that you have included comments at the top of your files that include your name and the current date.

Assignment: Unit 4 Review Worksheet

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

Review

Quiz created by Shannon Anderson-Rush with GoConqr

Resources

Writing Classes: Anatomy of Classes & Methods presentation

Encapsulation & Visibility Modifiers presentation

Methods presentation