.

Friday 27 March 2015

Address Book program in JAVA



package addressbook;

public class AddressBook

// This class defines an address book, consisting of 10 parts

{
// Class constant

final String PUNCT = ", "; // Punctuation for formatting

// Instance variable

String first;
String middle;
String last;
String homeadd;
String homeph;
String busadd;
String busph;
String faxnum;
String cellnum;
String pagenum;

// Constructors

public AddressBook(String firstName, String middleName, String lastName, String homeAddress, String homePhone, String businessAddress, String businessPhone, String faxNumber, String cellNumber, String pagerNumber)

// Init an Address Book object with all addressbook fields

{
first=firstName;
middle=middleName;
last=lastName;
homeadd=homeAddress;
homeph=homePhone;
busadd=businessAddress;
busph=businessPhone;
faxnum=faxNumber;
cellnum=cellNumber;
pagenum=pagerNumber;

}

// Basic observers that return the value of each feild

public String knowFirstName ()

{
return first;
}

public String knowMiddleName ()

{
return middle;
}

public String knowLastName ()

{
return last;
}
public String knowHomeAddress ()

{
return homeadd;
}
public String knowHomePhone ()

{
return homeph;
}
public String knowBusinessAddress ()

{
return busadd;
}
public String knowBusinessPhone ()

{
return busph;
}
public String knowFaxNumber ()

{
return faxnum;
}
public String knowCellNumber ()

{
return cellnum;
}
public String knowPagerNumber ()

{
return pagenum;
}

// Additional observers for first middle last format

public String firstMidLast ()

{
return first + " " + middle + " " + last;
}

public String lastFirstMid ()

{
return last + PUNCT + first + " " + middle;
}

    // Equals and CompareTo methods

public boolean equals (AddressBook otherName)
{
return
first.equals (otherName.first) &&
middle.equals (otherName.middle) &&
last.equals (otherName.last);
}

public int compareTo (AddressBook otherName)
{
int result;
result = last.toUpperCase().compareTo(otherName.last.toUpperCase());
if (result != 0)
return result;
else
{
result = first.toUpperCase().compareTo(otherName.first.toUpperCase());
if (result != 0)
return result;
else
return
result = middle.toUpperCase().compareTo(otherName.middle.toUpperCase());
}
}
}
Read more ...

Program to do circle operations in JAVA

import java.text.*;

public class Circle


{

  public static void main (String[] args)

  {

    // create decimal format

    DecimalFormat DF = new DecimalFormat ("0.00000");

    // Radius is the radius of the circle
    // Diam is diameter
    // Circum is circumference
    // Area is area of circle

    double Radius = 6.75;
    double Diam;
    double Circum;
    double Area;

    // PI is constant 3.14159

    final double PI = 3.14159;

    // calculate diameter

    Diam = 2 * Radius;

    // calculate circumference

    Circum = PI * Diam;

    // calculate area

    Area = PI * Radius * Radius;

    // display intro message

    System.out.println("This program calculates Diameter, Circumference, and Area of a circle");

    // display the result

    System.out.println("Radius constant = " + DF.format(Radius));
    System.out.println("Diameter = " + DF.format(Diam));
    System.out.println("Circumference = " + DF.format(Circum));
    System.out.println("Area = " + DF.format(Area));

    // display end message

    System.out.println("End of program");

  }

}

Read more ...

Program to generate MARKSHEET in JAVA

import java.util.Scanner;
class Marksheet
{
public static void main(String[] arg)
{
Scanner sc=new Scanner(System.in);
System.out.println("\nWel come to marks sheet");
System.out.print("Enter the name of Student \t:\t");
String name=sc.next();
System.out.print("Enter the Roll Number \t\t:\t");
String roll=sc.next();
System.out.print("Enter the College Name \t\t:\t");
String clg=sc.next();
System.out.print("\nEnter the Marks of Sub1 \t:\t");
int s1=sc.nextInt();
System.out.print("Enter the Marks of Sub2 \t:\t");
int s2=sc.nextInt();
System.out.print("Enter the Marks of Sub3 \t:\t");
int s3=sc.nextInt();
System.out.print("Enter the Marks of Sub4 \t:\t");
int s4=sc.nextInt();
System.out.print("Enter the Marks of Sub5 \t:\t");
int s5=sc.nextInt();

int total;
float percent;
total=s1+s2+s3+s4+s5;
percent=total/5;

System.out.print("\n\n\t\t   Marksheet");
System.out.print("\n-----------------------------------------------------");
System.out.print("\n\tName\t\t:\t"+name);
System.out.print("\n\tRoll No.\t:\t"+roll);
System.out.print("\n\tCollege\t\t:\t"+clg);
System.out.print("\n-----------------------------------------------------");
System.out.print("\n\tSubject\t\t:\tMarks");
System.out.print("\n\tSub1\t\t:\t"+s1);
System.out.print("\n\tSub2\t\t:\t"+s2);
System.out.print("\n\tSub3\t\t:\t"+s3);
System.out.print("\n\tSub4\t\t:\t"+s4);
System.out.print("\n\tSub5\t\t:\t"+s5);
System.out.print("\n-----------------------------------------------------"); 
System.out.print("\n\tTotal\t\t:\t"+total);
System.out.print("\n\tPercentage\t:\t"+percent);

if(percent>=60)
{
System.out.print("\n\tDivision\t:\tFirst");
}

if(percent>=40 && percent<60)
{
System.out.print("\n\tDivision\t:\tSecond");
}

if(percent<40)
{
System.out.print("\n\tDivision\t:\tThird");
}
}
}
Read more ...

Metric conversion program in JAVA

import java.util.Scanner;
class conversion 
{

 static float dollartorupees()
   {  Scanner sc=new Scanner(System.in);
     System.out.println("enter the dollar");
    float d=sc.nextFloat();
    float r=d/50;
System.out.println("rupees ="+r);
return(r);
}
static float rupeestodollars()
   {  Scanner sc=new Scanner(System.in);
     System.out.println("enter the rupees");
    float r=sc.nextFloat();
    float d=r*50;
System.out.println("rupees ="+d);
return(d);
}

static float metertokilometer()
   {  Scanner sc=new Scanner(System.in);
     System.out.println("enter the meter");
    float m=sc.nextFloat();
    float km=m/1000;
System.out.println("rupees ="+m);
return(m);
}


static float kilometertometer()
   {  Scanner sc=new Scanner(System.in);
     System.out.println("enter the kilometer");
    float km=sc.nextFloat();
    float m=km*1000;
System.out.println("rupees ="+m);
return(m);
}



static float farenhitetocelcius()
   {  Scanner sc=new Scanner(System.in);
     System.out.println("enter the kilometer");
    float f=sc.nextFloat();
    float c=(5*(f-32))/9;
System.out.println("rupees ="+c);
return(c);
}




static float celciustofarenhite()
   {  Scanner sc=new Scanner(System.in);
     System.out.println("enter the celcius");
    float c=sc.nextFloat();
    float f=((9*c)/5)+32;
System.out.println("rupees ="+f);
return(f);
}








}


class testconversion extends conversion
{
public static void main(String[] arg)
{
conversion ob=new testconversion();
Scanner sc=new Scanner(System.in);
System.out.println("enter your choice");
int x=sc.nextInt();
if(x== 1)
ob.dollartorupees();
else if(x==2)
ob.rupeestodollars();
else if(x==3)
ob.metertokilometer();
else if(x==4)
ob.kilometertometer();
else if(x==5)
ob.farenhitetocelcius();
else if(x==6)
ob.celciustofarenhite();
else
System.out.println("wronghoice");
}
}
Read more ...

Calculator program in JAVA using if else statements

import java.util.Scanner;
public class calculator
{


int c;
public void add()
{
Scanner sc =new Scanner(System.in);
System.out.println("enter first no.");
int a=sc.nextInt();
System.out.println("enter second no. ");
int b=sc.nextInt();
c=a+b;
System.out.println(c);
}
public void sub()
{
Scanner sc =new Scanner(System.in);
System.out.println("enter first no.");
int a=sc.nextInt();
System.out.println("enter second no. ");
int b=sc.nextInt();
c=a-b;
System.out.println(c);
}
public void mul()
{
Scanner sc =new Scanner(System.in);
System.out.println("enter first no.");
int a=sc.nextInt();
System.out.println("enter second no. ");
int b=sc.nextInt();
c=a*b;
System.out.println(c);
}
public void div()
{
Scanner sc =new Scanner(System.in);
System.out.println("enter first no.");
int a=sc.nextInt();
System.out.println("enter second no. ");
int b=sc.nextInt();
c=a/b;
System.out.println("your answer is"+c);
}
}

class scientificcalculator extends calculator
 {
final float pi=3.14f;
float k;
public void square()
{
Scanner sc =new Scanner(System.in);
System.out.println("enter first no.");
int a=sc.nextInt();
k=pi*a*a;
System.out.println("your answer is"+k);
}
public void cube()
{
Scanner sc =new Scanner(System.in);
System.out.println("enter first no.");
int a=sc.nextInt();
c=a*a*a;
System.out.println("your answer is"+c);
}
public void power()
{
Scanner sc =new Scanner(System.in);
System.out.println("enter first no.");
int a=sc.nextInt();

System.out.println("enter second no. ");
int b=sc.nextInt();

c=a^b;
System.out.println("your answer is"+c);
}
public void fact()
{
int x=1;
int i;
Scanner sc =new Scanner(System.in);
System.out.println("enter first no.");
int a=sc.nextInt();
for(i=0;i<=a;i++)
{
x=x*i;
System.out.println(x);
}
}

}

class Main extends scientificcalculator
{
public static void main(String[] arg)
{

scientificcalculator ob =new Main();

Scanner sc =new Scanner(System.in);
System.out.println("enter the no. b/w 1 to 4");
int num=sc.nextInt();

if(num==1)
ob.add();
else if(num==2)
ob.sub();
else if(num==3)
ob.mul();
else if(num==4)
ob.fact();
else if(num==5)
ob.div();
else if(num==6)
ob.square();
else if(num==7)
ob.power();
else 
ob.cube();}
}
Read more ...

Small shopping project in JAVA

import java.util.Scanner;
class Product
{
Scanner sc=new Scanner(System.in);
int ProductID;
String ProductName, ProductType;
float ProductPrice;

Product()
{
System.out.print("Enter the Product ID = ");
ProductID=sc.nextInt();
System.out.print("Enter the Product Name = ");
ProductName=sc.next();
System.out.print("Enter the Product Type = ");
ProductType=sc.next();
System.out.print("Enter the Product Price = ");
ProductPrice=sc.nextFloat();

}
void ViewAllProduct()
{
System.out.print("\n\nEnter the Product ID = "+ProductID);
System.out.print("\nEnter the Product Name = "+ProductName);
}

void ViewProductDetails(int ID)
{
if(ProductID==ID)
{

System.out.print("The Product ID = "+ProductID);
System.out.print("\nThe Product Name = "+ProductName);
System.out.print("\nThe Product Type = "+ProductType);
System.out.print("\nThe Product Price = "+ProductPrice);
}
}

}

class Shop extends Product
{
public static void main(String s[])
{
Scanner sc1=new Scanner(System.in);
Product ob[]=new Product[5];

for(int i=0;i<5;i++)
{
ob[i]=new Product();
}

System.out.print("\n\n Please Press an Option :\n1 to view all Product\n2 to see prouct details\n3 to EXIT\n\nEnter your choice = ");
int c=sc1.nextInt();

if(c==1)
{
for(int i=0;i<5;i++)
{
ob[i].ViewAllProduct();
}
}

if(c==2)
{
System.out.print("\n\nEnter the Product ID for details = ");
int x=sc1.nextInt();
for(int i=0;i<5;i++)
{
ob[i].ViewProductDetails(x);
}

}
if(c==3)
{
System.exit(0);
}
else
{
System.out.print("\n\nWrong Input");
}

}
}
Read more ...

Matrix addition program in JAVA

import java.util.Scanner;
class addm
{
 public static void main(String[] arg)
 {
  int i,j;
 int a[][]=new int[3][3];
 Scanner sc=new Scanner(System.in);
 for(i=0;i<3;i++)
 {
   for(j=0;j<3;j++)
 {
  a[i][j]=sc.nextInt();
 }
}
  int b[][]=new int[3][3];
 for(i=0;i<3;i++)
 {
   for(j=0;j<3;j++)
 {
  b[i][j]=sc.nextInt();
}
}
int c[][]=new int[3][3];
 for(i=0;i<3;i++)
 {
   for(j=0;j<3;j++)
 {
  c[i][j]=a[i][j]+b[i][j];
 System.out.println("Matrix Addition="+c[i][j]);
}
}
}
}
Read more ...

Program to find diagonal elements in matrix

import java.util.Scanner;
class diagonalm
{
 public static void main(String[] arg)
 {
  int i,j;
 int a[][]=new int[3][3];
 Scanner sc=new Scanner(System.in);
 for(i=0;i<3;i++)
 {
   for(j=0;j<3;j++)
 {
  a[i][j]=sc.nextInt();
 }
}
System.out.println("Left diagonal elements=0");
 for(int k=0;k<3;k++)
 {
   for(int l=0;l<3;l++)
 {
 if(k==l)
 {
 System.out.println(a[k][l]);
}
}
}
 System.out.println("Right diagonal elements=0");
 for(int m=0;m<3;m++)
 {
   for(int n=0;n<3;n++)
 {
  if(m+n==2)
 {
  System.out.println(a[m][n]);
 }
}
}
}
}


Read more ...

Download program of LOGIC GATE IN JAVA

AND GATE

package logicgate;
 public class and
{
 int z1;
 public int dooperation(int x1,int y1)
 {
  int z;
  z=x1*y1;
  z1=z;
 return(z1);
}
 

 }

 NOT GATE
package logicgate;

 public class not
 {

 public int dooperation(int x3)
 {
  int z3;
  {
   if(x3==0)
   z3=1;
   else
   z3=0;
   return(z3);
 }
 }
  }


OR GATE
package logicgate;

 public class or
{
 int z2;
 public int dooperation(int x2,int y2)
 {
 int z;
 z=x2+y2;
 z2=z;
  return(z2);
 }
 
 }

MAIN CLASS


import logicgate.and;
import logicgate.or;
import logicgate.not;

 class Main
{
 public static void main(String[] arg)
{
 and ob1=new and();
 or ob2=new or();
 not ob3=new not();
 ob1.dooperation(0,1);
 ob2.dooperation(0,0);
 ob3.dooperation(0);
}
}

Read more ...

Program to find pallindrome in JAVA

class Pallindrome
{
public static void main(String args[])
{
int num=131;
int r,temp,sum=0;
temp=num;
while(num!=0)
{
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(sum==temp)
System.out.println("no. is pallindrome");
else
System.out.println("no. is not pallindrome");
}
}
Read more ...

Program to find electricity bill in JAVA using if and else conditions

class electricity
{
Public static Void main(String args[])
{
int unit=Integer.parseInt(args[1]);
String name=args[0];
float rupees=0.0f;
{
if (unit<100)
rupees=rupees +unit*.40;
else if(unit>100 && unit<300)
rupees=rupees+(unit-100)*.50;
else (unit>300)
rupees=rupees+(unit-300)*.60;
}
if(rupees<250)
{
System.out.println("name="+name);
System.out.println("unit="+unit+"rupees="+rupees);
}
else
{
rupees=rupees+(rupees*15)/100;
System.out.println("name="+name);
System.out.println("unit="+unit+"rupees="+rupees);
}}}

Read more ...

Program to find area of circle and cylinder in JAVA

class Area{

final float PI=3.14f;
float arac,aracy;
int r=2,h=3,l=4;
void areaofcircle()
{
arac=PI*r*r;
System.out.println("area of circle="+arac);
}
void areaofcylinder()
{
aracy=((PI*r*r*h)+(2*PI*r*l));
System.out.println("area of cylinder="+aracy);
}
}
class area2{

public static void main(String[] arg)
{
 area ob=new area();
 ob.areaofcircle();
 ob.areaofcylinder();
}
}

Read more ...

Swapping two variables with using third variable in JAVA

class swap
{
   public static void main(String []args)
   {
      int a=25;
      int b=10;
      System.out.println("Intially a="+a+"b="+b);
      a=a+b;
      b=a-b;
      a=a-b;
      System.out.println("a="+a+"\nb="+b);
   }
}
Read more ...

Program to find factorial in JAVA



class factorial{

public static void main(String args[])

{

int f=1,i;

for(i=1;i<=5;i++)

f=f*i;

System.out.println("factorial="+f);

}

}
Read more ...

Program to set and print employee details

class Details

int empid,deptid,salary;

//method to set details
void setEmployeeDetails()
{
empid=10002;
deptid=15;
salary=5000;
}

//method to print details
void printEmployeeDetails()
    {
System.out.println(empid);
System.out.println(deptid);
System.out.println(salary);
      }

}

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

//method to create object of detail class

details ob=new details();

//method to call the function through object

ob.printEmployeeDetails();
}
}
Read more ...

Prime number program in JAVA

Program to find prime number in java

class prime{
public static void main(String []args){
int num=6;
int fac=0;
for(int i=1;i<=6;i++)
{
 if(num%i==0)
   fac++;
}
if(fac==2)
System.out.println("Is a prime no");
else
 System.out.println("Not a prime no");
}
}
Read more ...

Fibonacci program in JAVA

Program to find fibonacci series in JAVA

class fib
 {
 public static void main(String[] arg)
 {
  int i,a=0,b=0,c=1;
  int n=8;
   System.out.println("fibonacci series"+n);
  for(i=1;i<=n;i++)
  {
   a=b;
  b=c;
  c=a+b;
  System.out.println(c);
  
  }
  }
  }
        
Read more ...

Finding five armstrong numbers in JAVA

Program to find Five armstrong Numbers in java

class fivearms
{
public static void main(String args[])
{
int r,temp,count;
for(count=1;count<500;count++)
{
int sum=0;
temp=count;
while(temp!=0)
{
r=temp%10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==count)
System.out.println("no.s are"+count);}
}
}
Read more ...

"Hello World" program in JAVA

Steps to develop a hello world program in java-


class Hello {
//Everything should be written inside this block
public static void main(String args[])
{

//to print on the console
 System.out.println("hellow world");

}
}

1. Everything in the world is defined in terms of class.If you want to go for developing car first you will design the model for it. In Every car everything will be same as the model but objects can hold different characterstics(objects are real world entities in case of car it can be Honda city, Tata nano, swift desire)

Class- class is nothing but a blueprint of an object.

System.out.println("") is a method to print anything inside the parameter on the console.where System is a class, out is its object and println() is a method of it.






Read more ...