Monday, 15 January 2018

Throw, Thorws and Finally in Java

/*
 * throw, throws and finally together
 */
package corejava;

/**
 *
 * @author pavan
 */
public class ThrowThrowsDemo {

    void check() throws ArithmeticException{    // use throws to specify possible exceptions
        System.out.println("Inside check method");
        throw  new ArithmeticException();   // use throw to throw exception explicitely
    }
   

Divide by Zero Exception Handling in Java

/*
 * Exception handling Divide by zero
 */
package corejava;

/**
 *
 * @author pavan
 */
public class DivideByZero {

    public static void main(String args[]) {

        try {
            for (int i = 5; i >= 0; i--) {
                System.out.println(10 / i);
            }
        } catch (Exception e) {
            System.out.println("Exception occured .."+ e.getMessage());
            e.printStackTrace();
        }
    }
}

/*
OUUPUT

2
java.lang.ArithmeticException: / by zero
2
3
5
10
Exception occured ../ by zero
    at corejava.DivideByZero.main(DivideByZero.java:16)
*/

Array in Java

/*
 * One dimensional Array demonstration
 */
package corejava;

/**
 *
 * @author pavan
 */
public class ArrayDemo {
    int arr[] = new int[3]; // array creation, do not mention size on left hand side
   
    int arr2[] = {50,60};   //another way to initialize
   
    int arr3[][] = {{1,2,3},{4,5,6},{7,8,9}}; // declaring and initialiazing multi dimensional array
   
    void setArray(){    // array initialization
        arr[0] = 10;
        arr[1] = 20;
        arr[2] = 30;
    
     }

Interface in Java

/*
 * Interface
 */
package corejava;

/**
 *
 * @author pavan
 */

interface Bank{
    double getROI();
}

class AXIS implements Bank{
    public double getROI(){
        return 8.5;
    }
}

class HDFC implements Bank{
    public double getROI(){
        return 8.35;
    }
}

Abstract class in Java

/*
 * Abstract class and abstract method
 */
package corejava;

/**
 *
 * @author pavan
 */
abstract class Shape {

    abstract void draw();
}

class Circle extends Shape {

    void draw() {
        System.out.println("Drawing circle...");
    }
}

Single Level Inheritance in Java

/*
 * Interface
 */
package corejava;

/**
 *
 * @author pavan
 */

interface Bank{
    double getROI();
}

class AXIS implements Bank{
    public double getROI(){
        return 8.5;
    }
}

Static varible in Java

package corejava;

/*
 * With static members
 */

/**
 *
 * @author pavan
 */
public class WithStatic {
    static int counter = 0;    // only once memory will be given, rest of instance will share memory
   
    WithStatic(){
        counter++; 
        System.out.println(counter);
    }
   
    public static void main(String args[]){
        WithStatic ob1 = new WithStatic();
        WithStatic ob2 = new WithStatic();
        WithStatic ob3 = new WithStatic();
    }
}

/*
OUTPUT

1
2
3
*/

POJO - Plain Old Java Object Demonstration

/*
 * POJO - Plain Old Java Object
 * Link: https://www.quora.com/What-is-POJO-in-Java
 */
package corejava;

/**
 *
 * @author pavan
 */
public class POJO {
    int id;
    String name;
    String dept;
   
    int getId(){
        return id;
    }
   
    void setId(int id){
        this.id = id;
    }
   

Constructors in Java

/*
 * Demonstrating constructors
 */
package corejava;

/**
 *
 * @author pavan
 */
class Employee{
    int eId;
    String eName, eDept;
   
    Employee(){ //default contructor
        eId = 0;
        eName = "";
        eDept = "";
    }
   

Accept Input from User using Scanner class and BufferedReader class in Java

/*
 * Accept two numbers from user and perform addition
 * Classes used: Scanner, BufferedReader  
 * To know difference between both classes you may refer:
 * https://www.geeksforgeeks.org/difference-between-scanner-and-bufferreader-class-in-java/
 */
package corejava;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 *
 *
 * @author pavan
 */
public class AcceptInput {
    int num1, num2;
    void usingScannerClass(){
        Scanner scan = new Scanner(System.in);  // import java.util
        System.out.println("Using Scanner Class");
        System.out.println("Enter first number");
        num1 = scan.nextInt();  // implicit parsing
       
        System.out.println("Enter second number");
        num2 = scan.nextInt();
    }
   

Demonstrating Ternary Operator in Java

/*
 * Demonstrating ternary operator example
 */
package corejava;

/**
 *
 * @author pavan
 */
public class TernaryOperator {
    public static void main(String args[]){
        int num1 = 10;
        int num2 = 30;
        System.out.println("Greater No: "+((num1>num2)?num1:num2));
    }
   
}

/*
OUTPUT

Greater No: 30
*/

Variable Demo in Java

/*
 * Demonstrating local, instance and static varibles
 */
package corejava;

/**
 *
 * @author pavan
 */
public class VariablesDemo {
  
    int instanceVar =10;
    static int statVar = 20;
    public void checkValues(){
        int localVar = 30;
     
        System.out.println("Instance Var: "+ instanceVar);    // call to instance var
        System.out.println("Static Var: "+ statVar);    // call to static var
        System.out.println("Local Var: "+ localVar);    // call to local var
    }
    

Installation of jdk-1.8.xxx on Fedora

Dear Viewer

Follow the below mentioned steps to install / upgrade jdk version on your Linux box.


JDK1.8.0_151 installation steps

[pavan@Pavan opt]$ su
Password:
[root@Pavan opt]# pwd
/opt
[root@Pavan opt]#
[root@Pavan opt]# wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-linux-x64.tar.gz"
--2018-01-15 18:25:57-- http://download.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-linux-x64.tar.gz
Resolving download.oracle.com... 23.66.247.173
Connecting to download.oracle.com|23.66.247.173|:80... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://edelivery.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-linux-x64.tar.gz [following]
--2018-01-15 18:25:57-- https://edelivery.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-linux-x64.tar.gz
Resolving edelivery.oracle.com... 23.211.219.179, 2600:1417:2c:183::2d3e, 2600:1417:2c:190::2d3e
Connecting to edelivery.oracle.com|23.211.219.179|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: http://download.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-linux-x64.tar.gz?AuthParam=1516021067_f2db42d3f19a78350632a1e8f269fe63 [following]
--2018-01-15 18:25:58-- http://download.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-linux-x64.tar.gz?AuthParam=1516021067_f2db42d3f19a78350632a1e8f269fe63
Connecting to download.oracle.com|23.66.247.173|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 189736377 (181M) [application/x-gzip]
Saving to: `jdk-8u151-linux-x64.tar.gz'

100%[==========================================>] 189,736,377 401K/s in 9m 4s

2018-01-15 18:35:02 (340 KB/s) - `jdk-8u151-linux-x64.tar.gz' saved [189736377/189736377]

Three Days Hands-on Workshop on "Developing Web Applications using JSP, Servlet, AJAX, AngularJS and Modern Frameworks" during 18 - 20 Jan, 2018 @ Computer Dept, PICT Pune


Online registration link: https://goo.gl/forms/wBS8xKuF3AWcEffj1