Gayle J Yaverbaum - PowerPoint PPT Presentation

About This Presentation
Title:

Gayle J Yaverbaum

Description:

a class defining an abstract method must be called 'abstract' ... Class comments may be general, providing overview information. ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 43
Provided by: person3
Category:
Tags: class | gayle | yaverbaum

less

Transcript and Presenter's Notes

Title: Gayle J Yaverbaum


1
Object-Oriented Design and Program Development in
Business INFSY 535.1
  • Gayle J Yaverbaum
  • School of Business Administration
  • Penn State Harrisburg
  • Fall 2006

2
Abstract Methods
  • Abstract methods
  • public abstract void aMethod ( )
  • ..
  • Place the method in an abstract class
  • public abstract class AnyClass

3
Abstract Methods(cont)
  • All subclasses must then have the method in them
  • Other words
  • We want all subclasses to have the method
  • Method content will differ

4
public class SketchApp extends Frame private
Cursor _cursor private DrawButton _upButton,
_downButton private DrawButton _leftButton,
_rightButton public SketchApp()
//instantiates a cursor which simply keeps track
of a location // the
_cursor is not visible _cursor new
Cursor() _upButton new UpButton(350,
400, _cursor) _rightButton new
RightButton(370, 420, _cursor)
_leftButton new LeftButton(330, 420, _cursor)
_downButton new DownButton(350, 440,
_cursor) - public static void main
(String argv) new SketchApp()
  • These classes extend
  • DrawButton
  • cursor Object is a
  • class

SketchApp.java
5
package sketchapp import wheels.users. public
class Cursor private java.awt.Point
_location public Cursor () _location
new java.awt.Point(350, 250) public
void setLocation(java.awt.Point point)
_location point public
java.awt.Point getLocation () return
_location
Mutator method
Assessor method
6
  • Remember
  • LeftButton, rightbutton, upButton, and
    downButton are all
  • subclasses of the DrawButton class
  • DrawButton has an abstract computeNextPoint()
    method that
  • each of its subclasses (LeftButton,
    RightButton, UpButton,
  • DownButton) MUST also defines!
  • Each subclass responds to computeNext ()
    differently

7
package sketchapp import wheels.users. public
class UpButton extends DrawButton public
UpButton (int x, int y, Cursor cursor)
super(x, y, cursor) //super class is
DrawButton public java.awt.Point
computeNextPoint
(java.awt.Point lastPoint) return new
java.awt.Point(lastPoint.x,
lastPoint.y-5)
8
package sketchapp public class LeftButton
extends DrawButton public LeftButton(int
x, int y, Cursor cursor) super(x, y,
cursor) public java.awt.Point
computeNextPoint
(java.awt.Point lastPoint) return new
java.awt.Point(lastPoint.x-5,
lastPoint.y)
9
//a class defining an abstract method must be
called abstract public abstract class
DrawButton extends Ellipse private Cursor
_cursor // is an instance variable in
all the button classes! // receives cursor,
location is sent to the super class! // Known
as peer objects and provide an instance of the
class public DrawButton (int x, int y,
Cursor cursor) super(x, y)
this.setSize(20, 20) _cursor cursor //
store reference to peer cursor
..ltnext pagegt
10
.ltlast pagegt public void
mousePressed(java.awt.event.MouseEvent e)
this.setFillColor(java.awt.Color.blue)
public void mouseReleased(java.awt.event.MouseEve
nt e) java.awt.Point lastPoint
_cursor.getLocation() java.awt.Point
nextPoint
computeNextPoint(lastPoint) Line line
new Line(lastPoint, nextPoint)
line.setColor(java.awt.Color.black)
line.setThickness(2) _cursor.setLocation(
nextPoint) this.setFillColor(java.awt.Co
lor.red) //defined in each ButtonDraw
method is abstract public abstract
java.awt.Point computeNextPoint
(java.awt.Point lastPoint)
11
Lab 6
  • Set up SketchApp in Eclipse
  • Page 178 Number 4 under Modifying Programs
  • Zip files and upload

12
Documentation Comments
  • Documentation included in form of comments
  • Prior to a class
  • Prior to a method
  • Begins with / and ends with /
  • Method comments are discouraged
  • Tend to clutter

13
Documentation
  • Objective
  • Tells other programmers how to use the
    method!
  • precondition documentation
  • postcondition documentation

14
Documentation
  • Precondition describes the state of relevant
    identifiers
  • prior to the function's execution.
  • Includes a reference to what variables are
  • passed to the function.
  • Postcondition describes the state after the
    function is
  • executed
  • the state of attributes after execution
  • the return value if any.

15
Documentation
  • Do NOT
  • Repeat any code description!
  • (code is hidden and user/programmer on needs to
    know what to expect and what must be supplies)
  • Describe how code works
  • Use attribute names names

16
public abstract class DrawButton extends Ellipse
private Cursor _cursor /
precondition The subclass for drawing a button
has inherited this class
postcondition a round button (30,30 pixels) is
on the screen / public
DrawButton (int x, int y, Cursor cursor)
super(x, y) this.setSize(30, 30) _cursor
cursor // store reference to peer cursor
/ precondition The mouse has not been
activated postcondition A button on the
screen has been selected and
the color is changed to blue / public void
mousePressed(java.awt.event.MouseEvent e)
this.setFillColor(java.awt.Color.blue)
Example
17
/ precondition The mouse is selected and
the button color is blue
postcondition A line has been drawn between
the previous cursor location
and the location obtained by
calling a method that determines the
next point. Tje
button color is changed to red / public
void mouseReleased(java.awt.event.MouseEvent e)
java.awt.Point lastPoint
_cursor.getLocation() java.awt.Point
nextPoint
computeNextPoint(lastPoint) Line line
new Line(lastPoint, nextPoint)
line.setColor(java.awt.Color.black)
line.setThickness(2) _cursor.setLocation(
nextPoint) this.setFillColor(java.awt.Co
lor.red) public abstract
java.awt.Point computeNextPoint
(java.awt.Point lastPoint)
18
Summary
  • What is the state of the method variables prior
    to execution
  • What is the state of a method variables after
    execution
  • Integrate pre and post condition comments prior
    to all methods.
  • Class comments may be general, providing overview
    information.
  • Include on ALL projects from this time forward!!!

19
Understanding More About Java
  • Arithmetic
  • Conditional Statements

20
Numbers
21
Declaring Base Types
  • Base primitive types
  • int, double, etc
  • do not have constructors, instance variables, or
    methods
  • do not instantiate (new) them
  • assign values to them!

22
Integer Assignment
  • Integer variables can be
  • used in arithmetic expressions,
  • sent as parameters,
  • and assigned values, just like other variables
  • references to objects are assigned a default
    value of null integers are assigned a default
    value of 0

23
We use integers to position and size shapes on
screen. Entire screen or any sub screen defines
bounded coordinate system
24
Integral Types
Type Size in Bits Minimum Value
to Maximum Value byte 8 -128
to 127 short 16 -32,768 to
32,767 int 32 -2,147,483,648 to
2,147,483,647 long 64
-9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
25
Floating Point Types
  • Type Size in Bits Range of Values
  • float 32 1.4E - 45 to
  • 3.4028235E38
  • double 64 4.9E - 324 to
  • 1.7976931348623157E308

26
Integer Assignment
  • public class HockeyPlayer
  • private int _numTeeth
  • public HockeyPlayer()
  • _numTeeth 8
  • public void playHockey()
  • _numTeeth 4 //note reassignment

27
Some Java Operators
  • Precedence Operator Description
  • Higher () Parentheses

  • Positive
  • - Negative
  • Multiplication
  • / Division
  • Modulus (remainder)
  • Addition
  • - Subtraction
  • Lower
    Assignment

28
Evaluation
  • evaluation takes place from left to right,
    except
  • expressions in parentheses
  • , /, and have precedence over and -

29
Parentheses
  • evaluation takes place from left to right,
    except
  • expressions in parentheses
  • , /, and have precedence over and
  • Parentheses can be used to change the usual order
  • Parts in () are evaluated first

30
Evaluate (7 (10 - 5) 3) 4
9
31
Evaluation Follow Rules from 9th Grade Algebra
  • Sample Arithmetic Expressions
  • 2
  • 2 3
  • 2 4 3 - 7
  • 4 / 2
  • 10 3
  • (2 3) (11 / 12)
  • (6 4) 3 (2 - (6 / 3))
  • 0 9
  • 9 9

32
rules
  • May mix types in the equation
  • if any variable is float, the result will be
    floating point
  • Note only two variables are considered
  • at a single instant in time
  • Mod () operator may only be used with integers
    and always yields an integer result

33
Dialog Boxes
  • Message Dialog Box
  • Input Dialog Box
  • Confirm Dialog Box

34
static String showInputDialog (object msg)
Displays an input box with a message and an
input text field number JOptionPane.showInputD
ialog (Enter a number )
35
static int showConfirmDialog (component parent,
object msg) Displays the specific message and
yes/no buttons. If the parent is null, the box is
displayed in the center of the screen ans
JOptionPane.ConfirmDialog (Try Again? )
36
static void showMessageDialog (component
parent,object msg) Displays the specific message
and if the parent is null, the box is displayed
in the center of the screen. JOptionPane.showMess
ageDialog (The Number is an Integer )
37
Predefined Numeric Classes
  • class methods for translating Strings to numeric
    values
  • parseInt parseLong,
  • parseFloat parseDouble

38
Converting Strings to Numbers
  • double _y1
  • int _y2
  • String _number
  • _number JOptionPane.showInputDialog (Enter a
    number )
  • y1Double.parseDouble(_number)
  • y2Integer.parseInt (_number)

39
More Operators
  • To change sign, use - operator on single
    expression
  • val -val // negates val
  • Since represents assignment, not statement of
    equality, can operate on and assign to same
    variable
  • e.g., a a 5

40
We can also do this using shorthand ltopgt a
5 a a 5 a - 5 a a - 5 a
5 a a 5 a / 5
a a / 5 a 5 a a 5 a
a a 1 a-- a a - 1
41
Variable Increment operators
  • Variable increment operators
  • i use i, then add 1 to it
  • i add 1 to i, then use it
  • What does order mean?
  • first case, prefix increment variable, then use
  • int i 10
  • int j i // j is 11, so is i
  • second case, postfix use, then increment
    variable
  • int i 10
  • int j i // j is 10, i becomes 11
  • same for decrement operators

42
Fractional Numbers
floats (floating point) or doubles (double
precision floating point) We declare and
assign these in same way
double myRadius 2.427
Write a Comment
User Comments (0)
About PowerShow.com