- import java.util.Scanner;
- class ScannerTest{
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- System.out.println("Enter your rollno");
- int rollno=sc.nextInt();
- System.out.println("Enter your name");
- String name=sc.next();
- System.out.println("Enter your fee");
- double fee=sc.nextDouble();
- System.out.println("Rollno:"+rollno+" name:"+name+" fee:"+fee);
- sc.close();
- }
- }
Sunday, 20 November 2016
Problem 42 - Contoh Scanner console
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
- import java.util.Calendar;
- import java.util.TimeZone;
- public class GetCurrentTimeZone {
- public static void main(String[] args) {
- //get Calendar instance
- Calendar now = Calendar.getInstance();
- //get current TimeZone using getTimeZone method of Calendar class
- TimeZone timeZone = now.getTimeZone();
- //display current TimeZone using getDisplayName() method of TimeZone class
- System.out.println("Current TimeZone is : " + timeZone.getDisplayName());
- }
- }
Problem 32 - Kabisat
- public class DetermineLeapYearExample {
- public static void main(String[] args) {
- //year we want to check
- int year = 2004;
- //if year is divisible by 4, it is a leap year
- if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
- System.out.println("Year " + year + " is a leap year");
- else
- System.out.println("Year " + year + " is not a leap year");
- }
- }
Problem 31 - Break
- public class JavaBreakExample {
- public static void main(String[] args) {
- /*
- * break statement is used to terminate the loop in java. The following example
- * breaks the loop if the array element is equal to true.
- *
- * After break statement is executed, the control goes to the statement
- * immediately after the loop containing break statement.
- */
- int intArray[] = new int[]{1,2,3,4,5};
- System.out.println("Elements less than 3 are : ");
- for(int i=0; i < intArray.length ; i ++)
- {
- if(intArray[i] == 3)
- break;
- else
- System.out.println(intArray[i]);
- }
- }
- }
Problem 30 - Pyramid
- public class JavaPyramid3 {
- public static void main(String[] args) {
- for(int i=1; i<= 5 ;i++){
- for(int j=0; j < i; j++){
- System.out.print("*");
- }
- //generate a new line
- System.out.println("");
- }
- //create second half of pyramid
- for(int i=5; i>0 ;i--){
- for(int j=0; j < i; j++){
- System.out.print("*");
- }
- //generate a new line
- System.out.println("");
- }
- }
- }