CS99

Fundamental Programming Concepts
Summer 2001

 

Lab 8: Solutions


Part I: class Triangle

class Triangle {    

   int a, b, c;    

   /* Default constructor */    
   public Triangle() {        
      a = b = c = 0;    
   }    

   /* Constructor */    
   public Triangle( int s1, int s2, int s3 ) {         
      a = s1;b = s2;c = s3;    
   }    

   /* Return the area of the triangle */    
   public double area() {        
      return 0.25*Math.sqrt( (a + b - c)*(a + c - b)*(b + c - a)*(a+b+c));    
   }    

   /* Return the perimeter of the triangle */    
   public int perimeter() {        
      return a + b + c;    
   }    

   /* Returns the angle opposite side a (in degrees) */    
   public double angleA() {        
      return Math.acos( (double) (b*b + c*c - a*a)/(2*b*c))*(180/Math.PI);    
   }    

   /* Return the angle opposite side b in degrees */    
   public double angleB() {        
      return Math.acos( (double) (a*a + c*c - b*b)/(2*a*c))*(180/Math.PI);    
   }    

   /* Return the angle opposite side c in degrees */    
   public double angleC() {        
      return Math.acos( (double) (b*b + a*a - c*c)/(2*b*a))*(180/Math.PI);    
   }    

   /* Return true if the triangle is a right triangle, false otherwise */    
   public boolean isRight() {        
      return ( a*a == b*b + c*c )||( b*b == a*a + c*c) || (c*c == a*a + b*b);    
   }    

   /* Return true if no two sides are the same length, false otherwise */    
   public boolean isScalene() {        
      return !isEquilateral() && !isIsosceles();    
   }    

   /* Return true if exactly two sides are the same length, false otherwise */    
   public boolean isIsosceles() {        
      return ( a == b || b == c || a == c ) && ( a!=c || b!=c || a!= b );    
   }    

   /* Return true if all three sides are the same length, false otherwise */    
   public boolean isEquilateral() {        
      return (a == b) && (b == c);    
   } 

class TriangleTester {    

   public static void main( String[] args ) {        
      TokenReader in = new TokenReader( System.in );        
      System.out.print("What is the length of the first side of your triangle? ");        
      int a = in.readInt();        
      System.out.print("What is the length of the second side of your triangle? ");        
      int b = in.readInt();        
      System.out.print("What is the length of the third side of your triangle? ");        
      int c = in.readInt();        
      Triangle t = new Triangle( a, b, c );        
      System.out.println("\nPondering...\n");        
      if ( t.isRight() )            
         System.out.println("Your triangle is a right triangle!");        
      else if ( t.isIsosceles() )            
         System.out.println("Your triangle is isosceles!");        
      else if ( t.isScalene() )            
         System.out.println("Your triangle is scalene!");        
      else if ( t.isEquilateral() )            
         System.out.println("Your triangle is equilateral!");        
      System.out.println("Its area is " +  t.area() +"." );        
      System.out.println("Its perimeter is " + t.perimeter() + "." );        
      System.out.println("Its angles are " + t.angleA() + ", " + t.angleB() +            
         ", " + t.angleC() + " degrees respectively. " );    
   } 

}


Part II: Playing With Numbers

class NumberTypes {    

   public static void main( String[] args) {        
      /* Print the number of type A integers <= 1,000,000 */        
      int numOfAs = 0;        
      int count = 1;        
      while ( count <= 1000000 ) {            
         if ( isTypeA( count ) )                
            numOfAs++;            
            count++;        
      }        
      System.out.println( "The number of type A integers <= 1,000,000 is " + numOfAs + "."  );        

      /* Print the number of type B integers in the range [100, 999]. */        
      int numOfBs = 0;        
      count = 100;        
      while ( count <= 999 ) {            
         if ( isTypeB( count ) )                
            numOfBs++;            
         count++;        
      }        
      System.out.println( "The number of type B integers in the range [100, 999] is " + numOfBs + "." );    
   }   

 /* Return true if parameter n is a type A integer */    
static boolean isTypeA( int n ) {        
   return (n%2)*(n%3)*(n%4)*(n%7) != 0;    
}    

/* Return true if parameter n is a type B integer */    
static boolean isTypeB( int n ) {        
   int ones = n%10;    // ones digit    
   int tens = (n/10)%10;        // tens digit
   int hundreds = (n/100)%10;       // hundreds digit 
   return ones == tens || tens == hundreds || ones == hundreds;    
  } 
}


Part III: Reversing A String

class Reverse {   

   public static void main( String[] args) {       
      String start = "abcdefghijklmnopqrstuvwxyz";       
      String end = reverse( start );       
      System.out.println( end );   
   }   

   static String reverse( String s ) {       
      /* Your String reverse code here */       
      String rev = "";       
      int n = s.length() - 1;       
      while ( n >= 0 ) {           
         rev += s.charAt( n-- );       
      }       
      return rev;   
   }

}


class EquationSolver {

   public static void main( String[] args ) {
       System.out.println("Enter the coefficients and constants for this system of equations:");
       System.out.println("\tax + by = c");
       System.out.println("\tdx + ey = f ");

       /* Accepting Input Portion */
       TokenReader in = new TokenReader( System.in );
       System.out.print("Enter a: " );
       int a = in.readInt();
       System.out.print("Enter b: " );
       int b = in.readInt();
       System.out.print("Enter c: " );
       int c = in.readInt();
       System.out.print("Enter d: " );
       int d = in.readInt();
       System.out.print("Enter e: " );
       int e = in.readInt();
       System.out.print("Enter f: " );
       int f = in.readInt();

       /* Processing data */
       double x = (double) det(c, b, f, e)/(det(a, b, d, e));
       double y = (double) det(a, c, d,  f)/(det(a, b, d, e));

       /* Printing output */
       System.out.println("\nThe solution to the system of equations:\n");
       System.out.println( a + "x + " + b + "y = " + c );
       System.out.println( d + "x + " + e + "y = " + f );
       System.out.println("\n\nis:\n");
       System.out.println("x = " +   x );
       System.out.println("y = " +   y);

   }


   /**
   *    Returns the determinant of the 2x2 matrix [a b; c d], or ad - bc.
   *    @param a b c d        The coefficients of the 2x2 matrix
   */
   public static int det( int a, int b, int c, int d ) {
       return a*d- b*c;
   }


}