CS100J%20Lecture%2025 - PowerPoint PPT Presentation

About This Presentation
Title:

CS100J%20Lecture%2025

Description:

CS 100. Lecture 25. 1. CS100J Lecture 25. Previous Lecture. Inheritance ... Grotesque duplication of code. Loss of 'single point of change'. No abstraction. ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 16
Provided by: Mill1
Category:

less

Transcript and Presenter's Notes

Title: CS100J%20Lecture%2025


1
CS100J Lecture 25
  • Previous Lecture
  • Inheritance
  • Method overriding
  • Polymorphism
  • This Lecture
  • Application of inheritance
  • Higher-order" methods
  • Abstract classes
  • Visibility modifier protected

2
Recall class ML Method add
  • // x y
  • public static ML add(ML x, ML y)
  • if ( x.isEmpty() y.isEmpty() )
  • return new ML(0,0)
  • else if ( x.isScalar() )
  • ML result new ML(y.h, y.w)
  • ml scalarValue x.values00
  • for (int r 0 r lt y.h r)
  • for (int c 0 c lt y.w c)
  • result.valuesrc
  • ml.add( scalarValue,
  • y.valuesrc )
  • return result
  • else if ( y.isScalar() )
  • ML result new ML(x.h, x.w)

3
Recall class ML Method add, continued
  • else if (x.h ! y.h x.w ! y.w)
  • throw new RuntimeException(
  • matrix dimensions do not match")
  • else
  • ML result new ML(x.h, x.w)
  • for (int r 0 r lt x.h r)
  • for (int c 0 c lt x.w c)
  • result.valuesrc
  • ml.add( x.valuesrc,
  • y.valuesrc)
  • return result
  • // end of method add

4
Recall Class ml Arithmetic Methods
  • public static ml add(ml x, ml y)
  • return new ml(x.re y.re, x.im y.im)
  • public static ml sub(ml x, ml y)
  • return new ml(x.re - y.re, x.im - y.im)
  • / other operators
  • . ./ lt lt gt gt unary- etc.
  • /

5
Recall the Critique of ML Method add
  • Most of the code (all but the 3 invocations of
    ml.add) has nothing to do with the fact that this
    is the method for addition.
  • To implement subtraction, almost all code (except
    for the 3 invocations of ml.add) would be
    identical.
  • We could implement each of the other
    element-by-element matrix operations in about 30
    seconds each
  • Copy the definition of ML.add
  • Replace add with sub, or mult, or div,
    etc.
  • Write ml.add, ml.mult, ml.div, etc.
  • Terrible
  • Grotesque duplication of code.
  • Loss of single point of change.
  • No abstraction.
  • Surely, there must be a way to avoid duplicating
    the common code.
  • Coming later...
  • Now is that time

6
class ML Method Binary
  • // x op y
  • public static ML Binary(
  • OP o, // where method o.op combines 2 mls.
  • ML x, // left operand.
  • ML y // right operand.
  • )
  • if (x.isEmpty() y.isEmpty())
  • return new ML(0,0)
  • else if (x.isScalar())
  • ML result new ML(y.h, y.w)
  • ml scalarValue x.values00
  • for (int r 0 r lt y.h r)
  • for (int c 0 c lt y.w c)
  • result.valuesrc
  • o.op( scalarValue,
  • y.valuesrc)
  • return result

7
class ML Method Binary, continued
  • else if ( y.isScalar() )
  • ML result new ML(x.h, x.w)
  • ml scalarValue y.values00
  • for (int r 0 r lt x.h r)
  • for (int c 0 c lt x.w c)
  • result.valuesrc
  • o.op( x.valuesrc,
  • scalarValue)
  • return result
  • else if (x.h ! y.h x.w ! y.w)
  • throw new RuntimeException(
  • matrix dimensions do not match")
  • else
  • ML result new ML(x.h, x.w)
  • for (int r 0 r lt x.h r)
  • for (int c 0 c lt x.w c)

8
Class OP a class of binary operations
  • class OP
  • // A placeholder binary operation.
  • ml op(ml x, ml y)
  • return null
  • class BinaryAdd extends OP
  • ml op(ml x, ml y)
  • return ml.add(x,y)
  • class BinarySub extends OP
  • ml op(ml x, ml y)

9
Class ML Methods add and sub
  • // x y
  • public static ML add(ML x, ML y)
  • return ML.Binary(new BinaryAdd(), x, y)
  • // x - y
  • public static ML sub(ML x, ML y)
  • return ML.Binary(new BinarySub(), x, y)

10
Sample Client Code
  • // ones(2,3) ones(2,3)
  • ML.add( ML.ones(2,3), ML.ones(2,3) )
  • Evaluation of expression
  • ML.add(,)
  • invokes
  • ML.Binary(new BinaryAdd(), , )
  • which invokes
  • BinaryAdd.op(,)
  • to add pairs of complex matrix elements, which
    invokes
  • ml.add(,)

11
Abstract Classes
  • abstract class OP
  • // A placeholder binary operation.
  • abstract ml op(ml x, ml y)
  • class BinaryAdd extends OP
  • ml op(ml x, ml y)
  • return ml.add(x,y)
  • class BinarySub extends OP
  • ml op(ml x, ml y)
  • return ml.sub(x,y)

12
An Application of Inheritance
  • abstract class OP
  • static int count 0 // ml operations.
  • // A placeholder binary operation.
  • abstract ml op(ml x, ml y)
  • class BinaryAdd extends OP
  • ml op(ml x, ml y)
  • count
  • return ml.add(x,y)
  • class BinarySub extends OP
  • ml op(ml x, ml y)

13
Visibility Modifier Protected
  • Recall the visibility modifiers of constructors,
    methods, or fields
  • public A public item is visible anywhere in the
    program
  • private A private items is only visible in the
    class in which the item is declared or defined.
    In particular, a private item declared or defined
    in class c is not visible in the subclasses of a
    class c
  • The new visibility modifier
  • protected A protected item is visible in the
    class in which the item is declared or defined,
    and in any subclasses of that class

14
Visibility Modifier Protected
  • abstract class OP
  • // ml operations.
  • protected static int count 0
  • // A placeholder binary operation.
  • abstract ml op(ml x, ml y)
  • static int getCount() return count
  • class BinaryAdd extends OP
  • ml op(ml x, ml y)
  • count
  • return ml.add(x,y)

15
Sample Client Code
  • // ones(2,3) ones(2,3)
  • System.out.println(
  • ML.add( ML.ones(2,3), ML.ones(2,3) )
  • )
  • / Output the total number of primitive ml
    operations performed by all matrix operations to
    date. /
  • System.out.println(
  • OP.getCount()
  • )
  • Output
  • 2 2 2
  • 2 2 2
  • 6
Write a Comment
User Comments (0)
About PowerShow.com