Tuesday, 11 October 2011

Some Example programs

Inheritance Area:
import java.util.*;
class rectangle
{
int l,h,rect;
Scanner sc=new Scanner(System.in);
void get()
{
System.out.println("Enter The Length Of The Rectangle");
l=sc.nextInt();
System.out.println("Enter The Height of the Rectangle");
h=sc.nextInt();
rect=l*h;
}
void put()
{
System.out.println("Area Of Rectangle="+rect);
}
}
class square extends rectangle
{
int a,area;
void gett()
{
System.out.println("Enter The Value For The Square");
a=sc.nextInt();
area=a*a;
get();
}
void putt()
{
System.out.println("Area Of Square="+a);
put();
}
public static void main(String args[])
{
square sq=new square();
sq.gett();
sq.putt();
}
}


PROGRAM:
GRID LAYOUT:

import javax.swing.*;
import java.awt.*;

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

JFrame f=new JFrame();

JPanel p=new JPanel();
f.setTitle("Calculator");
f.setSize(300,300);

JButton b1=new JButton("ADD");
JButton b2=new JButton("sub");
JButton b4=new JButton("DIV");
JButton b5=new JButton("MUL");
JButton b6=new JButton("LOG");
JButton b7=new JButton("SQUARE");
JButton b8=new JButton("ROOT");
JButton b9=new JButton("1/X");
p.add(b1);
p.add(b2);

p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
f.add(p);
GridLayout g=new GridLayout(2,2);
p.setLayout(g);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);


}
}





SIGN IN FORM:

import javax.swing.*;
import java.awt.*;

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

JFrame f=new JFrame();

JPanel p=new JPanel();
f.setTitle("Calculator");
f.setSize(300,300);
JLabel jj=new JLabel("USER NAME");
JLabel jj1=new JLabel("PASSWORD");
JTextField th=new JTextField("");
JPasswordField ps=new JPasswordField("");
JButton b1=new JButton("SUBMIT");
p.add(jj);
p.add(th);
p.add(jj1);
p.add(ps);
p.add(b1,BorderLayout.SOUTH);
f.add(p);
GridLayout g=new GridLayout(1,1);
p.setLayout(g);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);


}
}
FACTORIAL USING PACKAGE:
package p1;
import java.util.*;
public class sss{

public void fact()
{
Scanner sc=new Scanner(System.in);
  System.out.println("enter the number");
  int a=sc.nextInt();
  int fact= 1;
  System.out.println("Factorial of " +a+ ":");
  for (int i= 1; i<=a; i++){
  fact=fact*i;
  }
  System.out.println(fact);
  }
 
Main File:
Import p1.*;
Class main
{
Public static void main(String args[])
{
 Sss s=new sss();
s.fact();
}
}


Factorial using interface:
import java.util.*;
interface if
{
public void fact();
}
class sss implements if
{
int a;
Scanner sc=new Scanner(System.in);
 
public void fact()
{

  System.out.println("enter the number");
 a=sc.nextInt();
  int fact= 1;
  System.out.println("Factorial of " +a+ ":");
  for (int i= 1; i<=a; i++){
  fact=fact*i;
  }
  System.out.println(fact);
  }
public static void main(String args[])
{
sss s=new sss();
s.fact();
}
}

No comments:

Post a Comment