import java.awt.Color; import uwcse.graphics.*; /** * Exercise 7.8 p 250 (a,b,c,d and g). Skip e and f. */ public class Exercise78 { /** * What is p implies q? * * @return p implies q */ public boolean implies(boolean p, boolean q) { // p=>q has the following truth table (see p 244) // p:true q:true p=>q:true // p:false q:true p=>q:true // p:true q:false p=>q:false // p:false q:false p=>q:true return !p || q; } /** * Are the three integers a, b and c in order (i.e. a<b<c)? * * @return a<b AND b<c */ public boolean inOrder(int a, int b, int c) { return (a < b && b < c); } /** * Does Rectangle r1 have a greater width than Rectangle r2? * * @return r1 has a greater width than r2 */ public boolean isWider(Rectangle r1, Rectangle r2) { return r1.getWidth() > r2.getWidth(); } /** * Is point (x1,y1) farther from the origin than point (x2,y2)? * * @return point 1 is farther from the origin than point 2 */ public boolean fartherFromOrigin(int x1, int y1, int x2, int y2) { return (x1 * x1 + y1 * y1 > x2 * x2 + y2 * y2); } /** * Are the three integers s1, s2 and s3 the lengths of the sides of a right * triangle? * * @return s1,s2 and s3 are the lengths of the sides of a right triangle */ public boolean areSidesOfRightTriangle(int s1, int s2, int s3) { // If any is negative, they can't be the lengths of a triangle if (s1 < 0 || s2 < 0 || s3 < 0) return false; // Hypotenuse and sides of the supposedly right triangle int side1 = s1; int side2 = s2; int h = s3; // The hypotenuse is the largest of the 3 integers if (h < side1) { // swap h and side1 int temp = h; h = side1; side1 = temp; } if (h < side2) { // swap h and side2 int temp = h; h = side2; side2 = temp; } // Is it a right triangle? return h * h == side1 * side1 + side2 * side2; } /** * Starts the application */ public static void main(String[] args) { Exercise78 e = new Exercise78(); System.out .println("e.implies(true, false) = " + e.implies(true, false)); System.out.println("e.inOrder(-5, 7, 9) = " + e.inOrder(-5, 7, 9)); Rectangle r1 = new Rectangle(0, 0, 10, 10, Color.GREEN, true); Rectangle r2 = new Rectangle(100, 50, 50, 5, Color.BLUE, false); System.out.println("e.isWider(r1, r2) = " + e.isWider(r1, r2)); System.out.println("e.fartherFromOrigin(10, 20, 1, 2) = " + e.fartherFromOrigin(10, 20, 1, 2)); System.out.println("e.areSidesOfRightTriangle(5, 3, 4) = " + e.areSidesOfRightTriangle(5, 3, 4)); } }