Google search

Basic and advanced computer skills like Excel with macros, How to speed up your PC, C, CPP, Java programming, HTML, JavaScript, PHP, Wordpress, all web tools, Android tutorials, MySQL Tutorials, WAMP server installation. etc.

Java Programs


Java program - Type casting [converting from float to an integer]
class Typecast
{
public static void main(String args[])
{
float a=2.3333f;
int b=10;
int c=(int)a+b;
System.out.println(c);
}
}

Java Program - use of all data types.
class vari
{
public static void main(String args[])
{
int a=3;
String str="Hello World!";
float f=2.333f;
double d=1.22222;
System.out.println(str);
System.out.println(a);
System.out.println(f);
System.out.println(d);
}
}

Java program - Switch demo
class switchDemo
{
public static void main(String args[])
{
char day='S';
switch(day)
{
case 'M':
System.out.println("Monday");
break;

case 'T':
System.out.println("Tuesday");
break;

case 'W':
System.out.println("Wednesday");
break;

default:
System.out.println("Invalid day of the week");
break;
}
}
}

Java Program to add two numbers using DataInputStream
import java.io.*;
class inp
{
public static void main(String args[])
{
int a,b,c;
try
{
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter val for a:");
a=Integer.parseInt(in.readLine());
System.out.print("Enter val for b:");
b=Integer.parseInt(in.readLine());
c=a+b;
System.out.print("SUM:"+c);
}
catch(IOException e)
{}
}
}


Java program to print person name and age
import java.io.DataInputStream;

class inp2
{
public static void main(String args[])
{
int age;
String name;
DataInputStream in=new DataInputStream(System.in); 
try
{
System.out.print("Enter Name:");
name=in.readLine();

System.out.print("Enter age:");
age=Integer.parseInt(in.readLine());

System.out.println("Name="+name);
System.out.println("Age="+age);
}
catch(Exception e) {}
}
}

Java Program to use Constructor
class Books
{
String title, auth;
int rate;

Books()
{
title="C Book";
auth="BSwamy";
rate=150;
System.out.println("Books details");
System.out.println("Book Title: "+title);
System.out.println("AuthorName:"+auth);
System.out.println("Book Price:"+rate);
}
}
class ConTest
{
public static void main(String args[])
{
Books b=new Books();
}
}

Java Program to display 3 employees name and salary.
import java.io.*;
class Emp3
{
public static void main(String args[])
{
int i, sal;
String nm;
try
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter 3 Employees info : Name and 

salary:");
for(i=0; i<3; i++)
{
nm=in.readLine();
sal=Integer.parseInt(in.readLine());
System.out.println(i+1+": Employee Info...............................");
System.out.println("Name : " + nm);
System.out.println("Salary : " + sal);
}
}
catch(IOException e) {}
}
}


Java Program to print Employee name, Department and Total salary.
class Emp
{
String ename, dept;
int bs,bn,sal;
void show()
{
ename="Amit";
dept="Accts";
bs=12000;
bn=1000;
sal=bs+bn;
System.out.println("Details of Employee");
System.out.println("Employee Name:"+ename);
System.out.println("Department:"+dept);
System.out.println("Total Salary"+sal);
}
}
class Emp_Dets
{
public static void main(String args[])
{
Emp e=new Emp();
e.show();
}
}


Java Program to print Vector and Enumeration
import java.util.*;
public class EnumTest 
{
public static void main(String args[]) 
{
      Enumeration days;

      Vector dayNames = new Vector();

      dayNames.add("Sunday");
      dayNames.add("Monday");
      dayNames.add("Tuesday");
      dayNames.add("Wednesday");
      dayNames.add("Thursday");
      dayNames.add("Friday");
      dayNames.add("Saturday");

      days = dayNames.elements();
 while (days.hasMoreElements())
{
System.out.println(days.nextElement()); 
  }
 }
}

Java Program to print Inventory information with use of Exception Handling.
import java.io.*;
class Err
{
public static void main(String args[])
{
String pnm;
int r, q, t;
try
{
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter Pname:");
pnm=in.readLine();
System.out.print("Enter Rate:");
r=Integer.parseInt(in.readLine());
System.out.print("Enter Qty:");
q=Integer.parseInt(in.readLine());
t=q*r;
System.out.println("\t Product Details\n ");
System.out.println("\t Product Name:  "+pnm);
System.out.println("\t Product Rate "+r);
System.out.println("\t Product Qty:  "+q);
System.out.println("\t======================");
System.out.println("\t Total amount:  "+t);
}
catch(IOException ie) {}
catch(NumberFormatException ne) 
{
System.out.println("\tData Invalid ");
}
}
}

Java Program to show ArrayIndexOutOfBoundsException
class Err2
{
public static void main(String args[])
{
int arr[]={1,2,3,4};
try
{
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
System.out.println(arr[3]);
System.out.println(arr[4]);
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println("Array index is out of range");
}
finally
{
System.out.println("Finally, Program Executed successfully");
}
}
}

Java Program to show Arithmetic Exception and Invalid number [Divide by Zero / Number Format Exception]
import java.io.*;

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

int n1,n2,res;
try
{
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter num1:");
n1=Integer.parseInt(in.readLine());
System.out.print("Enter num2:");
n2=Integer.parseInt(in.readLine());
res=n1/n2;
System.out.print("Div:"+res);
}

catch(IOException ie) {}
catch(NumberFormatException ne) 
{
System.out.println("\tData Invalid ");
}
catch(ArithmeticException ae) 
{
System.out.println("Divide by Zero err...");
}
}
}

Java Program to show Method Over Loading
class Disp
{
int x,y;
Disp()
{
System.out.println("No Parameter........");
}

Disp(int a, int b)
{
x=a;
y=b;
System.out.println("With Parameter...");
System.out.println("X: "+x);
System.out.println("Y: "+y);
}
}

class Mover
{
public static void main(String args[])
{
Disp d=new Disp();
Disp e=new Disp(2,3);
}
}

Java Program to show Method Overriding
class Base
{
void show()
{
System.out.println("inside the Base class method");
}
}

class Derive extends Base
{
void show()
{
System.out.println("inside the Dervied class method");
}
}

class Override
{
public static void main(String args[])
{
Derive d=new Derive();
d.show();
}
}


Java Program to print Multiplication Table from 1 to 10.
class MT
{
public static void main(String args[])
{
int i,j;

for(i=1; i<=10; i++)
{
for(j=1; j<=10; j++)
{
System.out.print("\t"+i*j);
}
System.out.println("\n");
}
}
}


Java Forms, Vectors and Hash Map Programs 


No comments:

Post a Comment