SlideShare uma empresa Scribd logo
1 de 10
Lecture 1
Object Oriented Programming




                Object Oriented Programming
                Eastern University, Dhaka
                        Md. Raihan Kibria
Why object oriented programming

Consider the following program written in C:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

struct cgpa{
    int grades[3];
}ca;

float getCgpa1(struct cgpa cgpa){
    float sum = 0;
    int i = 0;
    for (i=0;i<3; i++){
         sum += cgpa.grades[i];
    }
    return sum/3;
}
float getCgpa2(struct cgpa cgpa){
      float sum = 0;
      int i = 0;
      for (i=0;i<3; i++){
             sum += cgpa.grades[i];
      }
      return roundf(sum/3);
}
float getCgpa3(struct cgpa cgpa){
      float sum = 0;
      int i = 0;
      for (i=0;i<3; i++){
             sum += cgpa.grades[i];
      }
      return abs(sum/3);
}


int main(){
    printf ("%s", "a message to startn");
    ca.grades[0] = 3;
    ca.grades[1] = 5;
    ca.grades[2] = 6;
    printf("%fn", getCgpa1(ca));
    printf("%fn", getCgpa2(ca));
    printf("%fn", getCgpa3(ca));
}
OOP goals
   Maximize code re-use
   Minimize re-coding
Same program in java
public class Grade {

public static void main(String[] args) {
    System.out.println(new Grade1().getCgpa());
    System.out.println(new Grade2().getCgpa());
    System.out.println(new Grade3().getCgpa());
  }
}

class Grade1{
  int[] grades = {3,5,6};
  float sum;

    protected void calculateSum(){
      for (int i=0; i<grades.length; i++)
        sum += grades[i];
    }

    public float getCgpa(){
       calculateSum();
      return sum / grades.length;
    }
}

class Grade2 extends Grade1{
   public float getCgpa(){
    calculateSum();
    return Math.round(sum / grades.length);
  }
}

class Grade3 extends Grade1{
  public float getCgpa(){
    calculateSum();
    return (float)Math.floor(sum / grades.length   );
  }
}
How to start programming in OOP
 Class
A class is a template

//a student
public class ClassDemo {
   String code;
   String name;
}
Entry point
public static void main(String[] args) {}
e.g.
//a student
public class ClassDemo {
    String code;
    String name;
    public static void main(String[] args){
         ClassDemo demo = new ClassDemo();
         demo.code = "1221232323";
         demo.name = "Raihan Kibria";
         System.out.println(demo.code);
         System.out.println(demo.name);
   }
}
How to compile

Install jdk

Compile:
javac ClassDemo.java


How to run:
java ClassDemo

Mais conteúdo relacionado

Mais procurados

SHA1 collision analysis and resolving a problem of recursive hashing with xra...
SHA1 collision analysis and resolving a problem of recursive hashing with xra...SHA1 collision analysis and resolving a problem of recursive hashing with xra...
SHA1 collision analysis and resolving a problem of recursive hashing with xra...Diego Hernan Marciano
 
halstead software science measures
halstead software science measureshalstead software science measures
halstead software science measuresDeepti Pillai
 
Lessons learned from functional programming
Lessons learned from functional programmingLessons learned from functional programming
Lessons learned from functional programmingBryceLohr
 
PyCon Ukraine 2016: Maintaining a high load Python project for newcomers
PyCon Ukraine 2016: Maintaining a high load Python project for newcomersPyCon Ukraine 2016: Maintaining a high load Python project for newcomers
PyCon Ukraine 2016: Maintaining a high load Python project for newcomersViach Kakovskyi
 
Program persamaan kuadrat
Program persamaan kuadratProgram persamaan kuadrat
Program persamaan kuadratlinda_rosalina
 
Speaker Diarization
Speaker DiarizationSpeaker Diarization
Speaker DiarizationHONGJOO LEE
 
Tail Recursion in data structure
Tail Recursion in data structureTail Recursion in data structure
Tail Recursion in data structureRumman Ansari
 
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORYCS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORYRadha Maruthiyan
 
Learn C# Programming Polymorphism & Operator Overloading
Learn C# Programming Polymorphism & Operator OverloadingLearn C# Programming Polymorphism & Operator Overloading
Learn C# Programming Polymorphism & Operator OverloadingEng Teong Cheah
 
Removal Of Recursion
Removal Of RecursionRemoval Of Recursion
Removal Of RecursionRicha Sharma
 
Cilk - An Efficient Multithreaded Runtime System
Cilk - An Efficient Multithreaded Runtime SystemCilk - An Efficient Multithreaded Runtime System
Cilk - An Efficient Multithreaded Runtime SystemShareek Ahamed
 
CS106 Lab 11 - Functions (passing by reference)
CS106 Lab 11 - Functions (passing by reference)CS106 Lab 11 - Functions (passing by reference)
CS106 Lab 11 - Functions (passing by reference)Nada Kamel
 

Mais procurados (20)

C lab excellent
C lab excellentC lab excellent
C lab excellent
 
Periodic test
Periodic testPeriodic test
Periodic test
 
CS2106 Tutorial 2
CS2106 Tutorial 2CS2106 Tutorial 2
CS2106 Tutorial 2
 
C# Overriding
C# OverridingC# Overriding
C# Overriding
 
Pslb lab manual
Pslb lab manualPslb lab manual
Pslb lab manual
 
SHA1 collision analysis and resolving a problem of recursive hashing with xra...
SHA1 collision analysis and resolving a problem of recursive hashing with xra...SHA1 collision analysis and resolving a problem of recursive hashing with xra...
SHA1 collision analysis and resolving a problem of recursive hashing with xra...
 
halstead software science measures
halstead software science measureshalstead software science measures
halstead software science measures
 
Lessons learned from functional programming
Lessons learned from functional programmingLessons learned from functional programming
Lessons learned from functional programming
 
PyCon Ukraine 2016: Maintaining a high load Python project for newcomers
PyCon Ukraine 2016: Maintaining a high load Python project for newcomersPyCon Ukraine 2016: Maintaining a high load Python project for newcomers
PyCon Ukraine 2016: Maintaining a high load Python project for newcomers
 
Program persamaan kuadrat
Program persamaan kuadratProgram persamaan kuadrat
Program persamaan kuadrat
 
Speaker Diarization
Speaker DiarizationSpeaker Diarization
Speaker Diarization
 
Tail Recursion in data structure
Tail Recursion in data structureTail Recursion in data structure
Tail Recursion in data structure
 
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORYCS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
 
Learn C# Programming Polymorphism & Operator Overloading
Learn C# Programming Polymorphism & Operator OverloadingLearn C# Programming Polymorphism & Operator Overloading
Learn C# Programming Polymorphism & Operator Overloading
 
Analysis of algo
Analysis of algoAnalysis of algo
Analysis of algo
 
Removal Of Recursion
Removal Of RecursionRemoval Of Recursion
Removal Of Recursion
 
Cilk - An Efficient Multithreaded Runtime System
Cilk - An Efficient Multithreaded Runtime SystemCilk - An Efficient Multithreaded Runtime System
Cilk - An Efficient Multithreaded Runtime System
 
Rcpp11 useR2014
Rcpp11 useR2014Rcpp11 useR2014
Rcpp11 useR2014
 
Ankita sharma focp
Ankita sharma focpAnkita sharma focp
Ankita sharma focp
 
CS106 Lab 11 - Functions (passing by reference)
CS106 Lab 11 - Functions (passing by reference)CS106 Lab 11 - Functions (passing by reference)
CS106 Lab 11 - Functions (passing by reference)
 

Destaque (9)

Oop lecture2
Oop lecture2Oop lecture2
Oop lecture2
 
Presentacion viernes 20 [compatibility mode]
Presentacion viernes 20 [compatibility mode]Presentacion viernes 20 [compatibility mode]
Presentacion viernes 20 [compatibility mode]
 
Oop lecture6
Oop lecture6Oop lecture6
Oop lecture6
 
Calendario portada
Calendario portadaCalendario portada
Calendario portada
 
Cwgd
CwgdCwgd
Cwgd
 
Oop lecture8
Oop lecture8Oop lecture8
Oop lecture8
 
Oop lecture9 12
Oop lecture9 12Oop lecture9 12
Oop lecture9 12
 
Oop lecture9 13
Oop lecture9 13Oop lecture9 13
Oop lecture9 13
 
Oop lecture9 11
Oop lecture9 11Oop lecture9 11
Oop lecture9 11
 

Semelhante a Oop lecture1

Semelhante a Oop lecture1 (20)

java-programming.pdf
java-programming.pdfjava-programming.pdf
java-programming.pdf
 
OOP with Java - continued
OOP with Java - continuedOOP with Java - continued
OOP with Java - continued
 
Java file
Java fileJava file
Java file
 
Wap to implement bitwise operators
Wap to implement bitwise operatorsWap to implement bitwise operators
Wap to implement bitwise operators
 
Java programs
Java programsJava programs
Java programs
 
C language
C languageC language
C language
 
Java PSkills Session-6 PNR.pptx
Java PSkills Session-6 PNR.pptxJava PSkills Session-6 PNR.pptx
Java PSkills Session-6 PNR.pptx
 
39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
 
39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
 
06slide.ppt
06slide.ppt06slide.ppt
06slide.ppt
 
JAVAPGMS.docx
JAVAPGMS.docxJAVAPGMS.docx
JAVAPGMS.docx
 
C++ Template
C++ TemplateC++ Template
C++ Template
 
Java Lab Manual
Java Lab ManualJava Lab Manual
Java Lab Manual
 
Presentation1 computer shaan
Presentation1 computer shaanPresentation1 computer shaan
Presentation1 computer shaan
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
 
Java Question-Bank-Class-8.pdf
Java Question-Bank-Class-8.pdfJava Question-Bank-Class-8.pdf
Java Question-Bank-Class-8.pdf
 
Java practical(baca sem v)
Java practical(baca sem v)Java practical(baca sem v)
Java practical(baca sem v)
 
11slide.ppt
11slide.ppt11slide.ppt
11slide.ppt
 
(chapter 5) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 5) A Concise and Practical Introduction to Programming Algorithms in...(chapter 5) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 5) A Concise and Practical Introduction to Programming Algorithms in...
 
Data structures
Data structuresData structures
Data structures
 

Mais de Shahriar Robbani (8)

Group111
Group111Group111
Group111
 
SQL
SQLSQL
SQL
 
Oop lecture9 10
Oop lecture9 10Oop lecture9 10
Oop lecture9 10
 
Oop lecture4
Oop lecture4Oop lecture4
Oop lecture4
 
Oop lecture9
Oop lecture9Oop lecture9
Oop lecture9
 
Oop lecture7
Oop lecture7Oop lecture7
Oop lecture7
 
Oop lecture5
Oop lecture5Oop lecture5
Oop lecture5
 
Oop lecture3
Oop lecture3Oop lecture3
Oop lecture3
 

Último

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
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
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
 
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
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 

Último (20)

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
 
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
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
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)
 
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...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 

Oop lecture1

  • 1. Lecture 1 Object Oriented Programming Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
  • 2. Why object oriented programming Consider the following program written in C:
  • 3. #include <stdio.h> #include <math.h> #include <stdlib.h> struct cgpa{ int grades[3]; }ca; float getCgpa1(struct cgpa cgpa){ float sum = 0; int i = 0; for (i=0;i<3; i++){ sum += cgpa.grades[i]; } return sum/3; }
  • 4. float getCgpa2(struct cgpa cgpa){ float sum = 0; int i = 0; for (i=0;i<3; i++){ sum += cgpa.grades[i]; } return roundf(sum/3); }
  • 5. float getCgpa3(struct cgpa cgpa){ float sum = 0; int i = 0; for (i=0;i<3; i++){ sum += cgpa.grades[i]; } return abs(sum/3); } int main(){ printf ("%s", "a message to startn"); ca.grades[0] = 3; ca.grades[1] = 5; ca.grades[2] = 6; printf("%fn", getCgpa1(ca)); printf("%fn", getCgpa2(ca)); printf("%fn", getCgpa3(ca)); }
  • 6. OOP goals  Maximize code re-use  Minimize re-coding
  • 7. Same program in java public class Grade { public static void main(String[] args) { System.out.println(new Grade1().getCgpa()); System.out.println(new Grade2().getCgpa()); System.out.println(new Grade3().getCgpa()); } } class Grade1{ int[] grades = {3,5,6}; float sum; protected void calculateSum(){ for (int i=0; i<grades.length; i++) sum += grades[i]; } public float getCgpa(){ calculateSum(); return sum / grades.length; } } class Grade2 extends Grade1{ public float getCgpa(){ calculateSum(); return Math.round(sum / grades.length); } } class Grade3 extends Grade1{ public float getCgpa(){ calculateSum(); return (float)Math.floor(sum / grades.length ); } }
  • 8. How to start programming in OOP  Class A class is a template //a student public class ClassDemo { String code; String name; }
  • 9. Entry point public static void main(String[] args) {} e.g. //a student public class ClassDemo { String code; String name; public static void main(String[] args){ ClassDemo demo = new ClassDemo(); demo.code = "1221232323"; demo.name = "Raihan Kibria"; System.out.println(demo.code); System.out.println(demo.name); } }
  • 10. How to compile Install jdk Compile: javac ClassDemo.java How to run: java ClassDemo