AY 2021-2022
COMPUTER
PROGRAMMING 2
Prepared By:
Prof. Jomariss B. Plan, MSIT
CCS Faculty
WEEK 5 SESSION 2:
OBJECTS AND CLASSES-
PART 2
Learning Outcomes:
At the end of the topic session, the students should be
able to:
1. Understand the accessor and mutator methods.
2. Differentiate method overloading and
overloading constructors.
3. Apply the dot notation and this keyword.
4. Apply methods in programming.
public class Employee {
private int number;
public int getNumber () {
return number;
}
public void setNumber (int newNumber) {
number= newNumber;
}
}
class Check {
private int amount=0;
public int getAmount( ) {
return amount;
}
public void setAmount (int amt) {
amount = amt;
}}
public class MainClass {
public static void main (String args[ ]) {
int amt=0;
Check obj = new Check( );
[Link](200);
amt = [Link]( );
[Link]("Your current amount is : " +amt); } }
DEMONSTRATION
Accessor and Mutator
[Link]
ehGg4aVJD9M
Static Methods
[Link]
1rdbEUh3R_o
class MethodOverloading {
public static void main(String args[]){
[Link]("Result: " + aMethod(13, 2.0));
}
public static double aMethod (int a, int b){
return a*b;
}
public static double aMethod (int a, double b){
return a+b;
}
public static double aMethod (double a, int b){
return a-b;
}
public static double aMethod (double a, double b){
return a/b;
}}
public class UberLoading {
public static void main (String []args) {
UberLoading2 me= new UberLoading2();
[Link]('a');
[Link](10, 'a');
[Link](5);
}
}
public class UberLoading2 {
public void display (int a) {
[Link]("Printed: " + a);
}
public void display(char c) {
[Link]("Printed: " + c);
}
public void display (int a, char c) {
[Link]("Both Printed: " + a + " " + c);
}
}
DEMONSTRATION
Method Overloading
[Link]
?v=_iz6cjXlJfw
A constructor in Java is a special
method that is used to initialize
objects. The constructor is called
when an object of a class is created.
It can be used to set initial values for
object attributes.
public class MyClass2 {
int x;
public MyClass2() {
x = 5;
}
public static void main(String[] args) {
MyClass2 myObj = new MyClass2();
[Link](myObj.x);
}
}
Constructors can also take parameters,
which is used to initialize attributes.
public class MyClass {
int x;
public MyClass(int y) {
x = y; }
public static void main(String[] args) {
MyClass myObj = new MyClass(5);
[Link](myObj.x);
}}
public class Car2 {
int modelYear;
String modelName;
public Car2(int year, String name) {
modelYear = year;
modelName = name;
}
public static void main(String[] args) {
Car2 myCar = new Car2(1969, "Mustang");
[Link]([Link] + " " +
[Link]);
}}
Human person = new Human("Lucy");
CoolDog coolDog = new CoolDog("Buddy", 4, 30);
[Link](coolDog);
For example, the Point class was written like this
public class Point {
public int x = 0;
public int y = 0;
public Point(int a, int b) {
x = a;
y = b;
}
}
but it could have been written like this:
public class Point {
public int x = 0;
public int y = 0;
public Point(int x, int y) {
this.x = x;
this.y = y;
}}
To refer to the Point field x, the constructor must use this.x.
public class MyClass2 {
int x;
public MyClass2 (int x) {
this.x = x;
}
public static void main(String[] args) {
MyClass2 myObj = new MyClass2 (5);
[Link]("Value of x = " + myObj.x);
}
}
public class MyClass2 {
int x;
int y;
public MyClass2 (int y) {
this.x = y;
}
public static void main(String[] args) {
MyClass2 myObj = new MyClass2(5);
[Link]("Value of y = " + myObj.y);
[Link]("Value of x = " + myObj.x);
}
}
DEMONSTRATION
This Keyword
[Link]
?v=csjfLTt6-io
That ends our Week 5 Session 2!
NEXT MEETING--
WEEK 6 SESSION 1:
INHERITANCE, POLYMORPHISM,
AND ENCAPSULATION
REFERENCES:
Java Programming 6th edition by
Joyce Farrell
An Introduction to OOP with Java by
C. Thomas Wu
thenewboston (link:
[Link]
CJbPGzawDH1njbqV-D5HqKw)