Pages

Sunday, 20 November 2016

Problem 42 - Contoh Scanner console

  1. import java.util.Scanner;  
  2. class ScannerTest{  
  3.  public static void main(String args[]){  
  4.    Scanner sc=new Scanner(System.in);  
  5.      
  6.    System.out.println("Enter your rollno");  
  7.    int rollno=sc.nextInt();  
  8.    System.out.println("Enter your name");  
  9.    String name=sc.next();  
  10.    System.out.println("Enter your fee");  
  11.    double fee=sc.nextDouble();  
  12.    System.out.println("Rollno:"+rollno+" name:"+name+" fee:"+fee);  
  13.    sc.close();  
  14.  }  
  15. }   


Problem 41 - Contoh Constructor Overload

class Language {
  String name;
 
  Language() {
    System.out.println("Constructor method called.");
  }
 
  Language(String t) {
    name = t;
  }
 
  public static void main(String[] args) {
    Language cpp  = new Language();
    Language java = new Language("Java");
 
    cpp.setName("C++");
 
    java.getName();
    cpp.getName();
  }
 
  void setName(String t) {
    name = t;
  }
 
  void getName() {
    System.out.println("Language name: " + name);
  }
}


Problem 40 - Linear Search

import java.util.Scanner;
 
class LinearSearch 
{
  public static void main(String args[])
  {
    int c, n, search, array[];
 
    Scanner in = new Scanner(System.in);
    System.out.println("Enter number of elements");
    n = in.nextInt(); 
    array = new int[n];
 
System.out.println("Enter " + n + " integers");   for (c = 0; c < n; c++) array[c] = in.nextInt();   System.out.println("Enter value to find"); search = in.nextInt();   for (c = 0; c < n; c++) { if (array[c] == search) /* Searching element is present */ { System.out.println(search + " is present at location " + (c + 1) + "."); break; } } if (c == n) /* Searching element is absent */ System.out.println(search + " is not present in array."); } }


Problem 39 - Binary Search

import java.util.Scanner;
 
class BinarySearch 
{
  public static void main(String args[])
  {
    int c, first, last, middle, n, search, array[];
 
    Scanner in = new Scanner(System.in);
    System.out.println("Enter number of elements");
    n = in.nextInt(); 
    array = new int[n];
 
    System.out.println("Enter " + n + " integers");
 
 
    for (c = 0; c < n; c++)
      array[c] = in.nextInt();
 
    System.out.println("Enter value to find");
    search = in.nextInt();
 
    first  = 0;
    last   = n - 1;
    middle = (first + last)/2;
 
    while( first <= last )
    {
      if ( array[middle] < search )
        first = middle + 1;    
      else if ( array[middle] == search ) 
      {
        System.out.println(search + " found at location " + (middle + 1) + ".");
        break;
      }
      else
         last = middle - 1;
 
      middle = (first + last)/2;
   }
   if ( first > last )
      System.out.println(search + " is not present in the list.\n");
  }
}


Problem 38 - Exception Handling

import java.util.Scanner;
class Exceptions {
  public static void main(String[] args) {
 
  String languages[] = { "C", "C++", "Java", "Perl", "Python" };
 
  try {
    for (int c = 1; c <= 5; c++) {
      System.out.println(languages[c]);
    }
  }
  catch (Exception e) {
    System.out.println(e);
  }
  }
}


Problem 37 - Mencari Substring dari String

import java.util.Scanner;
 
class SubstringsOfAString
{
   public static void main(String args[])
   {
      String string, sub;
      int i, c, length;
 
      Scanner in = new Scanner(System.in);
      System.out.println("Enter a string to print it's all substrings");
      string  = in.nextLine();
 
      length = string.length();   
 
      System.out.println("Substrings of \""+string+"\" are :-");
 
      for( c = 0 ; c < length ; c++ )
      {
         for( i = 1 ; i <= length - c ; i++ )
         {
            sub = string.substring(c, c+i);
            System.out.println(sub);
         }
      }
   }
}


Problem 36 - Perbandingan 2 String

import java.util.Scanner;
 
class CompareStrings
{
   public static void main(String args[])
   {
      String s1, s2;
      Scanner in = new Scanner(System.in);
 
      System.out.println("Enter the first string");
      s1 = in.nextLine();
 
      System.out.println("Enter the second string");
      s2 = in.nextLine();
 
      if ( s1.compareTo(s2) > 0 )
         System.out.println("First string is greater than second.");
      else if ( s1.compareTo(s2) < 0 )
         System.out.println("First string is smaller than second.");
      else   
         System.out.println("Both strings are equal.");
   }
}



Problem 35 - Penambahan dalam Matriks

import java.util.Scanner;
 
class AddTwoMatrix
{
   public static void main(String args[])
   {
      int m, n, c, d;
      Scanner in = new Scanner(System.in);
 
      System.out.println("Enter the number of rows and columns of matrix");
      m = in.nextInt();
      n  = in.nextInt();
 
      int first[][] = new int[m][n];
      int second[][] = new int[m][n];
      int sum[][] = new int[m][n];
 
      System.out.println("Enter the elements of first matrix");
 
      for (  c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            first[c][d] = in.nextInt();
 
      System.out.println("Enter the elements of second matrix");
 
      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            second[c][d] = in.nextInt();
 
      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
             sum[c][d] = first[c][d] + second[c][d];  //replace '+' with '-' to subtract matrices
 
      System.out.println("Sum of entered matrices:-");
 
      for ( c = 0 ; c < m ; c++ )
      {
         for ( d = 0 ; d < n ; d++ )
            System.out.print(sum[c][d]+"\t");
 
         System.out.println();
      }
   }
}

hasil:


Problem 34 - Static Block

class StaticBlock {
  public static void main(String[] args) {
    System.out.println("Main method is executed.");
  }
 
  static {
    System.out.println("Static block is executed before main method.");
  }
}


Problem 33 - Zona Waktu

  1. import java.util.Calendar;
  2. import java.util.TimeZone;
  3.  
  4. public class GetCurrentTimeZone {
  5.  
  6.   public static void main(String[] args) {
  7.    
  8.     //get Calendar instance
  9.     Calendar now = Calendar.getInstance();
  10.    
  11.     //get current TimeZone using getTimeZone method of Calendar class
  12.     TimeZone timeZone = now.getTimeZone();
  13.    
  14.     //display current TimeZone using getDisplayName() method of TimeZone class
  15.     System.out.println("Current TimeZone is : " + timeZone.getDisplayName());
  16.   }
  17. }

Problem 32 - Kabisat

  1. public class DetermineLeapYearExample {
  2.  
  3.         public static void main(String[] args) {
  4.                
  5.                 //year we want to check
  6.                 int year = 2004;
  7.                
  8.                 //if year is divisible by 4, it is a leap year
  9.                
  10.                 if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
  11.                         System.out.println("Year " + year + " is a leap year");
  12.                 else
  13.                         System.out.println("Year " + year + " is not a leap year");
  14.         }
  15. }

Problem 31 - Break

  1. public class JavaBreakExample {
  2.  
  3.   public static void main(String[] args) {
  4.     /*
  5.      * break statement is used to terminate the loop in java. The following example
  6.      * breaks the loop if the array element is equal to true.
  7.      *
  8.      * After break statement is executed, the control goes to the statement
  9.      * immediately after the loop containing break statement.
  10.      */
  11.      
  12.     int intArray[] = new int[]{1,2,3,4,5};
  13.    
  14.     System.out.println("Elements less than 3 are : ");
  15.     for(int i=0; i < intArray.length ; i ++)
  16.     {  
  17.       if(intArray[i] == 3)
  18.         break;
  19.       else
  20.         System.out.println(intArray[i]);
  21.     }
  22.    
  23.   }
  24. }

Problem 30 - Pyramid

  1. public class JavaPyramid3 {
  2.         public static void main(String[] args) {
  3.                
  4.                 for(int i=1; i<= 5 ;i++){
  5.                        
  6.                         for(int j=0; j < i; j++){
  7.                                 System.out.print("*");
  8.                         }
  9.                        
  10.                         //generate a new line
  11.                         System.out.println("");
  12.                 }
  13.                
  14.                 //create second half of pyramid
  15.                 for(int i=5; i>0 ;i--){
  16.                        
  17.                         for(int j=0; j < i; j++){
  18.                                 System.out.print("*");
  19.                         }
  20.                        
  21.                         //generate a new line
  22.                         System.out.println("");
  23.                 }
  24.         }
  25. }