SlideShare a Scribd company logo
1 of 9
Download to read offline
Object-oriented programming
Monday, October 7, 13
2
Objects
• An object is a programming entity that contains state and
behavior.
• The state is the set of values stored in an object.
• The behavior is the set of actions an object can perform, often
reporting or modifying its internal state.
state behavior
int minute
int hour
boolean shouldAlarm
int alarmMinute
int alarmHour
setTime(...)
toggleAlarm(...)
Monday, October 7, 13
3
Classes and objects
• You already know how to create classes:
public class MyProgram {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
• Classes can also create new types, similar to String or Math
Monday, October 7, 13
4
Point
• Let’s create a Point object (from java.awt.Point)
Point p = new Point(3, 8);
• The Point stores its state; int values for x, y
• The Point also has behavior:
method description
translate(dx, dy) Translates the coordinates by the given amounts
setLocation(x, y) Sets the coordinates to the given values
distance(p2) Returns the distance from this point to p2
Monday, October 7, 13
5
Point
import java.awt.*;
public class PointExample1 {
public static void main(String[] args) {
Point p = new Point(3, 8);
System.out.println("initially p = " + p);
p.translate(-1, -2);
System.out.println("after translating p = " + p);
int sum = p.x + p.y;
System.out.println("sum of coordinates = " + sum);
}
}
• The output:
initially p = java.awt.Point[x=3,y=8]
after translating p = java.awt.Point[x=2,y=6]
sum of coordinates = 8
Monday, October 7, 13
6
Point
public class Point {
int x;
int y;
}
• Note there’s no main method; this class is not executable
public class PointMain {
public static void main(String[] args) {
Point p1 = new Point();
p1.x = 7;
p1.y = 2;
System.out.println("p1 is (" + p1.x + ", " + p1.y + ")");
}
}
Monday, October 7, 13
7
Point
• Write a method that translates the coordinates of a point:
public static void translate(Point p, int dx, int dy) {
p.x = dx;
p.y = dy;
}
• static is a bad choice here; we want objects to own their
behavior
Monday, October 7, 13
8
Point
public class Point {
int x;
int y;
public void translate(int dx, int dy) {
x += dx;
y += dy;
}
}
• Note we’ve dropped static! This is a method that affects an
object (remember, it gives the object behavior)
Monday, October 7, 13
9
Lab!
• See https://dl.dropboxusercontent.com/u/20418505/Labs/
M2.W2-0.txt
Monday, October 7, 13

More Related Content

What's hot

CS-141 Java programming II ASSIGNMENT 2
CS-141 Java programming II ASSIGNMENT 2CS-141 Java programming II ASSIGNMENT 2
CS-141 Java programming II ASSIGNMENT 2Voffelarin
 
Talk on Standard Template Library
Talk on Standard Template LibraryTalk on Standard Template Library
Talk on Standard Template LibraryAnirudh Raja
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnArnaud Joly
 
Constructor and Destructor PPT
Constructor and Destructor PPTConstructor and Destructor PPT
Constructor and Destructor PPTShubham Mondal
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
Lecture 3.1 to 3.2 bt
Lecture 3.1 to 3.2 btLecture 3.1 to 3.2 bt
Lecture 3.1 to 3.2 btbtmathematics
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structureeShikshak
 
constructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objectsconstructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objectsKanhaiya Saxena
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Sameer Rathoud
 

What's hot (20)

CS-141 Java programming II ASSIGNMENT 2
CS-141 Java programming II ASSIGNMENT 2CS-141 Java programming II ASSIGNMENT 2
CS-141 Java programming II ASSIGNMENT 2
 
TeraSort
TeraSortTeraSort
TeraSort
 
Task and Data Parallelism
Task and Data ParallelismTask and Data Parallelism
Task and Data Parallelism
 
Talk on Standard Template Library
Talk on Standard Template LibraryTalk on Standard Template Library
Talk on Standard Template Library
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
 
Javantura v6 - Kotlin-Java Interop - Matej Vidaković
Javantura v6 - Kotlin-Java Interop - Matej VidakovićJavantura v6 - Kotlin-Java Interop - Matej Vidaković
Javantura v6 - Kotlin-Java Interop - Matej Vidaković
 
Constructor and Destructor PPT
Constructor and Destructor PPTConstructor and Destructor PPT
Constructor and Destructor PPT
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Queues
QueuesQueues
Queues
 
123
123123
123
 
Lecture 3.1 to 3.2 bt
Lecture 3.1 to 3.2 btLecture 3.1 to 3.2 bt
Lecture 3.1 to 3.2 bt
 
Queue
QueueQueue
Queue
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Queue
QueueQueue
Queue
 
constructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objectsconstructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objects
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())
 
A taste of Functional Programming
A taste of Functional ProgrammingA taste of Functional Programming
A taste of Functional Programming
 
basic concepts
basic conceptsbasic concepts
basic concepts
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 

Viewers also liked

2 m3.w1.d2 - interfaces
2   m3.w1.d2 - interfaces2   m3.w1.d2 - interfaces
2 m3.w1.d2 - interfacesJustin Chen
 
2 m2.w3.d1 - encapsulation
2   m2.w3.d1 - encapsulation2   m2.w3.d1 - encapsulation
2 m2.w3.d1 - encapsulationJustin Chen
 
2 m2.w5.d1 - superclass
2   m2.w5.d1 - superclass2   m2.w5.d1 - superclass
2 m2.w5.d1 - superclassJustin Chen
 
Subir prof tapia 2
Subir prof tapia 2Subir prof tapia 2
Subir prof tapia 2Karla Z
 
m1.w4.d2 - parameters
m1.w4.d2 - parametersm1.w4.d2 - parameters
m1.w4.d2 - parametersJustin Chen
 
Architecting for Enterprise with JavaScript
Architecting for Enterprise with JavaScriptArchitecting for Enterprise with JavaScript
Architecting for Enterprise with JavaScriptKurtis Kemple
 
Luas dan volum tabung
Luas dan volum tabungLuas dan volum tabung
Luas dan volum tabungrenatrisea
 
2 m2.w4.d1 - inheritance
2   m2.w4.d1 - inheritance2   m2.w4.d1 - inheritance
2 m2.w4.d1 - inheritanceJustin Chen
 
bahan ajar materi bilangan bulat kelas 7
bahan ajar materi bilangan bulat kelas 7bahan ajar materi bilangan bulat kelas 7
bahan ajar materi bilangan bulat kelas 7renatrisea
 

Viewers also liked (12)

2 m3.w1.d2 - interfaces
2   m3.w1.d2 - interfaces2   m3.w1.d2 - interfaces
2 m3.w1.d2 - interfaces
 
2 m2.w3.d1 - encapsulation
2   m2.w3.d1 - encapsulation2   m2.w3.d1 - encapsulation
2 m2.w3.d1 - encapsulation
 
A success-to-be-shared
A success-to-be-sharedA success-to-be-shared
A success-to-be-shared
 
2 m2.w5.d1 - superclass
2   m2.w5.d1 - superclass2   m2.w5.d1 - superclass
2 m2.w5.d1 - superclass
 
Subir prof tapia 2
Subir prof tapia 2Subir prof tapia 2
Subir prof tapia 2
 
m1.w4.d2 - parameters
m1.w4.d2 - parametersm1.w4.d2 - parameters
m1.w4.d2 - parameters
 
Adán y Eva un matrimonio ejemplar
Adán y Eva un matrimonio ejemplarAdán y Eva un matrimonio ejemplar
Adán y Eva un matrimonio ejemplar
 
Architecting for Enterprise with JavaScript
Architecting for Enterprise with JavaScriptArchitecting for Enterprise with JavaScript
Architecting for Enterprise with JavaScript
 
Luas dan volum tabung
Luas dan volum tabungLuas dan volum tabung
Luas dan volum tabung
 
EL CRISTIANO Y LAS AFLICCIONES
EL CRISTIANO Y LAS AFLICCIONESEL CRISTIANO Y LAS AFLICCIONES
EL CRISTIANO Y LAS AFLICCIONES
 
2 m2.w4.d1 - inheritance
2   m2.w4.d1 - inheritance2   m2.w4.d1 - inheritance
2 m2.w4.d1 - inheritance
 
bahan ajar materi bilangan bulat kelas 7
bahan ajar materi bilangan bulat kelas 7bahan ajar materi bilangan bulat kelas 7
bahan ajar materi bilangan bulat kelas 7
 

Similar to Object-oriented programming concepts explained with Point class example

Scalable JavaScript
Scalable JavaScriptScalable JavaScript
Scalable JavaScriptYnon Perek
 
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)PROIDEA
 
08-classes-objects.ppt
08-classes-objects.ppt08-classes-objects.ppt
08-classes-objects.pptssuser419267
 
08-classes-objects.ppt
08-classes-objects.ppt08-classes-objects.ppt
08-classes-objects.pptUmooraMinhaji
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8franciscoortin
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressionsLogan Chien
 
Please help with this JAVA Assignment and show output if you can ple.pdf
Please help with this JAVA Assignment and show output if you can ple.pdfPlease help with this JAVA Assignment and show output if you can ple.pdf
Please help with this JAVA Assignment and show output if you can ple.pdfaroramobiles1
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxbradburgess22840
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsMuhammadTalha436
 
Java Programs
Java ProgramsJava Programs
Java Programsvvpadhu
 
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Codemotion
 
Chap2 class,objects contd
Chap2 class,objects contdChap2 class,objects contd
Chap2 class,objects contdraksharao
 
Software Transactioneel Geheugen
Software Transactioneel GeheugenSoftware Transactioneel Geheugen
Software Transactioneel GeheugenDevnology
 

Similar to Object-oriented programming concepts explained with Point class example (20)

00-review.ppt
00-review.ppt00-review.ppt
00-review.ppt
 
Scalable JavaScript
Scalable JavaScriptScalable JavaScript
Scalable JavaScript
 
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
 
Class and object C++.pptx
Class and object C++.pptxClass and object C++.pptx
Class and object C++.pptx
 
OOC in python.ppt
OOC in python.pptOOC in python.ppt
OOC in python.ppt
 
08-classes-objects.ppt
08-classes-objects.ppt08-classes-objects.ppt
08-classes-objects.ppt
 
08-classes-objects.ppt
08-classes-objects.ppt08-classes-objects.ppt
08-classes-objects.ppt
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressions
 
Please help with this JAVA Assignment and show output if you can ple.pdf
Please help with this JAVA Assignment and show output if you can ple.pdfPlease help with this JAVA Assignment and show output if you can ple.pdf
Please help with this JAVA Assignment and show output if you can ple.pdf
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
Web futures
Web futuresWeb futures
Web futures
 
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
 
Chap2 class,objects contd
Chap2 class,objects contdChap2 class,objects contd
Chap2 class,objects contd
 
Python tour
Python tourPython tour
Python tour
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
Software Transactioneel Geheugen
Software Transactioneel GeheugenSoftware Transactioneel Geheugen
Software Transactioneel Geheugen
 
Lecture3.pdf
Lecture3.pdfLecture3.pdf
Lecture3.pdf
 

Recently uploaded

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 

Recently uploaded (20)

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 

Object-oriented programming concepts explained with Point class example

  • 2. 2 Objects • An object is a programming entity that contains state and behavior. • The state is the set of values stored in an object. • The behavior is the set of actions an object can perform, often reporting or modifying its internal state. state behavior int minute int hour boolean shouldAlarm int alarmMinute int alarmHour setTime(...) toggleAlarm(...) Monday, October 7, 13
  • 3. 3 Classes and objects • You already know how to create classes: public class MyProgram { public static void main(String[] args) { System.out.println("Hello world"); } } • Classes can also create new types, similar to String or Math Monday, October 7, 13
  • 4. 4 Point • Let’s create a Point object (from java.awt.Point) Point p = new Point(3, 8); • The Point stores its state; int values for x, y • The Point also has behavior: method description translate(dx, dy) Translates the coordinates by the given amounts setLocation(x, y) Sets the coordinates to the given values distance(p2) Returns the distance from this point to p2 Monday, October 7, 13
  • 5. 5 Point import java.awt.*; public class PointExample1 { public static void main(String[] args) { Point p = new Point(3, 8); System.out.println("initially p = " + p); p.translate(-1, -2); System.out.println("after translating p = " + p); int sum = p.x + p.y; System.out.println("sum of coordinates = " + sum); } } • The output: initially p = java.awt.Point[x=3,y=8] after translating p = java.awt.Point[x=2,y=6] sum of coordinates = 8 Monday, October 7, 13
  • 6. 6 Point public class Point { int x; int y; } • Note there’s no main method; this class is not executable public class PointMain { public static void main(String[] args) { Point p1 = new Point(); p1.x = 7; p1.y = 2; System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); } } Monday, October 7, 13
  • 7. 7 Point • Write a method that translates the coordinates of a point: public static void translate(Point p, int dx, int dy) { p.x = dx; p.y = dy; } • static is a bad choice here; we want objects to own their behavior Monday, October 7, 13
  • 8. 8 Point public class Point { int x; int y; public void translate(int dx, int dy) { x += dx; y += dy; } } • Note we’ve dropped static! This is a method that affects an object (remember, it gives the object behavior) Monday, October 7, 13