Google Search

Sunday, October 30, 2011

Write a Java program using AWT to demonstrate Choice implementation.


Write a program using AWT to demonstrate Choice  implementation.


import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class ChoiceDemo extends Applet implements ItemListener
{
            Choice os,browser;
            String msg="";
            public void init()
            {
                        os=new Choice();
                        browser=new Choice();
                        os.add("Windows XP");
                        os.add("Windows Vista");
                        os.add("Solaris");
                        os.add("Mac OS");
                        browser.add("Internet Explorer");
                        browser.add("Firefox");
                        browser.add("Opera");
                        add(os);
                        add(browser);
                        os.addItemListener(this);
                        browser.addItemListener(this);
            }
            public void itemStateChanged(ItemEvent ie)
            {
                        repaint();
            }
            public void paint(Graphics g)
            {
                        msg="Current OS: ";
                        msg+=os.getSelectedItem();
                        g.drawString(msg,6,120);
                        msg="Current Browser: ";
                        msg+=browser.getSelectedItem();
                        g.drawString(msg,6,140);
            }
}

/*
<applet code="ChoiceDemo" width=300 height=180>
</applet>
*/



Demonstrate the functionality of packages.


  1. Demonstrate the functionality of packages.

package MyPack;

public class Balance
{
      String name;
      double bal;
      public Balance(String n,double b)
      {
                  name=n;
                  bal=b;
      }
      public void Show()
      {
                  if(bal<0)
                  {
                              System.out.print("--> ");
                  }
                  System.out.println(name + ": $" + bal);
      }
}

   TESTBALANCE.JAVA

   import MyPack.*;

class TestBalance
{
            public static void main(String args[])
            {
                        Balance test=new Balance("J.J.Jaspers",99.88);
                        test.Show();
            }
}

Calculate the area of triangle and rectangle by using abstract class.


  1. Calculate the area of triangle and rectangle by using abstract class.


abstract class Figure
{
      double dim1;
      double dim2;
      Figure(double a, double b)
      {
                  dim1 = a;
                  dim2 = b;
      }
      abstract double area();
}

class Rectangle extends Figure
{
      Rectangle(double a, double b)
      {
                  super(a, b);
      }
      double area()
      {
                  System.out.println("Inside Area for Rectangle.");
                  return dim1 * dim2;
      }
}

class Triangle extends Figure
{
      Triangle(double a, double b)
      {
                  super(a, b);
      }
      double area()
      {
                  System.out.println("Inside Area for Triangle.");
                  return dim1 * dim2 / 2;
      }
}

class AbstractAreas
{
      public static void main(String args[])
      {
                  Rectangle r = new Rectangle(9, 5);
                  Triangle t = new Triangle(10, 8);
                  Figure figref;
                  figref = r;
                  System.out.println("Area is " + figref.area());
                  figref = t;
                  System.out.println("Area is " + figref.area());
      }
}

Write a Java program for single inheritance using overriding concept.


  1. Write a program for single inheritance using overriding concept.

class A
{
      int i, j;
      A(int a, int b)
      {
                  i = a;
                  j = b;
      }
      void show()
      {
                  System.out.println("i and j: " + i + " " + j);
      }
}

class B extends A
{
      int k;
      B(int a, int b, int c)
      {
                  super(a, b);
                  k= c;
      }
      void show(String msg)
      {
                  System.out.println("k: " + k);
      }
}

class Override
{
      public static void main(String args[])
      {
                  B subOb = new B(1, 2, 3);
                  subOb.show("This is k");
                  subOb.show();
      }
}

Write a Java program using constructor overloading.


  1. Write a program using constructor overloading.

class Box
{
      double width;
      double height;
      double depth;
      Box(double w,double h,double d)
      {
                  width=w;
                  height=h;
                  depth=d;
      }
      Box()
      {
                  width=-1;
                  height=-1;
                  depth=-1;
      }
      Box(double len)
      {
                  width=height=depth=len;
      }
      double volume()
      {
                  return width*height*depth;
      }
}

class OverloadCons
{
      public static void main(String args[])
      {
                  Box mybox1=new Box(10,20,15);
                  Box mybox2=new Box();
                  Box mycube=new Box(7);
                  double vol;
                  vol=mybox1.volume();
                  System.out.println("Volume of mybox1 is "+vol);
                  vol=mybox2.volume();
                  System.out.println("Volume of mybox2 is "+vol);
                  vol=mycube.volume();
                  System.out.println("Volume of mycube is "+vol);
      }
}

Write a Java program using method overloading.


  1. Write a program using method overloading.

class Overload
{
      void ovlDemo()
      {
                  System.out.println("No Parameters");
      }
      void ovlDemo(int a)
      {
                  System.out.println("One Parameter:"+a);
      }
      int ovlDemo(int a,int b)
      {
                  System.out.println("Two Parameters:"+a+" "+b);
                  return a+b;
      }
      double ovlDemo(double a,double b)
      {
                  System.out.println("Two Double Parameters:"+a+" "+b);
                  return a+b;
      }
}

class OverloadDemo
{
      public static void main(String args[])
      {
                  Overload ob=new Overload();
                  int resI;
                  double resD;
                  ob.ovlDemo();
                  System.out.println();
                  ob.ovlDemo(2);
                  System.out.println();
                  resI=ob.ovlDemo(4,6);
                  System.out.println("Result of ob.ovlDemo(4,6): "+resI);
                  System.out.println();
                  resD=ob.ovlDemo(1.1,2.32);
                  System.out.println("Result of ob.ovlDemo(1.1,2.32): "+resD);
      }
}

Write a Java program to calculate the area of rectangle using parameterized constructor.


  1. Write a program to calculate the area of rectangle using parameterized constructor.
class Rectangle
{
      int length;
      int width;
      Rectangle(int x,int y)
      {
                  length=x;
                  width=y;
      }
      int rectArea()
      {
                  return(length*width);
      }
}

class RectangleArea
{
      public static void main(String args[])
      {
                  Rectangle rect1=new Rectangle(15,10);
                  int area1=rect1.rectArea();
                  System.out.println("Area1="+area1);
      }
}

Write a Java program by creating a class vehicle and calculate the range of vehicle.


Write a program by creating a class vehicle and calculate the range of vehicle.

class Vehicle
{
   int passengers;
   int fuelcap;
   int mpg;
   int range()
   {
               return mpg*fuelcap;
   }
}

class RetMeth
{
   public static void main(String args[])
   {
               Vehicle minivan=new Vehicle();
               Vehicle sportscar=new Vehicle();
               int range1,range2;
               minivan.passengers=7;
               minivan.fuelcap=16;
               minivan.mpg=21;
               sportscar.passengers=2;
               sportscar.fuelcap=14;
               sportscar.mpg=12;
               range1=minivan.range();
               range2=sportscar.range();
               System.out.println("Minivan can carry "+minivan.passengers+" with range of "+range1+" Miles");
               System.out.println("Sportscar can carry "+sportscar.passengers+" with range of "+range2+" Miles");
   }
}

Java Program for COMMAND LINE ARGUMENTS


1.COMMAND LINE ARGUMENTS

class ComLineTest
{
   public static void main(String args[])
   {
               int count,i=0;
               String string;
               count=args.length;
               System.out.println("Number of Arguments="+count);
               while(i<count)
               {
                           string=args[i];
                           i=i+1;
                           System.out.println(i+": Java is "+string+"!");
               }
   }
}

WRITE A PROGRAM TO SUM OF TWO MATRICES



WRITE A PROGRAM TO SUM OF TWO MATRICES

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,k,r1,r2,c1,c2;
clrscr();
printf("input the order of matrix A:");
scanf("%d%d",&r1,&c1);
printf("input the order of matrix B:");
scanf("%d%d",&r2,&c2);
printf("enter the element of matrix A(row wise) \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}}
printf("enter the element of matrix B(row wise) \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}}
 printf("the element of matrix A is \n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
{
printf("%d",a[i][j]);
printf("\t");
}}

printf("the element of matrix B is \n");
for(i=0;i<r2;i++)
{
printf("\n");
for(j=0;j<c2;j++)
{
printf("%d",b[i][j]);
printf("\t");
}}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}}
printf("the addition of matrix is :");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c2;j++)
{
printf("%d",c[i][j]);
printf("\t");
}}
getch();
}






WRITE A C PROGRAM TO PRINT PYRAMID STAR


WRITE A PROGRAM TO PRINT PYRAMID STAR

#include<stdio.h>
#include<conio.h>
void main()
{
int i,a,j,k;
clrscr();
printf(“enter the number of line \n”);
scanf(“%d”,&a);
for(i=0;i<a;i++)
{
printf(“\n”);
for(j=0;j<10-i;j++)
{
printf(“ ”);
}
for(k=0;k<=2*i;k++)
{
printf(“*”);
}
}
getch();
}

WRITE A C PROGRAM TO SWAP OF TWO NUMBER


WRITE A PROGRAM TO SWAP OF TWO NUMBER

#include<stdio.h>
#include<conio.h>
void main()
{
int a,,b,temp;
clrscr();
printf(“Enter the number of a&b \n”);
scanf(“%d\n%d\n”,&a,&b);
temp=a;
a=b;
b=temp;
printf(“the swapped values are \n”);
printf(“%d\n%d\n”,a,b);
getch();
}

WRITE A C PROGRAM TO REVERSE OF NUMBER


WRITE A PROGRAM TO REVERSE OF NUMBER

#include<stdio.h>
#include<conio.h>
void main()
{
int n,b,s=0;
clrscr();
printf(“Enter the number \n”);
scanf(“%d”,&n);
while(n!=0)
{
b=n%10;
s=(s*10)+b;
n=n/10;
}
printf(“The reverse number is %d”,s);
getch();
}

WRITE A PROGRAM TO PRINT FIBONACCI SERIES


WRITE A PROGRAM TO PRINT FIBONACCI SERIES

#include<stdio.h>
#include<conio.h>
void main()
{
int f,s,n,c,t;
clrscr();
printf(“Enter the number of series \n”);
scanf(“%d”,&n);
f=1;
s=1;
printf(“%d\t”,f);
printf(“%d\t”,s);
for(c=1;c<=n-2;c++)
{
t=f+s;
printf(“%d\t”,t);
f=s;
s=t;
}
getch();
}

WRITE A C PROGRAM TO FIND FACTORIAL OF A NUMBER


WRITE A PROGRAM TO FIND FACTORIAL OF A NUMBER

#include<stdio.h>
#include<conio.h>
void main()
{
int a,i.fact=1;
clrscr();
printf(“Enter the number \n”);
scanf(“%d%”,&a);
for(i=1;i<=a;i++)
{
fact=fact*i;
}
printf(“the factorial is %d”,fact);
getch();

}