Saturday, November 26, 2011

IT13: exer2(my answer) JAVA language


Name: Janer, Jesson L.
Section: Set A
Time: TF(1:00-2:30)  
Exercise: 1

Code:

public class exercise2{
      public static void main (String[] args) {
           
            String msg = "Hello World, This is a test ! :)";
            string_classes.display_msg (5,msg);

System.out.println ("\nThe reverse string is: " +string_classes.reverse_msg (msg));
System.out.println ("\nThe no of vowels in a string is: "  +string_classes.no_of_vowels (msg));
System.out.println ("\nThe no of consonants in a string is:" +string_classes.no_of_consonants(msg));
System.out.println ("\nThe no of special characters in a string is: "  +string_classes.no_of_special_chars (msg));
System.out.println ("\nThe no of characters occurences:  ");
string_classes.char_occurences (msg);
      }
}

public class string_classes {
            public static void display_msg(int x, String msg){
                  for(int i = 0; i < x ; i++){
                        System.out.println ("\n" +msg);
                        }
                  }

            public static String reverse_msg(String msg){
                  String str_reverse = " ";
                  //Enter your solution here.....
                 str_reverse = new StringBuffer(msg).reverse().toString();
                 return str_reverse;
                  }

            public static int no_of_vowels(String msg){
                  int nvowels = 0;
                  //Enter your solution here.....
                  String tester="aeiou";
             for(int a=0;a<msg.length();a++){
                   for(int b=0;b<tester.length();b++){
                     if((msg.toLowerCase()).charAt(a)==(tester.charAt(b)))nvowels++;
                              }
                }
                     return nvowels;
                  }


            public static int no_of_consonants(String msg){
                  int nconsonants = 0;
                  //Enter your solution here.....
              String tester="bcdfghjklmnpqrstvwxyz";
              for(int a=0;a<msg.length();a++){
                  for(int b=0;b<tester.length();b++){
                   if((msg.toLowerCase()).charAt(a)==(tester.charAt(b)))nconsonants++;
                    }
                }
                        return nconsonants;
                  }

            public static int no_of_special_chars(String msg){

                  int sp_chars = 0;
                  //Enter your solution here.....
                  String tester="~`!@#$%^&*()-_+={}[];:',.></?|";
                for(int a=0;a<msg.length();a++){
                   for(int b=0;b<tester.length();b++){
                     if((msg.toLowerCase()).charAt(a)==(tester.charAt(b)))sp_chars++;
                              }
                   }
                        return sp_chars;
                  }


            public static void char_occurences(String msg){
            //Enter your code here...
            /* Sample output: Hello World, This is a test ! :
             *H - 2, e - 2, l - 3, o - 2, W - 1, r - 1 d - 1,
             *T - 3, i - 2, s - 3, a - 1
             *
             */
    String tester="abcdefghijklmnopqrstuvwxyz";
    boolean t=true,th=true;
    int no=0;
    String char_passed=" ";
          
    System.out.println(msg);
          
    for(int a=0;a<msg.length();a++){
          
            for(int b=0;b<tester.length();b++){
           if(msg.toLowerCase().charAt(a)==tester.charAt(b)){
                 t=true;
                 b=27;
                }else{
                 t=false;
                }
           }
          
            if(t==true){
        for(int c=0;c<msg.length();c++){
           if(msg.toLowerCase().charAt(a)==msg.toLowerCase().charAt(c))no++;
             }
                    
        for(int d=0;d<char_passed.length();d++){
           if(msg.toLowerCase().charAt(a)==char_passed.charAt(d)){
                th=false;
                d=100;
                }
           }
                  
           if(th==true){
               System.out.println(msg.charAt(a)+ "-"+no);
               char_passed=char_passed+msg.toLowerCase().charAt(a);
                   }
          }
                    t=true;
                    th=true;
                    no=0;

     }

       }

}

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;
            }


}