Monday, 7 August 2017

SORTED SET



package java_topic.collection;

import java.util.SortedSet;
import java.util.TreeSet;

/**
 * Created by Akash on 8/7/2017.
 */
public class SortedSetClass {

   
public static void main(String[] args)
    {
        SortedSet sortedSet1 =
new TreeSet();

        sortedSet1.add(
1);
        sortedSet1.add(
2);
        sortedSet1.add(
3);
        sortedSet1.add(
4);
        sortedSet1.add(
5);
        sortedSet1.add(
6);
        sortedSet1.add(
7);
        sortedSet1.add(
8);
        sortedSet1.add(
9);
        sortedSet1.add(
10);

        System.
out.println("Full SortedSet : "+sortedSet1);

        System.
out.println("first() SortedSet : "+sortedSet1.first());

        System.
out.println("last() SortedSet : "+sortedSet1.last());

        System.
out.println("headset(Object fromElement) SortedSet : "+sortedSet1.headSet(4));

        System.
out.println("subset(Object fromElement, Object toElement) SortedSet : "+sortedSet1.subSet(3, 7));

        System.
out.println("tailSet(Object toElement) SortedSet : "+ sortedSet1.tailSet(6));

    }
}

************************************************************************
OUTPUT

Full SortedSet : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
first() SortedSet : 1
last() SortedSet : 10
headset(Object fromElement) SortedSet : [1, 2, 3]
subset(Object fromElement, Object toElement) SortedSet : [3, 4, 5, 6]

tailSet(Object toElement) SortedSet : [6, 7, 8, 9, 10]

Friday, 17 February 2017

Inner & Nested Class : LocalInnerClassWithVar



public class LocalInnerClassWithVar {

    int val = 50;    public void display()
    {
        int val = 100;        class Local{
            void msg()
            { System.out.println( "Hi, This is local inner class value : "+val );}
        }

        Local l = new Local();        l.msg();    }

    public static void main(String[] args)
    {
        LocalInnerClassWithVar localInnerClass = new LocalInnerClassWithVar();        localInnerClass.display();    }
}

Inner & Nested Class : NestedInterfaceWithinClass





class ABC
{
    interface XYZ{
        void msg();    }

}
public class NestedInterfaceWithinClass implements ABC.XYZ{

    @Override    public void msg()
    {
        System.out.println( "Hi, This is nested interface within class." );    }
    public static void main(String[] args)
    {
        ABC.XYZ nestedInterface1 = new NestedInterfaceWithinClass();        nestedInterface1.msg();    }
}

Inner & Nested Class : NestedInterface


interface D
{
    interface E{
     public void msg();    }

}
public class NestedInterface implements D.E{

    @Override    public void msg()
    {
        System.out.println( "Hi, This is nested interface." );    }
    public static void main(String[] args)
    {
        NestedInterface nestedInterface = new NestedInterface();        nestedInterface.msg();    }
}

Inner & Nested Class : StaticInnerClassWithStaticMethod




public class StaticInnerClassWithStaticMethod {

    static class InnerClass2
    {
        static void dispData1()
        {
            System.out.println( "Hi, This is static inner class with static method" );        }
    }
    public static void main(String[] args)
    {

        StaticInnerClassWithStaticMethod.InnerClass2 innerClass = new StaticInnerClassWithStaticMethod.InnerClass2();        innerClass.dispData1();        // OR, CAN CALL LIKE BELOW        StaticInnerClassWithStaticMethod.InnerClass2.dispData1();    }
}

Inner & Nested Class : StaticInnerClass




public class StaticInnerClass {

    static class InnerClass
    {
        public void dispData()
        {
            System.out.println( "Hi, This is static inner class" );        }
    }
    public static void main(String[] args)
    {

        StaticInnerClass.InnerClass innerClass = new StaticInnerClass.InnerClass();        innerClass.dispData();    }
}

Inner & Nested Class : LocalInnerClass



public class LocalInnerClass {

    public void display()
    {
        class Local{
            void msg()
            { System.out.println( "Hi, This is local inner class" );}
        }

        Local l = new Local();        l.msg();    }

    public static void main(String[] args)
    {
        LocalInnerClass localInnerClass = new LocalInnerClass();        localInnerClass.display();    }
}

Inner & Nested Class : AnonymousClassWithInterface



interface B
{
     void disp();}
public class AnonymousClassWithInterface {

    public static void main(String[] args)
    {

        B a1 = new B(){
            @Override            public void disp() {
                System.out.println( "Hi, This is anonymous interface ." );            }
        };        a1.disp();    }
}

Inner & Nested Class : AnonymousClass



abstract class A
{
    abstract void disp();}
public class AnonymousClass {

    public static void main(String[] args)
    {

        A a1 = new A(){
            @Override            public void disp() {
                System.out.println( "Hi, This is anonymous clasds ." );            }
        };        a1.disp();    }
}

Inner & Nested Class : MemberClass


public class MemberClass {

    class InnerClass
    {
        public void dispData()
        {
            System.out.println( "Hi, This is inner class" );        }
    }
    public static void main(String[] args)
    {
        MemberClass memberClass = new MemberClass();        MemberClass.InnerClass innerClass = memberClass.new InnerClass();        innerClass.dispData();    }
}





22

Default Method : Access other method within default method



 interface Summable {
    int getA();
    int getB();
    default int calculateSum() {
        return getA() + getB();    }
}

public class AccessOtherMethod implements Summable {
    @Override    public int getA() {
        return 1;    }

    @Override    public int getB() {
        return 2;    }

    public static void main(String[] args)
    {
        AccessOtherMethod accessOtherMethod = new AccessOtherMethod();        System.out.println( accessOtherMethod.getA() );        System.out.println( accessOtherMethod.getB() );        System.out.println( accessOtherMethod.calculateSum() );

    }
}

Default Method : Override






 interface Fooable {
    default int foo() {return 3;}
}

public class OverrideDefaultMethod extends Object implements Fooable {
    @Override    public int foo() {
        //return super.foo() + 1; //error: no method foo() in java.lang.Object        return Fooable.super.foo() + 1; //okay, returns 4    }

    public static void main(String[] args)
    {
        OverrideDefaultMethod overrideDefaultMethod = new OverrideDefaultMethod();        System.out.println( overrideDefaultMethod.foo() );    }
}

Default Method : Simple Program

interface A
{
    public void disp();    default void def()
    {
        System.out.println( "default() method called" );    }
}
public class DefaultMethod implements A {

    @Override    public void disp()
    {
        System.out.println( "disp() method called" );    }

    public static void main(String[] args)
    {
        DefaultMethod defaultMethod = new DefaultMethod();
        defaultMethod.disp();        defaultMethod.def();
    }
}

Wednesday, 15 February 2017

Math : UlpMethod

public class UlpMethod {

    public static void main(String[] args) {

        // get two double numbers numbers        double x = 956.294;        double y = 123.1;
        // print the ulp of these doubles        System.out.println("Math.ulp(" + x + ")=" + Math.ulp(x));        System.out.println("Math.ulp(" + y + ")=" + Math.ulp(y));    }
}

Math : ToRadiansMethod

public class ToRadiansMethod {

    public static void main(String[] args) {

        // get two double numbers numbers        double x = 45;        double y = -180;
        // convert them in radians        x = Math.toRadians(x);        y = Math.toRadians(y);
        // print the hyperbolic tangent of these doubles        System.out.println("Math.tanh(" + x + ")=" + Math.tanh(x));        System.out.println("Math.tanh(" + y + ")=" + Math.tanh(y));    }
}

Math : ToIntExactMethod

public class ToIntExactMethod {

    public static void main(String[] args) {

        // initialize long variables        long value1 = 123l;        long value2 = 3147483647l;

        // direct conversion from long to int        int intVal1 = (int) value1;        int intVal2 = (int) value2;
        // print the result        System.out.println("Direct conversion val1:"+intVal1);        System.out.println("Direct conversion val2:"+intVal2);

        // use Math.toIntExact()        try{
            intVal1 = Math.toIntExact(value1);            System.out.println("Using toIntExact val1:"+intVal1);        }catch(ArithmeticException e){
            System.out.println("value1 overflows an int");        }

        try{
            intVal2 = Math.toIntExact(value2);            System.out.println("Using toIntExact val2:"+intVal2);        }catch(ArithmeticException e){
            System.out.println("value2 overflows an int");        }


    }
}

Math : ToDegreesMethod

public class ToDegreesMethod {

    public static void main(String[] args) {

        // get two double numbers numbers        double x = 45;        double y = -180;
        // convert them in degrees        x = Math.toDegrees(x);        y = Math.toDegrees(y);
        // print the hyperbolic tangent of these doubles        System.out.println("Math.tanh(" + x + ")=" + Math.tanh(x));        System.out.println("Math.tanh(" + y + ")=" + Math.tanh(y));    }
}

Math : TanMethod

public class TanMethod {

    public static void main(String[] args) {

        // get two double numbers numbers        double x = 45;
        // convert them to radians        x = Math.toRadians(x);
        // print the trigonometric sine for these doubles        System.out.println("Math.tan(" + x + ")=" + Math.tan(x));        System.out.println("Math.tanh(" + x + ")=" + Math.tanh(x));    }
}

Math : SubstractExactMethod


public class SubstractExactMethod {

    public static void main(String[] args) {

        // Ask for user input

        // use scanner to read the console input        Scanner scan = new Scanner(System.in);
        // Assign the user to String variable        System.out.print("Enter X value input:");        String s = scan.nextLine();        System.out.print("Enter Y value input:");        String s1 = scan.nextLine();
        // close the scanner object        scan.close();
        // convert the string input to double        int valueX = Integer.parseInt(s);        int valueY = Integer.parseInt(s1);
        // get the result of decrementExact        int result = Math.subtractExact(valueX, valueY);        System.out.println("Result of the operation is " + result);
    }
}

Math : SqrtMethod

public class SqrtMethod {

    public static void main(String[] args) {

        // get two double numbers numbers        double x = 9;        double y = 25;
        // print the square root of these doubles        System.out.println("Math.sqrt(" + x + ")=" + Math.sqrt(x));        System.out.println("Math.sqrt(" + y + ")=" + Math.sqrt(y));    }
}

Math : SinMethod

public class SinMethod {

    public static void main(String[] args) {

        // get two double numbers numbers        double x = 45;
        // convert them to radians        x = Math.toRadians(x);
        // print the trigonometric sine for these doubles        System.out.println("Math.sin(" + x + ")=" + Math.sin(x));        System.out.println("Math.sinh(" + x + ")=" + Math.sinh(x));    }
}

Math : SignumMethod

public class SignumMethod {

    public static void main(String[] args) {

        // get two float numbers        float x = 50.14f;        float y = -4f;
        // call signum for both floats and print the result        System.out.println("Math.signum(" + x + ")=" + Math.signum(x));        System.out.println("Math.signum(" + y + ")=" + Math.signum(y));    }
}

Math : ScalbMethod

public class ScalbMethod {

    public static void main(String[] args) {

        // get a x to be raised        float x = 50.14f;        int y = 4;
        // calculate x multiplied by 2 raised in y        System.out.println("Math.scalb(" + x + "," + y + ")=" + Math.scalb(x, y));    }
}

Math : RoundMethod

public class RoundMethod {

    public static void main(String args[]) {
        double d = 100.675;        double e = 100.500;        float f = 100;        float g = 91f;
        System.out.println(Math.round(d));        System.out.println(Math.round(e));        System.out.println(Math.round(f));        System.out.println(Math.round(g));    }
}

Math : RintMethod

public class RintMethod {

    public static void main(String[] args) {

        // get two double numbers        double x = 1654.9874;        double y = -9765.134;
        // find the closest integers for these double numbers        System.out.println("Math.rint(" + x + ")=" + Math.rint(x));        System.out.println("Math.rint(" + y + ")=" + Math.rint(y));    }
}

Math : RandomMethod

public class RandomMethod {

    public static void main(String[] args) {

        // get two random double numbers        double x = Math.random();        double y = Math.random();
        // print the numbers and print the higher one        System.out.println("Random number 1  :" + x);        System.out.println("Random number 2  :" + y);        System.out.println("Highest number   :" + Math.max(x, y));    }
}

Math : PowMethod



public class PowMethod {

    public static void main(String[] args) {

        // get two double numbers        double x = 2.0;        double y = 5.4;
        // print x raised by y and then y raised by x        System.out.println("Math.pow(" + x + "," + y + ")=" + Math.pow(x, y));        System.out.println("Math.pow(" + y + "," + x + ")=" + Math.pow(y, x));    }
}

Math : NextDownUpMethod



public class NextDownUpMethod {

    public static void main(String[] args) {

        // get two double numbers        double y = 154.28764;

        // print the next number for y towards x        System.out.println("Math.nextDown(y)="                + Math.nextDown(y));
        // print the next number for x towards y        System.out.println("Math.nextUp(y))="                + Math.nextUp(y));    }
}

Math : NextAfterMethod




public class NextAfterMethod {

    public static void main(String[] args) {

        // get two double numbers        double x = 98759.765;        double y = 154.28764;
        // print the next number for x towards y        System.out.println("Math.nextAfter(" + x + "," + y + ")="                + Math.nextAfter(x, y));
        // print the next number for y towards x        System.out.println("Math.nextAfter(" + y + "," + x + ")="                + Math.nextAfter(y, x));    }
}

Math : NegateExactMethod



public class NegateExactMethod {

    public static void main(String[] args) {

        // Ask for user input        System.out.print("Enter an input:");
        // use scanner to read the console input        Scanner scan = new Scanner(System.in);
        // Assign the user to String variable        String s = scan.nextLine();
        // close the scanner object        scan.close();
        // convert the string input to double        int value = Integer.parseInt(s);
        // get the result of decrementExact        int result = Math.negateExact(value);        System.out.println("Result of the operation is " + result);
    }
}

Math : MultiplyExactMethod



public class MultiplyExactMethod {

    public static void main(String[] args) {

        // Ask for user input

        // use scanner to read the console input        Scanner scan = new Scanner(System.in);
        // Assign the user to String variable        System.out.print("Enter X value input:");        String s = scan.nextLine();        System.out.print("Enter Y value input:");        String s1 = scan.nextLine();
        // close the scanner object        scan.close();
        // convert the string input to double        int valueX = Integer.parseInt(s);        int valueY = Integer.parseInt(s1);
        // get the result of decrementExact        int result = Math.multiplyExact(valueX, valueY);        System.out.println("Result of the operation is " + result);
    }
}

Math : max, min



public class MaxMinMethod {

    public static void main(String[] args) {

        // get two integer numbers        int x = 60984;        int y = 1000;
        // print the larger number between x and y        System.out.println("Math.max(" + x + "," + y + ")=" + Math.max(x, y));        System.out.println("Math.min(" + x + "," + y + ")=" + Math.min(x, y));    }
}

Math : log




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

        // get two double numbers        double x = 60984.1;        double y = -497.99;
        // get the natural logarithm for x        System.out.println("Math.log(" + x + ")=" + Math.log(x));
        // get the natural logarithm for y        System.out.println("Math.log(" + y + ")=" + Math.log(y));
        // ---log10
        // get two double numbers        double x1 = 60984.1;        double y1 = 1000;
        // get the base 10 logarithm for x        System.out.println("Math.log10(" + x1 + ")=" + Math.log10(x1));
        // get the base 10 logarithm for y        System.out.println("Math.log10(" + y1 + ")=" + Math.log10(y1));
        // ---log1p        // get two double numbers        double x2 = 60984.1;        double y2 = 1000;
        // call log1p and print the result        System.out.println("Math.log1p(" + x2 + ")=" + Math.log1p(x2));
        // call log1p and print the result        System.out.println("Math.log1p(" + y2 + ")=" + Math.log1p(y2));    }
}

Math : incrementExact





public class IncrementExact {

    public static void main(String[] args) {

        // Ask for user input        System.out.print("Enter an input:");
        // use scanner to read the console input        Scanner scan = new Scanner(System.in);
        // Assign the user to String variable        String s = scan.nextLine();
        // close the scanner object        scan.close();
        // convert the string input to double        int value = Integer.parseInt(s);
        // get the result of decrementExact        int result = Math.incrementExact(value);        System.out.println("Result of the operation is " + result);
    }

}

Math : IEEE Remainder






public class IeeeRenainderMethod {

    public static void main(String[] args) {

        // get two double numbers        double x = 60984.1;        double y = -497.99;
        // get the remainder when x/y        System.out.println("Math.IEEEremainder(" + x + "," + y + ")="                + Math.IEEEremainder(x, y));
        // get the remainder when y/x        System.out.println("Math.IEEEremainder(" + y + "," + x + ")="                + Math.IEEEremainder(y, x));    }
}

Math : hypot





public class HypotMethod {

        public static void main(String[] args) {

            // get two double numbers            double x = 60984.1;            double y = -497.99;
            // call hypot and print the result            System.out.println("Math.hypot(" + x + "," + y + ")=" + Math.hypot(x, y));        }
}

Math : getExponent




public class GetExponentMethod {

    public static void main(String[] args) {

        // get two double numbers        double x = 60984.1;        double y = -497.99;
        // print the unbiased exponent of them        System.out.println("Math.getExponent(" + x + ")=" + Math.getExponent(x));        System.out.println("Math.getExponent(" + y + ")=" + Math.getExponent(y));        System.out.println("Math.getExponent(0)=" + Math.getExponent(0));    }
}

Math : floor




public class FloorMethod {

    public static void main(String[] args) {
        double d = 100.675;        float f = -90.65f;
        System.out.println(Math.floor(d));        System.out.println(Math.floor(f));
        int result = Math.floorDiv(9, 2);        System.out.println(result);
        // The remainder of 10 modulo 6 is 4.        int result1 = Math.floorMod(10, 6);        int result2 = 10 % 6;        System.out.println(result1);        System.out.println(result2);
        // Use negative numbers mixed with positive numbers.        // ... These are different with floorMod.        int result3 = Math.floorMod(10, -6);        int result4 = 10 % -6;        System.out.println(result3);        System.out.println(result4);    }
}

Math : exp




public class ExpMethod {

    public static void main(String[] args) {

        // get two double numbers        double x = 5;        double y = 0.5;
        // print e raised at x and y        System.out.println("Math.exp(" + x + ")=" + Math.exp(x));        System.out.println("Math.exp(" + y + ")=" + Math.exp(y));    }
}

Math : decrementExact



public class DecrementExact {

    public static void main(String[] args) {

        // Ask for user input        System.out.print("Enter an input:");
        // use scanner to read the console input        Scanner scan = new Scanner(System.in);
        // Assign the user to String variable        String s = scan.nextLine();
        // close the scanner object        scan.close();
        // convert the string input to double        int value = Integer.parseInt(s);
        // get the result of decrementExact        int result = Math.decrementExact(value);        System.out.println("Result of the operation is " + result);
    }

}

Math : cbrt





public class CubeRoot {

    public static void main(String[] args) {

        // get two double numbers        double x = 125;        double y = 10;
        // print the cube roots of three numbers        System.out.println("Math.cbrt(" + x + ")=" + Math.cbrt(x));        System.out.println("Math.cbrt(" + y + ")=" + Math.cbrt(y));        System.out.println("Math.cbrt(-27)=" + Math.cbrt(-27));
    }

}

Math : cos




public class CosMethod {


    public static void main(String args[]) {
        double degrees = 45.0;        double radians = Math.toRadians(degrees);
        System.out.format("The value of pi is %.4f%n", Math.PI);        System.out.format("The cosine of %.1f degrees is %.4f%n", degrees, Math.cos(radians));    }

}

Math : cosh




public class CoshMethod {

    public static void main(String[] args) {

        // get two double numbers        double x = 45.0;        double y = 180.0;
        // convert them to radians        x = Math.toRadians(x);        y = Math.toRadians(y);
        // print their hyperbolic cosine        System.out.println("Math.cosh(" + x + ")=" + Math.cosh(x));        System.out.println("Math.cosh(" + y + ")=" + Math.cosh(y));    }
}

Math : copysign



public class CopySign {

    public static void main(String[] args) {

        // get two double numbers        double x = 125.9;        double y = -0.4873;
        // print a double with the magnitude of x and the sign of y        System.out.println("Math.copySign(" + x + "," + y + ")=" + Math.copySign(x, y));
        // print a double with the magnitude of y and the sign of x        System.out.println("Math.copySign(" + y + "," + x + ")=" + Math.copySign(y, x));
    }

}

Math : ceil




public class CeilMethod {

    public static void main(String[] args) {

        // get two double numbers        double x = 125.9;        double y = 0.4873;
        // call ceal for these these numbers        System.out.println("Math.ceil(" + x + ")=" + Math.ceil(x));        System.out.println("Math.ceil(" + y + ")=" + Math.ceil(y));        System.out.println("Math.ceil(-0.65)=" + Math.ceil(-0.65));
    }
}

Tuesday, 14 February 2017

Math : atan()



public class Atan {

    public static void main(String[] args)
    {
        double degrees = 45.0;        double radians = Math.toRadians(degrees);
        System.out.format("The value of pi is %.4f%n", Math.PI);        System.out.format("The arccosine of %.4f is %.4f degrees %n", Math.tan(radians),                Math.toDegrees(Math.atan(Math.sin(radians))));    }
}

Math : asin




public class Asin {

    public static void main(String[] args)
    {
        double degrees = 45.0;        double radians = Math.toRadians(degrees);
        System.out.format("The value of pi is %.4f%n", Math.PI);        System.out.format("The arccosine of %.4f is %.4f degrees %n", Math.sin(radians),                Math.toDegrees(Math.asin(Math.sin(radians))));    }
}