Advanced   Java   Services JAVA-Praktikum Back Next Up Home
Einfache Methoden (Lösung)
// ------------------------------ SimpleMethods.java ------------------------------- \\

public class SimpleMethods
{
   public static void main(String args[])
   {
      double a, b, c ;

      a = 100*Math.random() ;
      b = 100*Math.random() ;
      c = 100*Math.random() ;

      System.out.println("a = " +a);
      System.out.println("b = " +b);
      System.out.println("c = " +c);
      System.out.println("max = " +  SimpleMethods.max(a, b, c) );
      System.out.println("min = " +  SimpleMethods.min(a, b, c) );

   }  // end main


   public static int max(int a, int b, int c)
   {
      int max=a ;

      if(b>max) max=b;
      if(c>max) max=c;

      return max ;
   }

   public static double max(double a, double b, double c)
   {
      double max=a ;

      if(b>max) max=b;
      if(c>max) max=c;

      return max ;
   }

   public static int min(int a, int b, int c)
   {
      int min=a ;

      if(b<min) min=b;
      if(c<min) min=c;

      return min ;
   }

   public static double min(double a, double b, double c)
   {
      double min=a ;

      if(b<min) min=b;
      if(c<min) min=c;

      return min ;
   }

   public static int max(int[] arr)
   {
      int max=arr[0] ;

      for(int i=1 ; i<arr.length; i++)
         if( arr[i]>max) max=arr[i];

      return max ;
   }

   public static double max(double[] arr)
   {
      double max=arr[0] ;

      for(int i=1 ; i<arr.length; i++)
         if( arr[i]>max) max=arr[i];

      return max ;
   }

   public static int min(int[] arr)
   {
      int min=arr[0] ;

      for(int i=1 ; i<arr.length; i++)
         if( arr[i]<min) min=arr[i];

      return min ;
   }

   public static double min(double[] arr)
   {
      double min=arr[0] ;

      for(int i=1 ; i<arr.length; i++)
         if( arr[i]<min) min=arr[i];

      return min ;
   }

   public static double compute(double a, double b, char ch)
   {
      switch(ch)
      {
         case '+' : return a+b ;
         case '-' : return a-b ;
         case '*' : return a*b ;
         case '/' : return b!=0 ? a/b : Double.POSITIVE_INFINITY ;
      }
      return Double.NaN ;
   }

}  // end class
Valid XHTML 1.0 Strict top Back Next Up Home