Saturday, November 26, 2011

IT13: exer1(my answer) JAVA language



Name: Janer, Jesson L.

Section: Set A
Time: TF(1:00-2:30)  
Exercise: 1

Code:

public class Exercise1{
public static void main (String[] args) {

       int x[] = {23, 6, 47, 35, 2, 14};

       num_classes.display_num (x);
       System.out.println ("\nSum of all integers: " +num_classes.sum_all(x));

       System.out.println ("\nAverage of a given integers: " +num_classes.get_ave (x));

       System.out.println ("\nLargest integer: " +num_classes.get_max (x));

       System.out.println ("\nSmallest integer: " +num_classes.get_min (x));

       System.out.println ("\nNumber of ODD integers: " +num_classes.get_no_of_odd (x));

       System.out.println ("\nNumber of EVEN integers: " +num_classes.get_no_of_even(x))
}
}

public class num_classes {
      public static void display_num(int x[]){
            for(int i=0; i < x.length; i++)
                  System.out.println (x[i]);
            }

      public static int sum_all(int x[]){
            int sum = 0;
            // type your solution here
            for(int i=0; i < x.length; i++){
            sum = sum + x[i];
                  }
            return sum;
            }

      public static int get_ave(int x[]){
            int ave = 0;
            // type your solution here
            int sum=0;
            for(int i=0; i < x.length; i++){
           sum=sum+x[i];
            }
           ave=sum/x.length;
           return ave;
            }

      public static int get_max(int x[]){
            int max = 0;
            // type your solution here
            for(int i=0; i < x.length; i++){
            if(x[i]>max)max=x[i];
                  }
                  return max;
            }

      public static int get_min(int x[]){
            int min = 0;
            // type your solution here
            for(int i=0; i < x.length; i++){
                  if(i==0)min=x[1];
                  if(x[i]<min)min=x[i];
                  }
                  return min;
            }

      public static int get_no_of_odd(int x[]){
            int n_odd = 0;
            // type your solution here
            for(int i=0; i < x.length; i++){
            if(x[i]%2!=0)n_odd++;
                  }
                  return n_odd;
            }

      public static int get_no_of_even(int x[]){
            int n_even = 0;
            // type your solution here
            for(int i=0; i < x.length; i++){
                if(x[i]%2==0)n_even++;
                  }
                  return n_even;
            }


}



1 comment: