doubles, variables, basic math Student approved
Remember the function to find the slope of a line given two points?
Divide the difference of the two y values (the rise) by the difference between the two x values (the run): (y2 - y1)/(x2 - x1)
Let's write a program to calculate the slope of a line for us, if we give it two points.
In the program, the points (x1, x2)
and (y1, y2)
are stored as four x1
, x2
, y1
, and y2
.
On line 9, write the general equation for a slope, in terms of x1
, x2
, y1
, and y2
. Then, run the program and check out the slope you just calculated!
class Slopes{
public static void main(String[] args){
double x1 = 3;
double y1 = 3;
double x2 = 4;
double y2 = 2;
double slope = // equation for slope between (x1, y1) and (y1, y2) here
System.out.println("The slope is " + slope);
}
}
The slope is -1