Review - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Review

Description:

It would be easier to make it a subclass of Node (see the graphed2 framework of Horstmann) ... Problem: I want scrollbars on my textarea ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 30
Provided by: JamesB128
Category:

less

Transcript and Presenter's Notes

Title: Review


1
Review
2
Class Design
  • Why is PizzaNode a subclass of FoodItemNode?
  • It would be easier to make it a subclass of Node
  • (see the graphed2 framework of Horstmann)

3
Move Common Behavior Into a Superclass
  • class PizzaNode extends Node
  • private FoodItem item
  • public getFoodItem( ) return item
  • ...
  • class DrinkNode extends Node
  • private FoodItem item
  • public getFoodItem( ) return item
  • ...

4
Move Common Behavior (2)
  • class FoodNode extends Node
  • protected FoodItem item
  • public getFoodItem() return item
  • class PizzaNode extends FoodNode
  • // item moved to FoodNode
  • ...
  • class DrinkNode extends FoodNode
  • // item moved to FoodNode
  • ...

5
Patterns
6
What Pattern is This?
  • Problem I want scrollbars on my textarea

JTextArea textarea new JTextArea( 8 /rows/,
40 / columns / ) JScrollPane pane new
JScrollPane( textarea ) pane.setVerticalScrollbar
Policy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
) // add to contents contentpane.add ( pane )
7
Give Example of Observer Pattern
8
Review of Java and OO Concepts
9
Student (sorry, this is boring)
T
ltltinterfacegtgt Comparable compareTo( other )
  • Implement the Student class described at right.
  • Use the appropriate visibility for all members
    (attributes and methods).
  • Compare students by id
  • Write a toString that can be used to produce
    output like this
  • 48541111 Bill Gates

ltTStudentgt
Student id firstname lastname Student( first,
last, id ) Student( first, last ) compareTo(
student ) toString( )
10
Write a StudentReader class
  • Purpose reads student data from an InputStream.
  • Each line of data contains
  • student_id firstname lastname
  • 48541111 Bill Gates
  • Provides an IteratorltStudentgt to return one
    student object for each line of data read.
  • Questions
  • what methods does an Iterator have?
  • how do you show this in UML?
  • why use an InputStream? Why not a File?

11
StudentReader
T
ltltinterfacegtgt Iterator hasNext( ) boolean
next( ) T remove( )
  • Write the code

ltTStudentgt
Student - id - firstname - lastname Student(
first, last, id ) compareTo( s )
StudentReader StudentReader( InputStream )
"has a"
12
Draw a Sequence Diagram
  • void test2( )
  • String data new String( "3 5 8 13" )
  • Scanner in new Scanner( data )
  • int sum 0
  • while ( in.hasNext( ) )
  • int n in.nextInt( )
  • sum n
  • System.out.println("Sum is " sum )

13
Draw a Sequence Diagram
  • void test3( )
  • CouponCalc calc new MyCouponCalc( )
  • CouponUI ui new CouponUI( calc )
  • ui.run( )

14
Coupon Assistant
  • Why not design Coupon Assistant like this?

CouponUI - calc MyCouponCalc
MyCouponCalc MyCouponCalc( )
public CouponUI( ) calc new MyCouponCalc(
) public static void main( ... ) ui new
CouponUI( ) ui.run( )
15
What does this diagram mean?
  • Why did we design the Coupon Assistant like this?
  • What is the benefit of this?

ltltinterfacegtgt CouponCalc methods
CouponUI - calc CouponCalc run( )
MyCouponCalc MyCouponCalc( )
Main main( )
calc new MyCouponCalc( ) ui new CouponUI(
calc ) ui.run( )
16
JOptionPane
What is showConfirmDialog ? Is it a static or
instance method? What is JOptionPane.NO_OPTION
  • int choice
  • choice JOptionPane.showConfirmDialog( null,
  • "Are you awake?",
  • "This is a Confirm Dialog",
  • JOptionPane.YES_NO_CANCEL_OPTION )
  • if ( choice JOptionPane.NO_OPTION )
  • JOptionPane.showMessageDialog( null, "Liar!!")

17
Magic Numbers
Is there any problem with this code?
  • int choice
  • choice JOptionPane.showConfirmDialog( null,
  • "Do you want to quit?",
  • "Confirm Quit",
  • JOptionPane.OK_CANCEL_OPTION )
  • if ( choice 0 ) System.exit( 0 )

How would you improve it?
18
Comparator
  • Arrays.sort can accept a 2nd parameter
  • Arrays.sort( array, comparator )

array to sort
sort will call comparator.compare(a,b)
T
ltltinterfacegtgt Comparator compare( a T, b T
) equals( other )
Why is this useful? Is compare( ) an instance or
static method?
19
Sort Students by First Name
  • Write a Comparator that sorts students by first
    name.
  • We can't use "Student.compareTo" because it
    already sorts Students by ID.

/ Define a comparator for ordering students by
first name / public class StudentComparator
implements ComparatorltStudentgt int compare(
Student a, Student b ) // return if after
b, - if a before b, 0 if a and b have same
order return a.getFirstName(
).compareToIgnoreCase( b.getFirstName() )
20
Using the Comparator
  • // Example sort an array of students
  • Student students Registrar.getStudents( )
  • ComparatorltStudentgt comp new StudentComparator()
  • Arrays.sort( students, comp )

/ Define a comparator for comparing students by
first name / public class StudentComparator
implements ComparatorltStudentgt int compare(
Student a, Student b ) // return if after
b, - if a before b, 0 if a and b have same
order return a.getFirstName( ).compareToIgnoreCa
se( b.getFirstName() )
21
How to create using anonymous class
  • Student students Registrar.getStudents( )
  • ComparatorltStudentgt comp new ComparatorltStudentgt
    ( )
  • // anonymous class definition for a
    ComparatorltStudentgt object
  • int compare( Student a, Student b )
  • // return if after b, - if a before b, 0 if
    a and b have same order
  • return a.getFirstName( ).compareToIgnoreCase(
    b.getFirstName() )
  • // end of anonymous class and end of
    assignment ( )
  • Arrays.sort( students, comp )

22
Comparator Factory Method
  • Create a static method in the Student class that
    creates the comparator object (previous slide)
    using an anonymous class.
  • Named the method getStudentNameComparator( )

public class Student implements
ComparableltStudentgt ... public static
ComparatorltStudentgt getStudentNameComparator ( )
return new ComparatorltStudentgt( ) public
int compareTo( Student a, Student b )
return a.getFirstName().compareTo(
b.getFirstName() ) // end of the
statement // end of the method
23
Designing Classes
  • Choose responsibilities and attributes.
  • Design the public "interface" of the class.
  • Guides for class design
  • cohesion - should be high
  • coupling to other classes - lower is better
  • Guides for interface
  • completeness
  • convenience
  • clarity
  • consistency

24
BigDecimal Arithmetic
  • Write an average( ) method that computes the
    average of an array of BigDecimal objects.
  • public BigDecimal average( BigDecimal x )
  • BigDecimal sum new BigDecimal( 0.0 )
  • for( BigDecimal value x )
  • sum sum.add(value)
  • return sum.divide( new BigDecimal( x.length ) )

25
BigDecimal Arithmetic
  • Write a max( ) method that returns the maximum
    value in an array of BigDecimal objects, using
    only the compareTo method -- don't use
    BigDecimal.max( ).
  • public BigDecimal max( BigDecimal x )
  • BigDecimal max x0
  • //... find the max in x
  • for( int k1 k lt x.length k )
  • //if ( xk gt max ) max xk
  • if ( xk.compareTo(max) gt 0 ) max xk
  • return max

26
BigDecimal More Arithmetic
  • Write a static sqrt( ) method for BigDecimal
    objects using Euler's method of finding square
    roots.
  • Find sqrt(2) accurate to 100 decimal places.

Let y the value you want the square root
of tolerance allowed error in the
answer Algorithm x 1.0 // initial guess
repeat x0 x x ( x y/x ) / 2
until ( x - x0 lt tolerance ) x is
approximately the square root of y.
This is useful practice for how to use method
calls, but it throws a runtime exception because
some values have infinitely long decimal form.
You must use another form of BigDecimal methods
that has a remainder.
27
How to Write a Clone Method
  • Clone the Student class (next slide)

28
Deep copy of student
  • public class Student implements Cloneable,
    Comparable
  • private String id
  • private Date birthday // date is mutable
  • private ListltCoursegt courses
  • public Object clone()
  • //1. make a new clone object and clone
    superclasses
  • Student clone (Student) super.clone( )
  • //2. copy all the atrributes to the clone
  • clone.id new String( this.id )
  • clone.birthday this.birthday.clone()
  • clone.courses new ArrayListltCoursegt( )
  • for( course this.courses )
    clone.courses.add( course.clone() )
  • //3. return the clone
  • return clone

29
Variable Scope and try - catch
  • int a in.nextInt( )
  • int b in.nextInt( )
  • try
  • int c a / b
  • catch ( Exception e)
  • System.err.println("bad division")
  • System.out.println("a / b " c )
Write a Comment
User Comments (0)
About PowerShow.com