/* Author : Deepak Majeti Date : 8/10/2008 Program: LAB7_Mon, Interval_Test.java Report bugs to: mdeepak@iitk.ac.in */ /* Use Interval class to develop a program which will take as input the endpoints of two intervals I1 and I2 from command line. (Total four command line arguments). For examples, if the arguments are 3 1 2 4, then I1 = (1, 3) and I2 = (2, 4). The program should compute the following information about these intervals and print suitable messages. • The length of Intervals I1 and I2 respectively. • The interval which is bigger, that is, the interval whose length is greater than the other. • Whether any of the intervals I1 and I2 is empty. • Whether any of the intervals contains the origin. • Whether the intervals I1 and I2 intersect, if yes, print the the interval which is intersection of these two intervals. • Print the distance between the intervals I1 and I2 . */ class Interval_Test{ public static void main(String args[]){ Interval I1,I2; double l1=0,r1=0,l2=0,r2=0; try{// if the user does not enter a number as an argument, catch the error and let the user know what the error is. //it is better because the error generated by the compiler could be hard to understand by the users who do not know about the code l1=Double.parseDouble(args[0]); r1=Double.parseDouble(args[1]); l2=Double.parseDouble(args[2]); r2=Double.parseDouble(args[3]); }catch(Exception e){ System.out.println("Please give four numbers as arguments"); System.exit(0); } //make new Interval Objects I1=new Interval(l1,r1); I2=new Interval(l2,r2); System.out.println("The Intervals are:\nI1=["+I1.left+","+I1.right+"]\nI2=["+I2.left+","+I2.right+"]"); System.out.println("The length of interval I1= "+I1.Length()); System.out.println("The length of interval I2= "+I2.Length()); //check for lengths if(I1.Length()>I2.Length()) System.out.println("The length of interval I1 is greater than I2 "); else if(I2.Length()>I1.Length()) System.out.println("The length of interval I2 is greater than I1 "); else System.out.println("The length of interval I1 is equal to I2 "); //check if empty if(I1.Isempty()) System.out.println("I1 is empty"); //check if empty if(I2.Isempty()) System.out.println("I2 is empty"); //check if the interval has origin, "0" if(I1.Contains(0)) System.out.println("I1 contains the Origin"); //check if the interval has origin, "0" if(I2.Contains(0)) System.out.println("I2 contains the Origin"); //check if intervals intersect Interval temp=I1.intersection(I2); if(!(temp.Isempty())){ System.out.println("I1 and I2 Intersect"); System.out.println("The intersection Interval is I=["+temp.left+","+temp.right+"]"); } //calculate the distance between intervals System.out.println("The distance between I1 and I2= "+I1.Distance(I2)); } }