Oop lecture1
Upcoming SlideShare
Loading in...5
×
 

Oop lecture1

on

  • 332 views

 

Statistics

Views

Total Views
332
Views on SlideShare
332
Embed Views
0

Actions

Likes
0
Downloads
7
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Oop lecture1 Oop lecture1 Presentation Transcript

  • Lecture 1Object Oriented Programming Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
  • Why object oriented programmingConsider 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 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 ); }}
  • How to start programming in OOP ClassA class is a template//a studentpublic class ClassDemo { String code; String name;}
  • 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); }}
  • How to compileInstall jdkCompile:javac ClassDemo.javaHow to run:java ClassDemo