Tuesday, March 7, 2017

Electrical Resistance of Wire

The electrical resistance R of a cylindrical wire with length l (in meter) and diameter d (in meter) can be computed from the area A of its diameter (m2) and the  resistivity Ρ of the material (rho, meter times Ohm). The formula:

R=P*(l/A)

Code::

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author mrdishant
 */
import java.util.*;
public class Resistance {
    public static void main(String[] nt)
    {
        Scanner in=new Scanner(System.in);
        System.out.print("Enter Length of wire:");
        double length=in.nextDouble();
        System.out.print("Enter Diameter of Wire:");
        double d=in.nextDouble();
        System.out.print("Enter Resistivity of wire:");
        double p=in.nextDouble();
        double r=p*(4*length/3.142*d*d);
        System.out.println("Resistance of Wire:"+r);
 
    }
    
}

Output::

Metropolis City or Not

Question::  We define a Metropolis to be a city, that is either a capital city with more than 100 000 citizens or more than 200 000 citizens and an average income of 720 000 000 per year.

Give a boolean expression with all three variables that is true if and only if the city is a metropolis.

Code::

import java.util.*;
public class NT 
{
    public static void main(String [] nt)
    {
        boolean iscapital=false;
        long nofcitizens;
        double taxpercitizen;
        Scanner in=new Scanner(System.in);
        System.out.print("Press Y if City is a Capital Else anything:");
        char a=in.next().charAt(0);
        if(a=='y'||a=='Y')
        {
            iscapital=true;
        }
        System.out.print("Enter Number of Citizens:");
        nofcitizens=in.nextLong();
        System.out.print("Enter Tax Per Citizen:");
        taxpercitizen=in.nextDouble();
        if(iscapital  && nofcitizens>=100000||nofcitizens>=200000&&taxpercitizen*12*nofcitizens>720000000)
        {
            System.out.println("Yes This city is a Metropolis");
        }
        else
        {
            System.out.println(" This city is not a Metropolis");
        }
    }
    

}

Output:: 

Sunday, March 5, 2017

Method Overloading In Inheritance Java

If there is a method in subclass having same name as that of method in superclass but these have some difference in number and type of arguments then Method Overloading Takes place.

Code::
class d
{
    void show()
    {
        System.out.println("D's method");
    }
}
class e extends d
{
    void show(int x)
    {
        System.out.println("E's method");
    }
}
public class MethodOverriding {

    public static void main(String[] nt)
    {
        e ob=new e();
        ob.show();
        ob.show(67);
    }

}
Output:

Method Overriding In Java

When Superclass and subclass have method having same signature and name then a call to that method using subclass object will lead to invoking subclass version of that method. This is called method overriding.

Following Code will illustrate the concept:

Code::

class d
{
    void show()
    {
        System.out.println("D's method");
    }
}
class e extends d
{
    void show()
    {
        System.out.println("E's method");
    }
}
class f extends e
{
    void show()
    {
        System.out.println("F's method");
    }
}
public class MethodOverriding {

    public static void main(String[] nt)
    {
        f ob=new f();
        ob.show();
    }

}

Output::
As the above example shows that only subclass version is called.

Order of Execution of Constructors Java

Constructors follow the order of derivation in case of Inheritance
i.e- Superclass to Subclass
firstly superclass constructor is called then subclass constructor is called.
also in case of Multilevel inheritance.

Following code will illustrate the concept:

Code::

class a
{
    a()
    {
        System.out.println("A's Constructor");
    }
}
class b extends a
{
    b()
    {
        System.out.println("B's Constructor");
    }
}
class c extends b
{
    c()
    {
        System.out.println("C's Constructor");
    }
}
public class Constructors
{

    public static void main(String[] nt)
    {
        c ob=new c();
    }

}

Output::


super.member in JAVA

Keyword Super is also used to use the members of superclass which are same as the members of subclass.In this case if we use that member it refers to the member of subclass.

Syntax::   super.member;
here member can be data member or method

Following code will illustrate the concept:

Code::

import java.util.*;
class Base
{
    Scanner in=new Scanner(System.in);
    int a,b;
    Base()
    {
        System.out.print("Enter Value of a:");
        a=in.nextInt();
        System.out.print("Enter Value of b:");
        b=in.nextInt();
       
    }
    void display()
    {
        System.out.println("A= "+a+" \nB= "+b);
    }
}
class Derived extends Base
{
    Scanner in=new Scanner(System.in);
    int c;
    Derived()
    {
        System.out.print("Enter Value of c:");
        c=in.nextInt();
    }
    void display()
    {   super.display();
        System.out.println("C= "+c);    
    }
}
public class Inheritance
{

    public static void main(String[] nt)
    {
        Derived ob2=new Derived();
        System.out.println("Values are:");
        ob2.display();
    }
}

Output::

Saturday, March 4, 2017

Super() in Java

Keyword Super is used to refer to the immediate Superclass in case of Inheritance from the Subclass.It is used to call the constructor in case of constructor overloading because without this default constructor is called.

It is used in two forms:

1.Super is used to call the constructor of the Superclass in Subclass Constructor.

Syntax:: super(parameter  list)

Code::

import java.util.*;
class Base
{
    Scanner in=new Scanner(System.in);
    int a,b;
    Base()
    {
        System.out.print("Enter Value of a:");
        a=in.nextInt();
        System.out.print("Enter Value of b:");
        b=in.nextInt();
       
    }
    void show()
    {
        System.out.println(a+b);
    }
}
class Derived extends Base
{
    Scanner in=new Scanner(System.in);
    int c;
    Derived()
    {
        super();
        System.out.print("Enter Value of c:");
        c=in.nextInt();
    }
    void display()
    {
        System.out.println(a+b+c);     // a,b are the members of superclass
    }
}
public class Inheritance
{

    public static void main(String[] nt)
    {
        Derived ob2=new Derived();
        System.out.print("Sum is:");
        ob2.display();
    }
}

Output::