You are on page 1of 6

Study Guide in FM-AA-CIA-15 Rev.

0 03-June-2020

OOP 101 - Object Oriented Programming

Lecture Activity 01 for Module 06

METHODS, CLASSES AND OBJECTS


PLEASE WRITE YOUR NAME HERE ↓
MODULE OVERVIEW
Last Name: _Kyle Edison Name: _Montil______ _______
Course, Year and Section: ___BSIT 2-2_____ 18 + 2
Date: 25/11/2022 Score

Creating Methods
1. Explain the purpose of a method. Define the terms
invoke, method call, calling method and called method,
arguments, parameters, and return type.
Explanation: The invoke () method of Method class Invokes the
underlying method represented by this Method object, on
the specified object with the specified parameters.
Individual parameters automatically to match primitive
formal parameters. Both primitive and reference
parameters are subject to method invocation conversions
as necessary.

2. Define the program statements that must be included in


a method and the components of a method declaration.
Explanation: A method is a block of code or collection of statements or
a set of code grouped together to perform a certain task
or operation. It is used to achieve the re-usability of code.
We write a method once and use it many times. We do
not require to write code again and again. It also provides
the easy modification and readability of code, just by
adding or removing a chunk of code. The method is
executed only when we call or invoke it. The method
declaration provides information about method attributes,
such as visibility, return-type, name, and arguments. It
has six components that are known as method header, as
we have shown in the following figure. There are two
types of methods in Java:Predefined Method and User-
defined Method.

Teaching Tip: Note that method identifiers follow the same naming rules as other Java
identifiers.
1. Review the example code in Figure 6.2, which contains a
static method.
Pictu
re
PANGASINAN STATE UNIVERSITY 617 .. 5
. Pictur
e
618 ...
Study Guide in FM-AA-CIA-15 Rev. 0 03-June-2020

OOP 101 - Object Oriented Programming

Explanation: A static method in Java (also called class method) is a


method that belongs to the class and not the instance.
Therefore, you can invoke the method through the class
instead of creating an instance first and calling the method
on that instance

public class Example {

    public static void printMessage() {

        System.out.println('Message from static


method');

    }

Teaching Tip: Note that method identifiers follow the same naming rules as other Java
identifiers.

1. Describe the placement of methods within a class, using the


code sample in Figure 6.1.
Explanation: A method in a class can run any external procedure using
the standard RUN statement. It can also run an internal
procedure or user-defined function using a procedure
object handle.

2. Explain how to call a method, using the code sample in Figure


6.2.
Explanation: A method must be created in the class with the name of
the method, followed by parentheses ().
The method definition consists of a method header
and method body. We can call a method by using the
following: method_name (); //non static method calling.

Quick Quiz 1:
____Method_____ 1.
A(n) __________ is a program module that contains a series
of statements that carry out a task.
______False_____ 2. True or False: The method declaration is the first line, or
header, of a method.
______A 3. The full name of the nameAndAddress() method is
dot_____ First.nameAndAddress();. The full name includes the class
name, a(n) ____, and the method name.
Pictu
re
PANGASINAN STATE UNIVERSITY 617 .. 6
. Pictur
e
618 ...
Study Guide in FM-AA-CIA-15 Rev. 0 03-June-2020

OOP 101 - Object Oriented Programming

_Encapsulation_ 4. An important principle of object-oriented programming is the


notion of implementation hiding, the
__________________of method details within a class.
____True_____ 5. True or False: You can pass multiple arguments to a method
by listing the arguments within the call to the method and
separating them with colons.

Creating Methods that Return Values


1. Explain the three different events that end method
execution.
Explanation: Use System.exit() method to end the java program
execution. exit() method is from the System class and
exit() is a static method. System.exit() method gives the
instructions to JVM to terminate the current running
program from the point of exit() method. We can
use return statements from the methods or from the main
program to end the current method execution and
continue from its parent.

2. Explain that a method can return a single value or no


value. The data type of value returned by a method is
the method’s return type. Using Figure 6.8, point out
that the method predictRaise() returns a double.
Explanation:
NO VALUE, Unreachable statements (dead code)or Logical
flow leaves method at returnstatementoCan never
execute, Causes compiler error

3. Describe the use of the return statement to pass a value


back to the calling method. Stress that the return type
of the method must be the same as the data type of the
value that is being returned.
Explanation: The return statement is used for returning a value when
the execution of the block is completed.
The return statement inside a loop will cause the loop to
break and further statements will be ignored by the
compiler. These return types required a return statement
at the end of the method. A return keyword is used for
returning the resulted value. The void return type doesn't
require any return statement. If we try to return a value
from a void method, the compiler shows an error.

Pictu
re
PANGASINAN STATE UNIVERSITY 617 .. 7
. Pictur
e
618 ...
Study Guide in FM-AA-CIA-15 Rev. 0 03-June-2020

OOP 101 - Object Oriented Programming

Teaching Tip: If a method does not have a return type (declared using the void
keyword), the return statement can be used followed by a semicolon to return program
execution to the calling method.

Chaining Method Calls


1. Note that one method can call another method, as
illustrated in Figure 6.9.
Explanation: CalculateBonus() method

Learning about Class Concepts


1. Describe is-a relationships. Explain that an instance is a
member of a class. Use some examples from everyday
life to help explain this concept.

Explanation: An Is-A relationship depends on inheritance. Further


inheritance is of two types, class inheritance and interface
inheritance. It is used for code reusability in Java. For
example, a Potato is a vegetable, a Bus is a vehicle, a
Bulb is an electronic device and so on. One of the
properties of inheritance is that inheritance is
unidirectional in nature. Like we can say that a house is a
building. But not all buildings are houses. We can easily
determine an Is-A relationship in Java. When there is an
extends or implement keyword in the class declaration in
Java, then the specific class is said to be following the Is-
A relationship.

2. Define the term instantiation to mean an object of a


class.
Explanation: Instantiation is a computer programming term, used by
developers, that is usually related to Object-Oriented
Programming (OOP). At its core, instantiation is the
process of creating an Instance of an Object. Objects are
structures created by Classes, which are essentially
blueprints or templates that describe a set of features that
will belong to Objects created from them. These “features”
include Properties also known as variables and Methods.

3.
Discuss the benefits of using classes, including
Pictu
re
PANGASINAN STATE UNIVERSITY 617 .. 8
. Pictur
e
618 ...
Study Guide in FM-AA-CIA-15 Rev. 0 03-June-2020

OOP 101 - Object Oriented Programming

reusability.
Explanation: Java programmers concentrate on crafting new classes
and reusing existing classes. Many class libraries exist,
and others are being developed worldwide. Software is
then constructed from existing, well-defined, carefully
tested, well-documented, portable, widely available
components. This kind of software reusability speeds the
development of powerful, high-quality software. Rapid
application development (RAD) is of great interest today.
Java programmers now have thousands of classes in the
Java API from which to choose to help them implement
Java programs. Indeed, Java is not just a programming
language. It is a framework in which Java developers can
work to achieve true reusability and rapid application
development. Java programmers can focus on the task at
hand when developing their programs and leave the
lower-level details to the classes of the Java API.

4. Describe various uses of class methods to provide access to


data fields and to provide other behaviors for the class. Explain
that Java applications are often composed of many classes,
some of which may not have been written by the programmer
of the application class.
Explanation: A class is a user defined blueprint or prototype from which
objects are created. It represents the set of properties or methods
that are common to all objects of one type. In general, class
declarations can include these components, in order: 
1. Modifiers: A class can be public or has default access
(Refer this for details).
2. Class keyword: class keyword is used to create a class.
3. Class name: The name should begin with an initial letter
(capitalized by convention).
4. Superclass(if any): The name of the class’s parent
(superclass), if any, preceded by the keyword extends. A
class can only extend (subclass) one parent.
5. Interfaces(if any): A comma-separated list of interfaces
implemented by the class, if any, preceded by the keyword
implements. A class can implement more than one
interface.
6. Body: The class body is surrounded by braces, { }.

Constructors are used for initializing new objects. Fields


are variables that provide the state of the class and its objects,
and methods are used to implement the behavior of the class
and its objects. There are various types of classes that are used
in real time applications such as nested classes, anonymous
classes, lambda expressions. It is a basic unit of Object-Oriented
Programming and represents real life entities. A typical Java
program creates many objects, which as you know, interact by
invoking methods. An object consists of : 
Pictu
re
PANGASINAN STATE UNIVERSITY 617 .. 9
. Pictur
e
618 ...
Study Guide in FM-AA-CIA-15 Rev. 0 03-June-2020

OOP 101 - Object Oriented Programming

 State: It is represented by attributes of an object. It also


reflects the properties of an object.
 Behavior: It is represented by methods of an object. It also
reflects the response of an object with other objects.
 Identity: It gives a unique name to an object and enables
one object to interact with other objects.

Teaching Tip: The Java language provides a large number of built-in classes that are
used to create programs. There are also open-source projects, such as Jakarta Commons,
that provide other prewritten classes that can be used within programs. When writing a
program, it is a good idea to see whether a prewritten class contains functionality that
can be used rather than “reinventing the wheel.”

For easy tracking of your file/s strictly follow the correct filename format given below.
Filename: Output Checking:
LastnameFirstNameMI_M6_Act1.docx none
e.g. DelacruzJuanL_M6_Act1.docx

How to submit? Deadline:


Send it individually to your instructor via MS Teams. December 3, 2022, till 5:00 PM
IMPORTANT! Late submission of paper/s will not be accepted, regardless of the reason(s). Don’t forget
to record the date of submission in the Google Sheet link posted in the group chat on Facebook
Messenger.

PANGASINAN STATE UNIVERSITY 10

You might also like