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 | Resources
Recall that is a class is a blueprint of an object. It has no memory space for data. Each object has its own data space.
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 runner program will not necessarily use every service provided by an object. For example, the CountFlips program did not use the toString method of the coin class. Once an object class has been defined, we can use it again in other programs as needed.
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.
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.
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.
Data encapsulation is the act of hiding the values of the instance variables from other classes.
In general you want to hide an object's state so nobody else can abuse it or make changes that unknowingly affect other copies of the object. Hiding the state is called encapsulation and is done with the use of the private visibility modifier. Declaring instance variables as public allows instance access to those variables using the dot operator.
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 (runner) as directed below.
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.
A return statement is the reserved word return followed by an optional expression. After the statement executes, control is immediately returned to the calling method, returning the value defined by the expression.
It is possible to have more than one method with the same name in the same program. Methods that have the same name but different parameters lists are called overloaded methods (similar to multiple versions of the same method). 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.
Overloaded methods must have the same name and may have a different return type; however, they must differ by one of these ways:
You can read more about method overloading and see specific examples in Beginners Book - Method Overloading in Java with Examples.
Other objects and methods can set and/or get private variables by writing a method to do it. A mutator method is a method used to control changes to a variable. They are also widely known as setter methods. Often a setter is accompanied by a getter (also known as an accessor), which returns the value of the private member variable.
The mutator method is most often used in object-oriented programming, in keeping with the principle of encapsulation. According to this principle, member variables of a class are made private to hide and protect them from other code, and can only be modified by a public member function (the mutator method), which takes the desired new value as a parameter and modifies the private member variable. Mutator methods can be compared to assignment operator overloading but they typically appear at different levels of the object hierarchy. Wikipedia.com
Directions: Identify the parts of a method in the itsLearning worksheet.
Directions: Review the parts of a method and be prepared to take the quiz as assigned by your instructor.
Directions: Complete the Writing Methods in Java worksheet in itsLearning.
Directions: Open the Exam class and answer the questions regarding the class in the worksheet in itsLearning.
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.
Directions: Read the short article on The Programming Code of Ethics: https://www.gammadyne.com/ethics.htm. Select the one ethic that you think is most important and explain why. Respond to two classmates who chose a different standard and elaborate on their point.
Directions: Complete the Unit 4 review worksheet in itsLearning to review for the unit assessment.
To prepare for the unit assessment, you should utilize the stude guide.Quiz created by Shannon Anderson-Rush with GoConqr
Writing Classes: Anatomy of Classes & Methods presentation
Encapsulation & Visibility Modifiers presentation
Setter & Getter Example: https://en.wikipedia.org/wiki/Mutator_method#Java_example