import java.util.Scanner; /** * Segment maintains information about a line segment * represented by a left and right value. The line * segment knows it's length. * @author Sarah Heckman */ public class Segment { /** Left point of the segment */ private int left; /** Right point of the segment */ private int right; /** Length of the segment */ private int length; /** * Constructs a line segment * @param left left point of the segment * @param right right point of the segment */ public Segment(int left, int right) { this.left = left; this.right = right; } /** * Calculates the length of the segment and * stores the value in the length field. Also, * returns the length. * @return length of line segment */ public int calculateLength() { length = right - left; return length; } /** * Starts the program and contains the user * interface * @param args command line args */ public static void main(String [] args) { Scanner in = new Scanner(System.in); System.out.print("Enter left edge of segment 1: "); int l = in.nextInt(); System.out.print("Enter right edge of segment 1: "); int r = in.nextInt(); Segment seg1 = new Segment(l, r); System.out.print("Enter left edge of segment 2: "); l = in.nextInt(); System.out.print("Enter right edge of segment 2: "); r = in.nextInt(); Segment seg2 = new Segment(l, r); int len1 = seg1.calculateLength(); int len2 = seg2.calculateLength(); if (len1 == len2) { System.out.println("Segment lengths are equal"); } else { System.out.println("Segment lengths are NOT equal"); } } }