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 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.
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.
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.
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.
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. 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 them using the dot operator.
Other objects and methods can set and/or get private variables by writing a method to do it (more information on methods in the next section). 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
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.
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. This is known as an overloaded method. Overloaded methods must have the same name and may have a different return type; however, they must differ by one of these ways:
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: 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).
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.
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.
Writing Classes: Anatomy of Classes & Methods presentation
Encapsulation & Visibility Modifiers presentation
Setter & Getter Example: https://en.wikipedia.org/wiki/Mutator_method#Java_example