Thursday, 28 August 2014

Inheritance, Interfaces, Abstract Classes and Access Specifiers!

//Program for Single_Inheritance
import java.io.*;
class Student
{
    private int roll_no;
    private String name;

    void get_data(int roll_no,String name)
    {
        this.roll_no = roll_no;
        this.name = name;
    }

    void display()
    {
        System.out.print("\nThe Student Details are:");
        System.out.print("\nRoll no: "+roll_no);
        System.out.print("\nName: "+name);
    }
}


class Marks extends Student
{
    private int mark1,mark2;

    Marks(int mark1,int mark2)
    {
        this.mark1 = mark1;
        this.mark2 = mark2;
    }

    void display_marks()
    {
        System.out.print("\nMarks1: "+mark1);
        System.out.print("\nMarks2: "+mark2);

          float avg;
          avg = (mark1+mark2)/2;
          System.out.println("\nAverage: "+avg);
    }
}

public class Single_Inheritance
{
    public static void main(String s[]) throws Exception
    {
          int roll_no,mark1,mark2;
          String name;
         
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         
          System.out.print("\nEnter Roll_no: ");
          roll_no = Integer.parseInt(br.readLine());

          System.out.print("\nEnter Name: ");
          name = br.readLine();

          System.out.print("\nEnter Marks1: ");
          mark1 = Integer.parseInt(br.readLine());

          System.out.print("\nEnter Marks2: ");
          mark2 = Integer.parseInt(br.readLine());

          Marks m = new Marks(mark1,mark2);
          m.get_data(roll_no,name);

          m.display();
          m.display_marks();
    }
}
Output



//Program for Multilevel Inheritance
import java.io.*;
class A
{
     private int a;
    
     void get_a(int a)
     {
        this.a = a;
     }

     void display_a()
     {
        System.out.print("\nA: "+a);
     }
}

class B extends A
{
     private int b;
    
     void get_b(int b)
     {
        this.b = b;
     }

     void display_b()
     {
        System.out.print("\nB: "+b);
     }
}

class C extends B
{
     private int c;
    
     void get_c(int c)
     {
        this.c = c;
     }

     void display_c()
     {
        System.out.println("\nC: "+c);
     }
}

public class Multilevel_inheritance
{
     public static void main(String s[]) throws Exception
     {
          int a,b,c;
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         
          System.out.print("\nEnter a: ");
          a = Integer.parseInt(br.readLine());

          System.out.print("\nEnter b: ");
          b = Integer.parseInt(br.readLine());

          System.out.print("\nEnter c:");
          c = Integer.parseInt(br.readLine());

          C abc=new C();

          abc.get_a(a);
          abc.get_b(b);
          abc.get_c(c);

          abc.display_a();
          abc.display_b();
          abc.display_c();
     }
}
Output


//Program for Abstract class
abstract class A
{
     abstract void a();
     abstract void b();
     void terminate()
     {
        System.out.print("\nTerminating.....");
     }
}

class B extends A
{
     void a()
     {
        System.out.print("\nStarting..... A");
     }
     void b()
     {
        System.out.print("\nStarting..... B");
     }
}

class Abstract_Demo
{
     public static void main(String s[]) throws Exception
     {
          B b1=new B();
          b1.a();
          b1.terminate();
          b1.b();
          b1.terminate();
          System.out.println();
     }
}
Output



//Program for Interface
interface A
{
    void get_A(int a);
}

interface B
{
    void get_B(int b);
}

interface C
{
    void get_C(int c);
}

class D implements A,B,C
{
     int a1,b1,c1;
     public void get_A(int a)
     {
        a1 = a;
     }
     public void get_B(int b)
     {
        b1 = b;
     }
     public void get_C(int c)
     {
        c1 = c;
     }
     void display()
     {
          System.out.print("\nA: "+a1);
          System.out.print("\nB: "+b1);
          System.out.println("\nC: "+c1);
     }
}

class InterfaceDemo
{
     public static void main(String soham[]) throws Exception
     {
          D d = new D();
          d.get_A(10);
          d.get_B(20);
          d.get_C(30);
          d.display();
     }
}

Tuesday, 26 August 2014

Method Overriding, Constructor Overloading, Scanner class

//Program for Method Overriding
class Bank
{
    int rate_of_interest()
    {
        return 0;
    }
}

class SBI extends Bank
{
    //override
    int rate_of_interest()
    {
        return 8;
    }
}

class ICICI extends Bank
{
    //override
    int rate_of_interest()
    {
        return 7;
    }
}

class AXIS extends Bank
{
    //override
    int rate_of_interest()
    {
        return 9;
    }
}

public class Method_Overriding
{
    public static void main(String soham[])
    {
        SBI sbi = new SBI();

        ICICI icici = new ICICI();

        AXIS axis = new AXIS();
          
        System.out.print("\nSBI Rate of Interest: " + sbi.rate_of_interest());

        System.out.print("\nICICI Rate of Interest: " + icici.rate_of_interest());

        System.out.print("\nAXIS Rate of Interest: " + axis.rate_of_interest());

        System.out.println();
    }
}
Output

//Program for Constructor Overloading
class Cube
{
    int length;
    int breadth;
    int height;

    Cube()                                                            //constructor 1        
    {
        length = 10;
        breadth = 10;
        height = 10;
    }

    Cube(int length, int breadth, int height)   //constructor 2
    {
        this.length = length;
        this.breadth = breadth;
        this.height = height;
    }

    public int get_volume()
    {
        return (length * breadth * height);
    }
 }

public class Constructor_Demo
{
    public static void main(String soham[]) throws Exception
    {
         Cube cube_obj1 = new Cube();
         Cube cube_obj2 = new Cube(10, 20, 30);
        System.out.println("Volume of Cube1 is : " + cube_obj1.get_volume());
        System.out.println("Volume of Cube1 is : " + cube_obj2.get_volume());
    }
}
Output

//Program for Scanner Class
import java.util.Scanner;
public class Scanner_Demo
{
   public static void main(String soham[]) throws Exception
  {
      Scanner sc = new Scanner(System.in);
  
      System.out.print("\nEnter your rollno: ");
       int rollno = sc.nextInt();

      System.out.print("\nEnter your name: ");
      String name = sc.next();

      System.out.print("\nEnter your fee: ");
      double fee = sc.nextDouble();

      System.out.print("\nStudents Details Are: ");
      System.out.print("\nRoll no: "+rollno);
      System.out.print("\nName: "+name);
      System.out.print("\nFee: "+fee);

       System.out.println();
   }
}
Output

Friday, 22 August 2014

White Spaces, Data Types and Array Of Objects.

//Program for White Space
import java.io.*;
class WhiteSpace
{
    public static void main(String soham[]) throws Exception
    {
        String str;

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("\nEnter String: ");
        str = br.readLine();

        System.out.print("\nIn this statement, back space is used: "+str+"\b ");
        System.out.print("\nIn this statement, form feed is used: \f "+str+" \f ");
        System.out.print("\nIn this statement, new line is used: \n"+str);
        System.out.print("\nIn this statement, horizontal tab is used: \t"+str);
        System.out.print("\nIn this statement, single quote is used: \'"+str+"\'");
        System.out.print("\nIn this statement, double quote is used: \""+str+"\"");
        System.out.print("\nIn this statement, backslash is used: \\"+str);
        System.out.println();
    }
}
Output

//Program for size of the Datatypes and their Default values

class Datatypes
{
    static int sizeof(byte b)
    {
        return 1;
    }
    static int sizeof(short s)
    {
        return 2;
    }
    static int sizeof(int i)
    {
        return 4;
    }
    static int sizeof(long l)
    {
        return 8;
    }
    static int sizeof(float f)
    {
        return 4;
    }
    static int sizeof(double d)
    {
        return 8;
    }

    public static void main(String soham[]) throws Exception
    {
        byte b = 0;
        short s = 0;
        int i = 0;
        long l = 0;
        float f = 0.0f;
        double d = 0.0d;
     
        System.out.println("\nDatatypes"+" Size"+"\tDefault values");
        System.out.println("byte:\t  "+sizeof(b)+" \t0");
        System.out.println("short:\t  "+sizeof(s)+" \t0");
        System.out.println("int:\t  "+sizeof(i)+" \t0");
        System.out.println("long:\t  "+sizeof(l)+" \t0L");
        System.out.println("float:\t  "+sizeof(f)+" \t0.0f");
        System.out.println("double:\t  "+sizeof(d)+" \t0.0d");

        System.out.println();
    }
}
Output

//Program for Print() And Println() method

class Print_Demo
{
    public static void main(String soham[]) throws Exception
    {
        System.out.println("\nWelcome To");
        System.out.print("Java Programming");
        System.out.println();
    }
}
Output

//Program for Array Of Object

import java.io.*;
class Employee
{
    String name;
    int emp_id;
    int salary;

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public void get_data()
    {
        try
        {
           System.out.print("\nEnter name: ");
           name = br.readLine();
           System.out.print("\nEnter emp id: ");
           emp_id = Integer.parseInt(br.readLine());
           System.out.print("\nEnter salary: ");
           salary = Integer.parseInt(br.readLine());
        }
        catch(Exception e)
        {
            System.out.print("\n"+e);
        }
    }

    public void display()
    {
        System.out.print("\nName: "+name);
        System.out.print("\nEmp id: "+emp_id);
        System.out.print("\nSalary: "+salary);
        System.out.println();
    }
}

public class Array_of_object
{
    public static void main(String s[]) throws Exception
    {
        int i;
        Employee emp[] = new Employee[3];
        for(i=0;i<=2;i++)
        {
            emp[i] = new Employee();
        }
 
        for(i=0;i<=2;i++)
        {
           System.out.print("\nEnter Employee Detail for employee "+(i+1));
           emp[i].get_data();
        }

        for(i=0;i<=2;i++)
        {
           System.out.print("\nEmployee Details for employee "+(i+1));
           emp[i].display();
        }

        System.out.println();
    }
}
Output

Wednesday, 20 August 2014

Class Demo

import java.io.*;
class Student
{
    private int roll_no;
    private String name;
    void get_data(String name,int roll_no)
    {
        this.name=name;
        this.roll_no=roll_no;
    }

    void display()
    {
        System.out.print("\nStudent Details: ");
        System.out.print("\nName: "+name);
        System.out.print("\nRoll_no: "+roll_no);
    }
}

class ClassDemo
{
    public static void main(String s[]) throws Exception
    {
        Student s1=new Student();
        Student s2=new Student();

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("\nEnter Name: ");
        String name = br.readLine();

        System.out.print("\nEnter Roll no: ");
        int roll_no = Integer.parseInt(br.readLine());

        s1.get_data(name,roll_no);
        s1.display();

        System.out.println();
    }
}
Output

Tuesday, 19 August 2014

Continue Statement

//Program for Continue statement

import java.io.*;
import java.io.*;
class Continue
{
    public static void main(String s[]) throws Exception
    {
        int n,i;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        System.out.print("\nEnter n:");
        n=Integer.parseInt(br.readLine());

        System.out.print("\nOdd Numbers: ");
        for(i=0;i<=n;i++)
        {
            if(i%2==0)
            {
                continue;
            }
            else
            {
                System.out.println("\n"+i);
            }
        }
    }
}
Output
Enter n:10

Odd Numbers:
1

3

5

7

9

Random Number Generator

//Program for generating Random number

import java.io.*;
import java.util.Random;
class Random_number
{
    public static void main(String s[]) throws Exception
    {
        int n;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("\nEnter number between 1 to 10: ");
        n = Integer.parseInt(br.readLine());

        Random random_num = new Random();
        int r = random_num.nextInt(10);   //generating Random number

        System.out.print("\nRandom number: "+r);

        if(n == r)
        {
            System.out.print("\nYou won...");
        }
        else
        {
            System.out.print("\nYou lose!!!");
        }

        System.out.println();
    }
}
OUTPUT 1:-

Enter number between 1 to 10: 5

Random number: 5
You won...

OUTPUT 2:-

Enter number between 1 to 10: 2

Random number: 8
You lose!!!