## CS 1401, Quiz 9

Date: Monday, November 6, 2006
Name (please type legibly, ideally in block letters): ______________________________________________________________________

Motivation: One of the main uses of computers in calculus is to provide graphical description of different functions. The graphical representation of a graph y = f(x) is formed by different points with coordinates (x, y = f(x)).

1. Define a class Point in which each object has two real-valued coordinates x and y. Do not forget to include a constructor, accessor methods, and mutator methods.

2. In the main method, define an array of 100 points, in which the i-th point has coordinates x = i/100 and y = x^2.

3. Trace your code for the first two points, with x = 0 and y = 0, and with x = 0.01 and y = 0.0001.

Solution:

public class Point{
private double x;
private double y;

public Point(double ax, double ay){x = ax; y = ay;}

public double getX(){return x;}

public double getY(){return y;}

public void setX(double newX){x = newX;}

public void setY(double newY){y = newY;}
}

in the main program:

double x;
double y;
for(int i = 0; i < 100; i++)
{x = (double) i / 100.0; y = x * x;

tracing for i =0 and i = 1 :

First, we define a new array quadratic of objects of type Point:

----
---\
\
\|
- -------
-------
-------
...

Then, we have new variables x and y which are undefined (i.e., have
a garbage value):

-------   --------
|   ?   | |   ?    |
-------   --------
x          y

At the first step of the loop, we define a new variable i and assign
to it the value 0:

-------
|   0   |
-------
i

We then assign to x and y new values:

-------   --------
|  0.0  | |   0.0  |
-------   --------
x          y

We then call a constructor method:

--------------
|  ----------  |
| |   0.0    | |
|  ----------  |
|     ax       |
|  ----------  |
| |   0.0    | |
|  ----------  |
|     ay       |
--------------
|              |
|              |
|              |
|              |
|              |
|              |
--------------
Point

after which we create a new object:

--------------
|  ----------  |
| |   0.0    | |
|  ----------  |
|      x       |
|  ----------  |
| |   0.0    | |
|  ----------  |
|      y       |
--------------

Then, we increase i by 1:

-------
|   1   |
-------
i

We then assign to x and y new values:

-------   --------
|  0.01 | | 0.0001 |
-------   --------
x          y

call a constructor method:

--------------
|  ----------  |
| |  0.01    | |
|  ----------  |
|     ax       |
|  ----------  |
| |  0.0001  | |
|  ----------  |
|     ay       |
--------------
|              |
|              |
|              |
|              |
|              |
|              |
--------------
Point

and create a new object:

--------------
|  ----------  |
| |  0.01    | |
|  ----------  |
|      x       |
|  ----------  |
| |  0.0001  | |
|  ----------  |
|      y       |
--------------