Review%20:%20abstract - PowerPoint PPT Presentation

View by Category
About This Presentation
Title:

Review%20:%20abstract

Description:

force concrete subclasses to implement abstract methods (if any) cannot be instantiated ... A moth stuck in a Harvard Mark II mainframe in 1947. 17. Bugs are bad ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 46
Provided by: weit7
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Review%20:%20abstract


1
Review abstract
  • abstract method
  • a method without implementation
  • abstract class
  • an incomplete class
  • force concrete subclasses to implement abstract
    methods (if any)
  • cannot be instantiated

2
The Student Class Hierarchy
  • class Student
  • public String getGrade()
  • return ERROR
  • class PassFailStudent extends Student
  • public String getGrade()
  • return (average gt 70) ? pass fail
  • class LetterGradeStudent extends Student
  • public String getGrade()

3
Using Student Class
  • Student students new StudentnumOfStudents
  • // create students
  • if (enrolledAsPassFail)
  • studentsi new PassFailStudent(..)
  • else
  • studentsi new LetterGradeStudent(..)
  • // prints the name and final grade of all
    students

4
Abstract Student Class
  • abstract class Student
  • public abstract String getGrade()
  • class PassFailStudent extends Student
  • public String getGrade()
  • return (average gt 70) ? pass fail
  • class LetterGradeStudent extends Student
  • public String getGrade()
  • if (average gt 90) return A
  • else if (average gt 70) return B

5
Interface
  • Pure abstract class
  • Must contain only constant members and abstract
    methods
  • Members are public final static by default
  • Methods are public abstract by default

6
Implements
  • an interface does not contain any implementation
  • a class provides implementations
  • We say a class implements an interface

7
interface And implements
  • interface Hero
  • boolean NEVER_DIES true
  • void swim()
  • void fight()
  • void jump()
  • class Jedi implements Hero
  • public void fight()
  • // swing light saber
  • public void swim() ...
  • public void jump() ...
  • class WesternHero implements Hero
  • public void fight()

8
More interface Examples
  • interface NumericSet
  • void add(Number n)
  • void remove(Number n)
  • void union(NumericSet n)
  • void intersect(NumbericSet n)
  • boolean isEmpty()
  • boolean contains(Number n)
  • int size()
  • interface Printable
  • void print()

9
More implements Examples
  • class ArraySet implements NumericSet, Printable
  • public void add(Number n)
  • ...
  • public void remove(Number n)
  • ...
  • public void union(NumericSet n)
  • ...
  • public void intersect(NumbericSet n)
  • ...
  • public boolean isEmpty()
  • ...
  • public boolean contains(Number n)
  • ...
  • public int size()
  • ...
  • public void print()
  • ...

10
Differences
  • a class can implement more than one interfaces
  • A implements B A has the behaviors of B
  • a class can only extend one other class
  • A extends B A is a kind of B

11
Extending Interface
  • We can extend an interface
  • interface A extends B B is called
    superinterface
  • We can override methods in superinterface
  • We can overload methods in superinterface

12
Extending interface
  • interface FlyingHero extends Hero
  • void fly()
  • interface RoboticHero extends Hero
  • void fight(Weapon w)
  • void swim() throws RustException
  • interface SortedSet extends NumericSet
  • boolean isSorted()
  • void sort()
  • Number first()
  • Number last()

13
Try it out yourself 1
  • can we declare a class without abstract methods
    as abstract ?
  • abstract class A
  • int f()
  • return 1

14
Try it out yourself 2
  • must we implement all abstract methods from the
    interface ?
  • interface I
  • int f()
  • int g()
  • class A implements I
  • int f() returns 1

15
Try it out yourself 3
  • can a class implement two interfaces with methods
    of the same name?

interface I1 int f() interface I2 int
f() interface I3 double f() class A
implements I1, I2 int f() return 1
class B implements I1, I3 int f()
return 1 double f() return 1.0
16
Bugs
  • Programming Errors
  • First bug
  • A moth stuck in a Harvard Mark II mainframe in
    1947.

17
Bugs are bad
  • 1990 ATT long distance service failed for 9
    hours and was traced to a single faulty line of
    code
  • 1991 Scud missile killed 28 soldiers because a
    bug caused the Patriot defense system to be off
    by 0.34 seconds
  • 2000 Y2K

18
Todays Lecture
  • How to prevent bugs ?
  • Understand the problem
  • Understand Java
  • Follow good programming practices
  • How to find bugs ?
  • Testing
  • How to kill bugs ?

19
Program Development
Design
Implement
Testing
20
Program Design Classes
Studentdouble averageGradeint grades6double
weight6calcAverageGrade()getAverageGrade()
Coursedouble averageGradedouble maxdouble
mindouble sumint numOfStudentStudent
studentscalcAverageGrade()getAverageGrade()
LetterGradeStudentprintGrade()
PassFailStudentprintGrade()
21
Program Design Pseudocode
Average grade for studentsfor ( i 0 .. 6)
average gradeiweightireturn average
Get inputget number of studentsfor i 1 ..
number of students get name get enrollment
status get all six grades if enroll as pass
fail then create a PassFailStudent else
create a LetterGradeStudent
Average Grade for coursefor each student sum
students weight averagereturn sum/number of
students
22
Program Design Data Flow
final grade
get final grade
Student obj
calc students average grade
average grade
23
Program Design Control Flow
24
Good Program vs. Bad Program
  • Easy to Read
  • Good Comments
  • Meaningful Names
  • Properly Indented
  • Blank Lines
  • Well-Designed
  • Cover all cases
  • Anticipate Changes
  • Facilitate Reuse

25
Very Bad Program
  • // A program that reads some inputs and outputs
    some stuffs
  • class P2Q3
  • public static void main(String X)
  • int temp 0, temp2 0, s 0, c0, M 0, X
  • TokenReader tr new TokenReader()
  • System.out.println(number)
  • int Ntr.ReadInt()
  • X new intNN tr.ReadInt()
  • while (N!0)sNif (NgtM)
  • MNXtemp N
  • temp
  • System.out.println(number)Ntr.ReadInt()
  • temp2
  • System.out.println(s temp2 temp)
  • temp2 temp1 0
  • while (temp2 lt X.length) System.out.println(Xtemp
    1)
  • temp2 temp1

26
Better Program
  • class P2Q3
  • public static void main(String X)
  • // sum total of values entered so far
  • // max maximum of values entered so far
  • // count number of values entered
  • int sum 0, count 0, max
    Integer.MAX_VALUE
  • TokenReader tr new TokenReader()
  • System.out.println(Enter number )
  • int input tr.ReadInt()
  • // keep reading until user enters 0
  • while (input ! 0)
  • sum input
  • // compare current input with maximum so
    far. update
  • // maximum so far if input is larger.
  • if ( input lt max )
  • max input

27
Good Design
  • Anticipate Changes
  • Reusable
  • Encapsulation
  • Think LEGO

28
  • public static void main(String args)
  • TokenReader in new TokenReader()
  • int histogram new int10
  • for (int i 0 i lt 10 i)
  • histogrami 0
  • int input in.readInt()
  • while (input ! 0)
  • if (input gt 1 input lt 10)
  • histogram0
  • else if (input gt 10 input lt 20)
  • histogram1
  • else if (...
  • // smarter implementation
  • // int n (input 1)/10

Bad Histogram Program
29
What if ..
  • create histogram for data between 1 .. 200 ?
  • tally data for smaller intervals ?
  • 1 5
  • 6 10
  • 11 15
  • 196 200
  • draw a histogram for average grade ?

30
  • class Histogram
  • int numOfBuckets, bucketSize, buckets
  • public Histogram(int numOfBuckets, int
    bucketSize)
  • this.numOfBuckets numOfBuckets
  • this.bucketSize bucketSize
  • buckets new intnumOfBucket
  • for (int j 0 j lt buckets.length j)
  • bucketsj 0
  • public void addData(int data)
  • buckets(data-1)/bucketSize
  • public void print()
  • for (int j 0 j lt numOfBucket j)
  • System.out.println((jbucketSize 1)
    - ((j1)bucketSize)))
  • for (int k 0 k lt bucketsj k)
    System.out.print()

Good Histogram Program
31
Bug Preventions
  • code reuse
  • fewer lines of code to write, fewer bugs
  • anticipate changes
  • fewer lines of code to change,fewer bugs
  • encapsulation
  • bugs are confined to one place, easier to detect
    and fix.

32
Good Programs
  • Easy to read
  • blank lines
  • indentation
  • comments
  • meaningful names
  • Easy to read, easy to spot bugs !

33
Finding Bugs
  • Wrong attitude
  • My program works ! I am done.
  • Did you test it with all possible inputs ?
  • negative numbers ?
  • zero ?
  • Did you test all possible paths of execution ?

34
Component Testing
  • Another motivation for encapsulations !
  • Test each component separately
  • Make sure they worked before using them

35
Debugging Techniques Think high-level
  1. scan from left to right
  2. swap two adjacent elements if they are out of
    order
  3. repeat until everything is in order

36
Debugging Techniques Printout
  • Print out your code
  • Spread it on a large table
  • Walkthrough your code
  • Draw diagrams
  • Make notes

37
Debugging Techniques Explain it to Someone Else
  • Old Chinese Proverb
  • Onlookers see most of the game Players see
    very little

38
Debugging Techniques System.err.println
  • Let you inspect the intermediate value of
    variables
  • 53 2147483647 034 2147483647 010 2147483647 0
  • 82 2147483647 0 72 2147483647 0
  • Can you guess what is wrong now ?

39
Using System.err.println
  • class P2Q3
  • public static void main(String X)
  • // keep reading until user enters 0
  • while (input ! 0)
  • sum input
  • // compare current input with maximum so
  • // far. update maximum so far if input
  • // is larger.
  • if ( input lt max )
  • max input
  • count
  • System.err.println(input max
    count)
  • System.out.println(Enter number )
  • input tr.ReadInt()

40
Debugging Techniques assert
  • a method to make sure that your invariants are
    true.
  • void assert(boolean condition, String
    errorMessage) if (!condition) throw new
    Error(errorMessage)

41
Using assert
  • class Histogram
  • int numOfBuckets
  • int bucketSize
  • int buckets
  • public void addData(int data)
  • assert(data gt 0 data lt numOfBucketsbucket
    Size,
  • data data is out of range.)
  • buckets(data-1)/bucketSize

42
Debugging Techniques assert
  • The Error exception will cause the stack trace to
    be printed.
  • java.lang.Error data 364 is out of rangeat
    java.lang.Throwable.ltinitgtat java.lang.Error.ltini
    tgtat Histogram.assertat Histogram.addData
  • at P2Q4.createHistogramat P2Q4.main

43
Debugging Techniques Debugger
breakpoint pause execution
step incremental execution
continue resume execution
watch stop if value of a variable changes
call stack list currently active frames
variables inspect value of variables
44
Summary
  • Bug Prevention
  • understand the problem and language
  • design before sit in front of computer
  • design for change/reuse
  • Bug Discovery
  • test all flow of controls
  • test small components separately before using
    them

45
Summary
  • Bug Termination
  • re-think your algorithm from a higher-level
  • manually trace through your program
  • explain your program to others
  • System.err.println
  • assert
  • use a debugger
About PowerShow.com