Binalatongan Community College
Brgy. Ilang San Carlos City, Pangasinan
Bachelor of Science in Information Technology (BSIT)
LEARNING MODULE
Module No. 9
Subject Code : IT 211
Subject Description : Object Oriented Programming
Term : 1st Semester 2022-2023
I. Learning Objectives:
To have a complete knowledge in Java programming using class
methods
II. Learning Outcome:
- Learn and apply the uses of class methods in Java programming.
III. Learning Resources/Reference:
- Concise Guide to Object Oriented Programming, Kingsley Sage,
2019
- The Object-Oriented Thought Process Fifth Edition, Matt Weisfeld,
2019
IV. Tasks to Complete:
- Read the Java class methods and static and non-static
- Learn the
- Answer the questions in self-assessment.
V. Content:
Java Class Methods
You learned from the Java Methods chapter that methods are declared
within a class, and that they are used to perform certain actions:
Example
Create a method named myMethod() in Main:
public class Main {
static void myMethod() {
[Link]("Hello World!");
}
}
myMethod() prints a text (the action), when it is called. To call a method, write
the method's name followed by two parentheses () and a semicolon;
Example
Inside main, call myMethod():
public class Main {
static void myMethod() {
[Link]("Hello World!");
}
public static void main(String[] args) {
myMethod();
}
}
// Outputs "Information Technology"
Static vs. Non-Static
You will often see Java programs that have either static or public attributes
and methods.
In the example above, we created a static method, which means that it can be
accessed without creating an object of the class, unlike public, which can only
be accessed by objects:
Example
An example to demonstrate the differences
between static and public methods:
public class Main {
// Static method
static void myStaticMethod() {
[Link]("Static methods can be called without
creating objects");
}
// Public method
public void myPublicMethod() {
[Link]("Public methods must be called by creating
objects");
}
// Main method
public static void main(String[] args) {
myStaticMethod(); // Call the static method
// myPublicMethod(); This would compile an error
Main myObj = new Main(); // Create an object of Main
[Link](); // Call the public method on the object
}
}
Access Methods With an Object
Example
Create a Car object named myCar. Call
the fullThrottle() and speed() methods on the myCar object, and run the
program:
// Create a Main class
public class Main {
// Create a fullThrottle() method
public void fullThrottle() {
[Link]("The car is going as fast as it can!");
}
// Create a speed() method and add a parameter
public void speed(int maxSpeed) {
[Link]("Max speed is: " + maxSpeed);
}
// Inside main, call the methods on the myCar object
public static void main(String[] args) {
Main myCar = new Main(); // Create a myCar object
[Link](); // Call the fullThrottle() method
[Link](200); // Call the speed() method
}
}
// The car is going as fast as it can!
// Max speed is: 200
Example explained
1) We created a custom Main class with the class keyword.
2) We created the fullThrottle() and speed() methods in the Main class.
3) The fullThrottle() method and the speed() method will print out some
text, when they are called.
4) The speed() method accepts an int parameter called maxSpeed - we will
use this in 8).
5) In order to use the Main class and its methods, we need to create
an object of the Main Class.
6) Then, go to the main() method, which you know by now is a built-in Java
method that runs your program (any code inside main is executed).
7) By using the new keyword we created an object with the name myCar.
8) Then, we call the fullThrottle() and speed() methods on
the myCar object, and run the program using the name of the object ( myCar),
followed by a dot (.), followed by the name of the method
(fullThrottle(); and speed(200);). Notice that we add an int parameter
of 200 inside the speed() method.
Think about this:
The dot (.) is used to access the object's attributes and methods.
To call a method in Java, write the method name followed by a set of
parentheses (), followed by a semicolon (;).
A class must have a matching filename (Main and [Link]).
Using Multiple Classes
Like we specified in the Classes chapter, it is a good practice to create an object
of a class and access it in another class.
Remember that the name of the java file should match the class name. In this
example, we have created two files in the same directory:
[Link]
[Link]
[Link]
public class Main {
public void fullThrottle() {
[Link]("The car is going as fast as it can!");
}
public void speed(int maxSpeed) {
[Link]("Max speed is: " + maxSpeed);
}
}
[Link]
class Second {
public static void main(String[] args) {
Main myCar = new Main(); // Create a myCar object
[Link](); // Call the fullThrottle() method
[Link](200); // Call the speed() method
}
}
VI. Assessment: Using your smart phone – Java Application
- Make a Java program that will accept two numbers and determine
the number Odd and Even numbers entered.