Data Structures and Other Objects Using C++

nutmegactSoftware and s/w Development

Nov 10, 2012 (4 years and 7 months ago)

225 views

1

CS121 Data Structures

Agenda for January 27


Assignment for this time


Homework 1.2


Does it compile?


Does it work?


Look over issues list

An example using the Reciprocal Class.


Assignment for next Sunday night Homework 1.3


Javadoc


Appendix H


JUnit


See the web and Netbeans


Jsubmit working OK?


More from Chapter 2




Class 3

Jan 27

2

Java Classes & Information Hiding

Mostly Review / Maybe Some New



Java Classes and Information Hiding

Classes and their Members:



Should all be review

Using a Class



Playing with objects:



null references, assignments, clones, equality

Parameters, Equals Methods, and Clones



Static methods



Parameters that are objects



A return value may be an object



So many other good things!!

3

Java Review
-

References


A little more on classes
:

String x1 = new String("xyz");

x1.concat(“abc”); // x1 now points to “xyzabc”

String x2 = x1;

x2.concat(“uvw”);

System.out.println(x1.toString() ); // What is printed?


What is a clone
:

String x1 = new String("xyz");

String x2 = x1.clone();

x2.concat(“uvw”);

System.out.println(x1.toString() ); // What is printed?

Does (x1 == x2 )??

Does (x1 == x2 )??

4

Java Review


You can see it!!

http://cs.clarku.edu/~jbreecher/Data_Structures/Lectures/Example_Class_03.java



http://cs.clarku.edu/~jbreecher/Data_Structures/Lectures/Example_Class_03_Methods.java


http://cs.clarku.edu/~jbreecher/Data_Structures/Lectures/Example_Class_03_FileIO.java


://cs.clarku.edu/~jbreecher/Data_Structures/Lectures/Example_Class_03_Exceptions.java

5

Java Classes & Information Hiding

Mostly Review / Maybe Some New



Java Classes and Information Hiding

Classes and their Members:



Should all be review

Using a Class



Playing with objects:



null references, assignments, clones, equality

Parameters, Equals Methods, and Clones



Static methods



Parameters that are objects



A return value may be an object



So many other good things!!

6

An Example Class

This is the example in the book


starting on
page 66.


Location is the x, y coordinates of a point on a
graph.

For instance: (
-
1, 3) is shown here.


The location class allows us to shift (translate)
a Location object to a new position.

Here we see a shift( 2, 1 )


The location class also allows a clockwise
rotation of 90 degrees. Here we see a
rotate90()

X

X

X

S

R

7

An Example Class

Constructor for the Location

public
Location
(double

xInitial, double

yInitial)

Construct a Location with specified coordinates.

Parameters:

xInitial
-

the initial x coordinate of this Location

yInitial
-

the initial y coordinate of this Location

Postcondition:

This Location has been initialized at the given coordinates.

clone

public
Location

clone
()

Generate a copy of this Location.

Overrides:

clone in class java.lang.Object

Parameters:

-

-

none

Returns:

The return value is a copy of this Location. Subsequent changes to the copy will not
affect the original, nor vice versa. Note that the return value is a Location (not a
plain Object) which is permitted with Java 5.0.

8

An Example Class

distance

public static double
distance
(
Location

p1,
Location

p2)

Compute the distance between two Locations.

Parameters:

p1
-

the first Location p2
-

the second Location

Returns:

the distance between p1 and p2

Note:

The answer is Double.POSITIVE_INFINITY if the distance calculation overflows. The answer is
Double.NaN if either Location is null.

equals

public boolean
equals
(java.lang.Object

obj)

Compare this Location to another object for equality.

Overrides:

equals in class java.lang.Object

Parameters:

obj
-

an object with which this Location will be compared

Returns:

A return value of true indicates that obj refers to a Location object with the same
value as this Location. Otherwise the return value is false.

9

An Example Class

getX

public double
getX
()

Get the x coordinate of this Location.

Parameters:

-

-

none

Returns:

the x coordinate of this Location
.

getY

public double
getY
()

Get the y coordinate of this Location.

Parameters:

-

-

none


Returns:

the y coordinate of this Location.

midpoint

public static
Location

midpoint
(
Location

p1,
Location

p2)

Generate and return a Location halfway between two others.

Parameters:

p1
-

the first Location p2
-

the second Location

Returns:

a Location that is halfway between p1 and p2.

Note:

The answer is null if either p1 or p2 is null.

10

An Example Class

rotate90

public void
rotate90
()

Rotate this Location 90 degrees in a clockwise direction.

Parameters:

-

-

none

Postcondition:

This Location has been rotated clockwise 90 degrees around the origin.

shift

public void
shift
(double

xAmount, double

yAmount)

Move this Location by given amounts along the x and y axes.

Parameters:

xAmount
-

the amount to move this Location along the x axis

yAmount
-

the amount to move this Location along the y axis

Postcondition:

This Location has been moved by the given amounts along the two axes.

Note:

The shift may cause a coordinate to go above Double.MAX_VALUE or below the
minimum double value. In these cases, subsequent activations of getX or getY
will return Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY.

11

An Example Class

toString

public java.lang.String
toString
()

Generate a String representation of this Location.


Overrides:

toString in class java.lang.Object

Parameters:

-

-

none

Returns:

a String representation of this Location

12

An Example Class

package edu.colorado.geometry;


public class Location

implements Cloneable {


private double x; // The x coordinate of this Location


private double y; // The y coordinate of this Location



public Location

(double xInitial, double yInitial) {


x = xInitial;


y = yInitial;


}


public Location clone
( ) { // Clone a Location object.


Location answer;


try {


answer = (Location) super.clone( );


} catch (CloneNotSupportedException e)


{ // This exception should not occur. But if it does, it would probably


// indicate a programming error that made super.clone unavailable.


// A common error would be forgetting the "Implements Cloneable"


// clause at the start of this class.


throw new RuntimeException("class does not implement Cloneable.");


}


return answer;


}

13

An Example Class


public static double distance

(Location p1, Location p2) {


double a, b, c_squared;



// Check whether one of the locations is null.


if ((p1 == null) || (p2 == null))


return Double.NaN;




// Calculate differences in x and y coordinates.


a = p1.x
-

p2.x;


b = p1.y
-

p2.y;


// Use Pythagorean Theorem to calculate the square of the distance.


// between the locations.


c_squared = a*a + b*b;


return Math.sqrt(c_squared);


}




public boolean equals

(Object obj) {


if (obj instanceof Location) {


Location candidate = (Location) obj;


return (candidate.x == x) && (candidate.y == y);


} else


return false;


}


14

An Example Class


public double
getX
( ) {


return x;


}



public double getY( ) {


return y;


}




public static Location midpoint

(Location p1, Location p2) {


double xMid, yMid;




//Check whether one of the locations is null.


if ((p1 == null) || (p2 == null))


return null;


// Compute the x and y midpoints.


xMid = (p1.x/2) + (p2.x/2);


yMid = (p1.y/2) + (p2.y/2);




// Create a new location and return it.


Location answer = new Location(xMid, yMid);


return answer;


}

15

An Example Class


public void
rotate90
( ) {


double xNew;


double yNew;




// For a 90 degree clockwise rotations, the new x is the original y


// and the new y is
-
1 times the original x.


xNew = y;


yNew =
-
x;


x = xNew;


y = yNew;


}




public void
shift

(double xAmount, double yAmount) {


x += xAmount;


y += yAmount;


}




public String
toString

( ) {


return "(x=" + x + " y=" + y + ")";


}

} // End of class Locate