object code 2
TRANSCRIPT
-
7/26/2019 Object Code 2
1/17
The Line Class
Suppose you are involved in the development of
a large mathematical application, and thisapplication needs an object to represent a Line.
You might proceed in this manner:
Attributes of a Line: x coordinate of endpoint 1
y coordinate of endpoint 1 x coordinate of endpoint 2 y coordinate of endpoint 2 the length of the line
-
7/26/2019 Object Code 2
2/17
ehaviors !actions" of a Line:
change point 1 change point 2 report length
report e#uation
**there are many more behaviors that a Line could have .. Well keep it simple
$e can describe the class that %ill beneed for this class in a UMLclass diagram.
-
7/26/2019 Object Code 2
3/17
UML (unified
modeling language)
is a language of
diagrams !hich
can be used to
describe a design.
" UML class
diagram has #
parts$
-
7/26/2019 Object Code 2
4/17
-
7/26/2019 Object Code 2
5/17
Putting together the UML diagram involved determiningwhich data types will be used to for storing each of theattributes.
Additionally, it was necessary to thin of what informationneeded to be provided for each behavior to tae place!parameters".
#nce this design activity has been completed, it is relativelyeasy to code the class seleton$.
-
7/26/2019 Object Code 2
6/17
public class Line%
private int &coord' ( )* ++pt ' default !),)" private int ycoord' ( )* private int &coord ( '* ++ pt default !',)" private int ycoord ( )* private double len ( '.)* ++ initial+default values have been given to each attribute
public Line!" % ++ method implementation -
public Line !int &',int y', int &, int y"% ++ method implementation -
public void setPt'!int &pos, int ypos"% ++ method implementation -
public void setPt!int &pos, int ypos"% ++ method implementation -
public double length !" % ++ method implementation -
public tring e/uation !" % ++ method implementation
--
-
7/26/2019 Object Code 2
7/17
0nstance 1ields !2ariables"
access specifier!such as private"
type of variable!such as int"
name of variable!such as &coord'"
eg3 private int &coord'*4ach ob5ect created from this class !each instance) gets its
own copy of this variable.
Private indicates that this class member is not accessible fromother classes
0nitial values may be provided for instance variables, which
are storedprior to the e&ecution of constructor statements.
-
7/26/2019 Object Code 2
8/17
Accessing 0nstance 1ields
The length method of the Line class canaccess the private instance field len$
public double length()%
return len&
'
ut methods outside the Line class
cannot access the variable len
-
7/26/2019 Object Code 2
9/17
Coding the Constructors
A constructor initiali6es the instance variables
Constructor name ( class name
Constructor has 7# return type
Constructor is only method called with 8new
public Line ! "% - ++this constructor maes no changes to ++ default values assigned in declarations
public Line !int &',int y', int &, int y"% &coord' ( &'*
ycoord' ( y'* &coord ( &* ycoord ( y* len ( Math.s/rt!!& 9 &'" :! & 9 &'" ; !y 9 y'" : !y 9 y'" "*
-
-
7/26/2019 Object Code 2
10/17
Coding the Mutators
: mutators are methods used from outside the class to change the value of one
of more instance variables !they 8mutate< the ob5ect, or change its state"
public void setPt'!int &pos, int ypos"% &coord' ( &pos* ycoord' ( ypos*
len ( Math.s/rt!!&coord 9 &coord'" :! &coord 9
&coord'" ; !ycoord 9 ycoord'" : !ycoord 9 ycoord'" "* -
public void setPt!int &pos, int ypos"% &coord ( &pos*
ycoord ( ypos*len ( Math.s/rt!!&coord 9 &coord'" :! &coord 9
&coord'" ; Math.pow !ycoord 9 ycoord', " "* -
-
7/26/2019 Object Code 2
11/17
4&plicit and 0mplicit Parameterspublic void setPt'!int &pos, int ypos"%
&coord' ( &pos* ycoord' ( ypos* $$ etc $.- :&pos and ypos are clearly parameters to this method, however there is a 8hidden