Transcript Object-Oriented Design and Program Development in Business INFSY 535.1
Object-Oriented Design and Program Development in Business INFSY 535.1
Gayle J Yaverbaum School of Business Administration Penn State Harrisburg Fall 2006
Abstract Methods
• Abstract methods: } { public
abstract
void aMethod ( ) …..
Place the method in an
abstract
class public
abstract
class AnyClass
Abstract Methods (con’t)
• All subclasses must then have the method in them • Other words: – We want all subclasses to have the method – Method content will differ
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);
-
These classes extend public static void main (String[] argv) { DrawButton new SketchApp(); } }
-
cursor Object is a class
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; } Mutator method } public java.awt.Point getLocation () { return _location; } Assessor method
DrawButton Cursor RightButton LeftButton UpButton DownButton 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
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); }
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); }
//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 } ………………………..
…………………….
Lab 6
• Set up SketchApp in Eclipse • Page 178 – Number 4 under Modifying Programs • Zip files and upload
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
•
Objective: Tells other programmers how to use the method!
• •
precondition documentation postcondition 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.
Do NOT:
1) Repeat any code description!
(code is hidden and user/programmer on needs to know what to expect and what must be supplies)
2) Describe how code works 3) Use attribute names names
public abstract class DrawButton extends Ellipse {
Example
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); }
/** * 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.Color.red); } } public abstract java.awt.Point computeNextPoint (java.awt.Point lastPoint);
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!!!
Understanding More About Java
Arithmetic Conditional Statements
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!
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
We use integers to position and size shapes on screen.
Entire screen or any sub screen defines bounded coordinate system
(0, 0) (0, MAX_Y) (MAX_X, 0) (MAX_X, MAX_Y)
Integral Types
Type Size in Bits Minimum Value to Maximum Value byte 8 -128 to 127 short int long 16 32 64 -32,768 to 32,767 -2,147,483,648 to 2,147,483,647 -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
24
Floating Point Types
Type Size in Bits Range of Values float double 32 64 +1.4E - 45 to +3.4028235E+38 +4.9E - 324 to +1.7976931348623157E+308
Integer Assignment
public class HockeyPlayer { private int _numTeeth; public HockeyPlayer() { _numTeeth = 8; } public void playHockey() { _numTeeth = 4; reassignment //note } }
Some Java Operators
Precedence
Higher
Operator () Description Parentheses
Lower
+ * / % + = Positive Negative Multiplication Division Modulus (remainder) Addition Subtraction Assignment
Evaluation
evaluation takes place from left to right,
except
:
expressions in parentheses
*
,
/
, and
%
have precedence over
+
and
-
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
Evaluate
(7 * (10 - 5) % 3) * 4 + 9
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
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
Dialog Boxes
Message Dialog Box
Input Dialog Box
Confirm Dialog Box
static String showInputDialog (object msg)
Displays an input box with a message and an input text field number = JOptionPane.showInputDialog (“Enter a number: “);
Input ?
Enter an Integer: 24 OK Cancel
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? “);
Select an Option ?
Try Again?
Yes No Cancel
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.showMessageDialog (“The Number is an Integer “);
Message i The Number is an Integer OK
Predefined Numeric Classes
class methods for translating Strings to numeric values: parseInt parseFloat parseLong, parseDouble
Converting Strings to Numbers
double _y1; int _y2; String _number;
_number = JOptionPane.showInputDialog (“Enter a number: “); y1=Double.parseDouble(_number); y2= Integer.parseInt (_number);
More Operators
•
To change sign, use expression operator on single val = -val; // negates val
•
Since = represents assignment, not statement of equality, can operate on and assign to same variable
–
e.g., a = a + 5;
We can also do this using shorthand
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
Fractional Numbers
float
s (floating point) or
double
s (double precision floating point) We declare and assign these in same way
double myRadius = 2.427;