Oop lecture1

332 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
332
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Oop lecture1

  1. 1. Lecture 1Object Oriented Programming Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
  2. 2. Why object oriented programmingConsider the following program written in C:
  3. 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. 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. 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. 6. OOP goals Maximize code re-use Minimize re-coding
  7. 7. Same program in javapublic 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. 8. How to start programming in OOP ClassA class is a template//a studentpublic class ClassDemo { String code; String name;}
  9. 9. Entry pointpublic static void main(String[] args) {}e.g.//a studentpublic 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. 10. How to compileInstall jdkCompile:javac ClassDemo.javaHow to run:java ClassDemo

×