Exercise 1:
Write a class called Rational for perfoming arithmetic
with fractions.
Requisites:
- Use two integer variables to represent the private data
of the class: the numerator and the denominator. This values are
given in the constructor.
- Numerator and denominator should store the fraction in reduced form.
- Provide public methods for addition, subtraction, multiplication and
division of two numbers. All of them should provide results in reduced form.
- Provide a public method for printing in the form (a/b).
- Provide a public method to return its floating-point value.
Exercise 2:
The target of this exercise is to create classes to represent 2-D trajectories,
and provide methods to draw them.
We define a trajectory as a path or line in a two dimensional space or plane,
between two points.
Note that a trajectory is an infinite series of continous positions (x,y).
In this exercise, we will model the trajectories as 100 points joined with
straight lines. These 100 points will be distributed from the begining until the end of the
real trajectory.
To calculate the positions (x,y) of these 100 points, we will use
two functions/methods, called X and Y. These methods are parameterized with a
integer value N, ranging from 0 to 99, and they will return the coordenates of the
N-th point of the trajectory.
We have written a class, called Display, that implement
a window where we can draw. It has methods to draw lines, circles,
manage colors, and so on. This class can be obtained from
Display.java.
We will use this class to create the windows where we are going to draw.
CircularT.java is a class,
also provided by us, that implements circular trajectories. The constructor
of this class takes as parameter the radius of the circular trajectory.
The methods X and Y
calculate the position (x,y)
of the n-th trajectory sample.
The method Draw draws the trajectory in a given display
and centered in a given position.
TestT.java is a program that tests this class.
Compile these files and execute TestT.
Now, write the following new trajectory classes:
- LinealT: a straight line between two points.
The constructor will take as argument the coordenates of the two points.
public LinealT(double x0, double y0,
double x1, double y1)
- SpiralT: a spiral line.
The constructor will take as arguments the numbers of loops that the
spiral will have, and the distance between the starting and the ending
points of the spiral.
public SpiralT(double radious, double loops)
- LissajousT: a lissajous figure.
The constructor will take as arguments the size of the building box
containing the figure, and the vertical and horizontal frecuencies.
public LissajousT(double width, double height,
double fv, double fh)
Modify the TestT program to test the new classes.
These new classes must be written duplicating the file CircularT.java, and
modifying just the necessary Java code source lines. That is:
- You must create new instance variables, rewrite the constructor,
and rewrite the methods X and Y
for each type of trajectory.
- The method Draw should be copied without any modification.