| CSCI A201/A597Lab Notes 10 Spring 2000 |
The assignment asks you to define some procedures (methods).
First solve the problem by itself.
Then turn your solution into a procedure.
For example: computing the distance between two points.
How do you solve it?
Here's a picture:
An implementation:
import element.*;
public class Test {
public static void main(String[] args) {
Pt p, q;
p = new Pt(2, 4);
q = new Pt(6, 1);
double distance;
int h, v;
h = Math.abs(p.x() - q.x());
v = Math.abs(p.y() - q.y());
distance = Math.sqrt(h * h + v * v);
System.out.println(distance);
}
}
Then we isolate the part that calculate the distance and place it in a method.
import element.*;
public class Test {
public static void main(String[] args) {
Pt p, q;
p = new Pt(2, 4);
q = new Pt(6, 1);
double distance;
distance = Library.computeDistanceBetween(p, q);
System.out.println(distance);
}
}
class Library {
public static double computeDistanceBetween(Pt p, Pt q) {
// There was a typo here thanks to Jason Levy for catching it!
int h, v;
h = Math.abs(p.x() - q.x());
v = Math.abs(p.y() - q.y());
return Math.sqrt(h * h + v * v);
}
}
When we create a method we follow these steps:
We've done this in Test.java
Our procedure needs the two points. It should have as the result the distance between the two points, most likely a number with decimals.
We call the parameters, the two points,pandq. Their type isPt.
We
decided to call it computeDistanceBetween. Other names
are possible.
void (that is, as not returning a result).
Our method returns a double.
This is one of the 12 parts of assignment 8. (This is #5).
Expect to see more help posted here on assignment 8.