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

No comments:

Post a Comment