Object and Class 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

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( );
obj.setAmount(200);
amt = obj.getAmount( );
System.out.println("Your current amount is : " +amt); } }
DEMONSTRATION

Accessor and Mutator


https://www.youtube.com/watch?v=
ehGg4aVJD9M

Static Methods
https://www.youtube.com/watch?v=
1rdbEUh3R_o
class MethodOverloading {
public static void main(String args[]){
System.out.println("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();

me.display('a');
me.display(10, 'a');
me.display(5);
}
}
public class UberLoading2 {
public void display (int a) {
System.out.println("Printed: " + a);
}
public void display(char c) {
System.out.println("Printed: " + c);
}
public void display (int a, char c) {
System.out.println("Both Printed: " + a + " " + c);
}
}
DEMONSTRATION

Method Overloading

https://www.youtube.com/watch
?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();
System.out.println(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);
System.out.println(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");
System.out.println(myCar.modelYear + " " +
myCar.modelName);
}}
Human person = new Human("Lucy");
CoolDog coolDog = new CoolDog("Buddy", 4, 30);
person.setPetDog(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);
System.out.println("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);
System.out.println("Value of y = " + myObj.y);
System.out.println("Value of x = " + myObj.x);
}
}
DEMONSTRATION

This Keyword

https://www.youtube.com/watch
?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:
https://www.youtube.com/channel/U
CJbPGzawDH1njbqV-D5HqKw)

You might also like