© Ron Dai 2019
R. DaiLearn Java with Mathhttps://doi.org/10.1007/978-1-4842-5209-3_17

17. Strong Typed Programming

Ron Dai1 
(1)
Seattle, WA, USA
 

As we have learned at the beginning of this book in Part I, the Java programming language has defined the integer, double, Boolean, String, etc., as basic types. In Java, we cannot assign any value to a variable without defining the variable with a type beforehand. Only after a variable has been defined clearly by a type—for example, integer—are we then allowed to assign an integer value to it and start using it as an integer in the calculation. Once a variable’s type is defined, it cannot be assigned a value with a different type, logically speaking. For instance, if a variable is defined as Boolean, it cannot be assigned an integer value. Otherwise, we will get a type mismatch compilation error.

Type Casting

However, if a variable is defined as a double, how do we assign an integer value to it? Let’s do some experimentation.
       public static void main(String[] args) {
              double a = 5;
              System.out.println(a);
              double b = 3 * 5;
              System.out.println(b);
              double x = 5 / 3;
              System.out.println(x);
              double y = (double)(5 / 3);
              System.out.println(y);
              double z = (double)5 / 3;
              System.out.println(z);
              double t = 5.0 / 3;
              System.out.println(t);
              double u = 5 / 3d;
              System.out.println(u);
       }

This is the output:

../images/485723_1_En_17_Chapter/485723_1_En_17_Figa_HTML.jpg
The following patterns are those we have learned from this experiment:
  • When the integer 5 is assigned to a double typed variable, the variable will get an equivalent value with a decimal point presentation, that is, double value 5.0.

  • When an integer is divided by another integer, the result follows the same integer type, for example, 5 / 3 = 1. But when the fraction “5 / 3” is assigned to a double typed variable, the resulting value will be automatically converted to double value 1.0.

  • (double)(5 / 3) converts an integer result of (5/ 3) to a double value. This is called type casting. The result is 1.0, a double value.

How do we produce a precise value from 5 / 3? The trick is to use (double)5 / 3, instead of 5 / 3. The outcome of (double)5 / 3 is a double value. Or, you may use 5.0 / 3 to generate the same outcome. Another way is 5d / 3, or 5 / 3d. The outcomes of both expressions are the same double value.

Math: Slope of a Line

In the x-y 2D Cartesian coordinates system, the slope of a line between points (x1, y1) and (x2, y2) is equal to (y2 - y1) / (x2 - x1).

Example

Implement a public method called double getSlope() , which returns the slope of a line. If the two points have the same x-coordinates, the denominator is zero and the slope is undefined, so you should throw an IllegalArgumentException in this case. This will stop your program running and show the specified error message.

Answer

In a Line class, we define two points and a constructor:
       private Point p1;
       private Point p2;
       public Line(Point p1, Point p2) {
              this.p1 = p1;
              this.p2 = p2;
       }
The Point class is designed as:
public class Point {
       private int x;
       private int y;
       public Point() {
       }
       public void setX(int x) {
              this.x = x;
       }
       public int getX() {
              return x;
       }
       public void setY(int y) {
              this.y = y;
       }
       public int getY() {
              return y;
       }
}
Now we add a method called getSlope() inside the Line class.
public double getSlope() {
       if (this.p1.getX() == this.p2.getX()) {
              throw new IllegalArgumentException("Denominator cannot be 0");
       }
       return (double)(this.p2.getY() - this.p1.getY()) / (this.p2.getX() - this.p1.getX());
}
The method looks easy, but the tricky part is where we convert the result of division between two integers to a double value, that is,
(double)(this.p2.getY() - this.p1.getY()) / (this.p2.getX() - this.p1.getX())

Math: Collinearity

Points are collinear if a straight line can be drawn to connect them. Two basic examples are when three points have the same x- or y-coordinate. The more general case can be determined by calculating the slope of the line between each pair of points and checking whether the slope is the same for all pairs of points.

We use the formula (y2 - y1) / (x2 - x1) to determine the slope between two points (x1, y1) and (x2, y2).

Add the following method to your Line class:
public boolean isCollinear(Point p)

It needs to return true if the given point is collinear with the points of this line.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.4.181