Sunteți pe pagina 1din 17

The Line Class

Suppose you are involved in the development of


a large mathematical application, and this
application needs an object to represent a Line.
You might proceed in this manner:
Attributes of a Line:
x coordinate of endpoint
y coordinate of endpoint
x coordinate of endpoint
y coordinate of endpoint
the length of the line

1
1
2
2

Behaviors (actions) of a Line:

change point 1
change point 2
report length
report equation
**there are many more behaviors that a
Line could have .. Well keep it simple

We can describe the class that will be


need for this class in a UML class diagram.

UML (unified
modeling language)
is a language of
diagrams, which
can be used to
describe a design.
A UML class
diagram has 3
parts:

Putting together the UML diagram involved determining


which data types will be used to for storing each of the
attributes.
Additionally, it was necessary to think of what information
needed to be provided for each behavior to take place
(parameters).
Once this design activity has been completed, it is relatively
easy to code the class skeleton.

public class Line{


private int xcoord1 = 0; //pt 1 default (0,0)
private int ycoord1 = 0;
private int xcoord2 = 1; // pt 2 default (1,0)
private int ycoord2 = 0;
private double len = 1.0;
// initial/default values have been given to each attribute
public Line() {
// method implementation
}
public Line (int x1,int y1, int x2, int y2){
// method implementation
}
public void setPt1(int xpos, int ypos){
// method implementation
}
public void setPt2(int xpos, int ypos){
// method implementation
}
public double length () {
// method implementation
}

public String equation () {


// method implementation
}

Instance Fields (Variables)


access specifier (such as private)
type of variable (such as int)
name of variable (such as xcoord1)
eg:

private int xcoord1;

Each object created from this class (each instance) gets its
own copy of this variable.
Private indicates that this class member is not accessible from
other classes
Initial values may be provided for instance variables, which
are stored prior to the execution of constructor statements.

Accessing Instance Fields


The length method of the Line class can
access the private instance field len:
public double length(){
return len;
}
But methods outside the Line class
cannot access the variable len!!

Coding the Constructors


A constructor initializes the instance variables
Constructor name = class name
Constructor has NO return type
Constructor is only method called with new
public Line ( ){ }

//this constructor makes no changes to


// default values assigned in declarations

public Line (int x1,int y1, int x2, int y2){


xcoord1 = x1;
ycoord1 = y1;
xcoord2 = x2;
ycoord2 = y2;
len = Math.sqrt((x2 x1) *( x2 x1) +
(y2 y1) * (y2 y1) );
}

Coding the Mutators


* mutators are methods used from outside the class to change the value of one
of more instance variables (they mutate the object, or change its state)

public void setPt1(int xpos, int ypos){


xcoord1 = xpos;
ycoord1 = ypos;
len = Math.sqrt((xcoord2 xcoord1) *( xcoord2
xcoord1) + (ycoord2 ycoord1) * (ycoord2 ycoord1) );
}
public void setPt2(int xpos, int ypos){
xcoord2 = xpos;
ycoord2 = ypos;
len = Math.sqrt((xcoord2 xcoord1) *( xcoord2
xcoord1) + Math.pow (ycoord2 ycoord1, 2) );
}

Explicit and Implicit Parameters


public void setPt1(int xpos, int ypos){
xcoord1 = xpos;
ycoord1 = ypos;
etc .
}
*xpos and ypos are clearly parameters to this method, however there is a hidden
parameter the object which was used to invoke the method

Assume myline is a Line object, and the call: myline.setPt1(6,7);


Within the setPt1 code:

xcoord1 refers to xcoord1 of the object to the left of the dot


ycoord1 refers to ycoord1 of the object to the left of the dot:
Means:
myline.xcoord1 = xpos; //however, this syntax would not
myline.ycoord1 = xpos; //be permitted

Coding an Access Method


* an access method is a method which returns the value of instance data,
which cannot be directly accessed outside the class

public double length () {


return len;
}

One Additional Method


* This method uses the instance data to return a printable representation
of the object in this case, the lines equation in y=mx + b form
public String equation () {
double m = slope();
double b;

if (xcoord1 == xcoord2) //vertical line


return x = + xcoord1;
else if (ycoord1 == ycoord2) // horizontal
return y = + ycoord1;
else{
b = ycoord1 ( m * xcoord1);
return y = + m + * x + + b;

A slope method must be added to this class for this method to function
properly.

Building a Test Program


1. Make a new subfolder for your
program.
2. Make one file for each class.
3. Compile all files.
4. Run the test program.

public class LineApp{


//create a Line, change its position, report its length and output its position

public static void main (String [] args){


Line myline = new Line( 2,2,6,7);
Line anotherline = new Line();
myline.setPt1(1,2);
System.out.println(The length of my line is + myline.length() );
System.out.println(The line is: + myline.equation() );
System.out.println(The length of my line is +anotherline.length() );
System.out.println(The line is: + anotherline.equation() );
}
}

Access
public class LineApp{
//create a Line, change its position, report its length and output its position
public static void main (String [] args){
Line myline = new Line( 2,2,6,7);
NOTE: it would be illegal to try to access instance variables from this class !!
myline.xcoord1 = 1;
//error
myline.ycoord1 = 2;
//error
System.out.println(The length of my line is + myline.length();
System.out.println(The line is: + myline.equation();
}

}
The instance data is ENCAPSULATED by the object!!!! This preserves
the integrity of the data!!

Variable Types

Instance fields (xcoord1 in Line)


Local variables (myline in LineApp, )
Parameter variables (xpos in setPt1)

S-ar putea să vă placă și