0% found this document useful (0 votes)
2 views67 pages

Java Record

Uploaded by

sree.devnova
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views67 pages

Java Record

Uploaded by

sree.devnova
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA PROGRAMS INDEX-WISE

1. Write a java program to add two numbers.

//Addition of two numbers

import [Link].*;
class puppy
{
int a, b, total;
void add()
{
a=10;
b=20;
total=(a + b);
[Link]("Addition of (a+b)="+total);
}
}
class MPuppy
{
public static void main(String args[])
{
puppy obj=new puppy();
[Link]();
}
}

/*Output:Addition of (a+b)=30*/

3
JAVA PROGRAMS INDEX-WISE
2. Write a java program to add two numbers using method return value.

//Addition of two numbers by using method return value

import [Link].*;
class tom
{
int a,b;
int add()
{
a=10;
b=20;
return(a+b);
}
}
class Mtom
{
public static void main(String args[])
{
tom obj=new tom();
int total=[Link]();
[Link]("Addition of (a+b)="+total);
}
}

/*Output:Addition of (a+b)=30*/

4
JAVA PROGRAMS INDEX-WISE
3. Write a java program to add two numbers using method passing parameters.

//Addition of two numbers by using Method passing parameters

import [Link].*;
class Jerry
{
int a,b,total;
void add(int x,int y)
{
a=x;
b=y;
total=(a+b);
[Link]("Addition of (a+b)="+total);
}
}
class MJerry
{
public static void main(String args[])
{
Jerry obj=new Jerry();
[Link](22,33);
}
}

/*Output:Addition of (a+b)=55*/

5
JAVA PROGRAMS INDEX-WISE
4. Write a java Program two numbers using method over loading.

//Addition of two numbers by using method over loading

class Shinchan
{
int a,b,total;
void add()
{
a=10;
b=30;
total=(a+b);
[Link]("\n Addition of (a+b)="+total);
}
void add(int x)
{
a=x;
b=x;
total=(a+b);
[Link]("\n Addition of(a+b)="+total);
}
void add(int x,int y)
{
a=x;
b=y;
total=(a+b);
[Link]("\n Addition of (a+b)="+total);
}
}
class MShinchan
{
public static void main(String args[])
{
Shinchan obj=new Shinchan();
[Link]();
[Link](22);
[Link](22,44);
}
}

/*Output: Addition of (a+b)=40

Addition of (a+b)=44

Addition of (a+b)=66*/

6
JAVA PROGRAMS INDEX-WISE
5. Write a java program to add two numbers by using constructor.

//Addition of two numbers by using constructor

import [Link].*;
class Dc
{
int a, b, total;
Dc()
{
a=10;
b=20;
}
void sum()
{
total=a+b;
[Link]("Sum="+total);
}
}
class MDc
{
public static void main(String args[])
{
Dc obj=new Dc();
[Link]();
}
}

/* Output :Sum=30 */

7
JAVA PROGRAMS INDEX-WISE
6. Write a java program to print a variable by using parameterized constructor.

//prints variable by using parameterized constructor

import [Link].*;
class Pc
{
int x;
Pc(int a)
{
x=a;
}
void display()
{
[Link]("x="+x);
}
}
class MPc
{
public static void main(String args[])
{
Pc obj1=new Pc(10);
Pc obj2=new Pc(20);
Pc obj3=new Pc(30);
[Link]();
[Link]();
[Link]();
}
}

/* Output :x=10
x=20
x=30 */

8
JAVA PROGRAMS INDEX-WISE
7. Write a java program two numbers using constructor over loading.

// Adding two numbers by using constructor overloading

import [Link].*;
class Add
{
int x,y,total;
Add()
{
x=10;
y=10;
}
Add(int a)
{
x=a;
y=a;
}
Add(int a,int b)
{
x=a;
y=b;
}
void sum()
{
total=x+y;
[Link]("Sum="+total);
}
}
class MAdd
{
public static void main(String args[])
{
Add obj=new Add();
Add obj1=new Add(10);
Add obj2=new Add(20,30);
[Link]();
[Link]();
[Link]();
}
}

/* Output :Sum=20
Sum=20
Sum=50 */

9
JAVA PROGRAMS INDEX-WISE
8. Write a java program to add two numbers using “this” keyword.

//Keyword "this"

import [Link].*;
class Keyword
{
int x,y,total;
void add(int x,int y)
{
this.x=x;
this.y=y;
total=x+y;
[Link]("Sum="+total);
}
}
class MKeyword
{
public static void main(String args[])
{
Keyword obj=new Keyword();
[Link](11,22);
}
}

/* Output :Sum=33 */

10
JAVA PROGRAMS INDEX-WISE
9. Write a java program to display array of elements in 1Dim.

//Display array elements of 1 dim

import [Link].*;
class M1d
{
public static void main(String args[])
{
int x[]=new int[5];
x[0]=1;
x[1]=2;
x[2]=3;
x[3]=4;
x[4]=5;
[Link]("1d array elements are:");
for(int i=0;i<=4;i++)
{
[Link]("x[i]="+x[i]);
}
}
}

/* Output: 1D array elements are:


x[0]=1
x[1]=2
x[2]=3
x[3]=4
x[4]=5 */

11
JAVA PROGRAMS INDEX-WISE
10. Write a java program display array of elements in 2Dim.

//Display array elements of 2 dim

import [Link].*;
class M2d
{
public static void main(String args[])
{
int x[][]={{10,20,30},{40,50,60}};
int i,j;
[Link]("2d elements are:");
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
{
[Link](x[i][j]);
}
[Link]();
}
}
}

/* Output : 2d elements are:


102030
405060 */

12
JAVA PROGRAMS INDEX-WISE
11. Write a java program to add two numbers using Single inheritance.

//Single inheritance

import [Link].*;
class A
{
int x;
void getx()
{
x=100;
[Link]("x value is"+x);
}
}
class B extends A
{
int y,total;
void gety()
{
y=200;
[Link]("y value is"+y);
}
void sum()
{
total=x+y;
[Link]("Addition of x,y is"+total);
}
}
class Si
{
public static void main(String args[])
{
B obj=new B();
[Link]();
[Link]();
[Link]();
}
}

/* Output: x value is100


y value is200
Addition of x,y is300 */

13
JAVA PROGRAMS INDEX-WISE
12. Write a java program to add two numbers by using Multi-level inheritance.

//Multi level inheritance

import [Link].*;
class sup
{
int x,y;
void getx()
{
x=110;
[Link]("x value is"+x);
}
}
class sub1 extends sup
{
int y;
void gety()
{
y=220;
[Link]("y value is"+y);
}
}
class sub2 extends sub1
{
int total;
void sum()
{
total=x+y;
[Link]("sum="+total);
}
}
class Mi
{
public static void main(String args[])
{
sub2 obj=new sub2();
[Link]();
[Link]();
[Link]();
}
}

/* Output: x value is110


y value is220
sum=330 */

14
JAVA PROGRAMS INDEX-WISE
13. Write a java program to add two numbers using Hierarchical inheritance.

//Hierarchical inheritance

import [Link].*;
class sup
{
int x,y;
void getx()
{
x=100;
}
void gety()
{
y=200;
}
}
class sub1 extends sup
{
int sum;
void add()
{
sum=x+y;
[Link]("sum="+sum);
}
}
class sub2 extends sub1
{
int S;
void minus()
{
S=x-y;
[Link]("Minus="+S);
}
}
class Hi
{
public static void main(String args[])
{
sub1 obj1=new sub1();
sub2 obj2=new sub2();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}

/* Output: sum=300
Minus=-100 */

15
JAVA PROGRAMS INDEX-WISE
14. Write a java program for interface.

//program for interface

interface balu
{
public final int Empcode=535;
public final String Empname="balu";
public abstract void emp();
}
class emp1 implements balu
{
public void emp()
{
[Link]("Emp code is "+Empcode);
[Link]("Emp name is "+Empname);
}
}
class Memp
{
public static void main(String args[])
{
emp1 obj=new emp1();
[Link]();
}
}

/*output: Emp code is 535


* Emp name is balu
*/

16
JAVA PROGRAMS INDEX-WISE
15. Write a java program to add two numbers by using Multiple inheritance.

//Multiple inheritance

import [Link].*;
interface sup1
{
public final int x=10;
public abstract void getx();
}
interface sup2
{
public final int y=20;
public abstract void gety();
}
class sub implements sup1 , sup2
{
public void getx()
{
[Link]("x="+x);
}
public void gety()
{
[Link]("y="+y);
}
public void sum()
{
[Link]("sum="+(x+y));
}
}

public class Mii


{
public static void main(String args[])
{
sub obj=new sub();
[Link]();
[Link]();
[Link]();
}
}

/* output: x=10
y=20
sum=30
*/

17
JAVA PROGRAMS INDEX-WISE
16. Write a java program to print abstract method & class.

// Abstract class & abstract method

import [Link].*;
abstract class dad
{
void method()
{
[Link]("This is a method in the abstract class dad");
}
abstract void method2();
}

class mom extends dad


{
void method2()
{
[Link]("This is the implementation of method2 in the child class");
}
}
class Mmom
{
public static void main(String[] args)
{
mom obj = new mom();
[Link](); // Calls the method from the abstract class
obj.method2(); // Calls the implemented method from the child class
}
}

/* Output: This is a method in the abstract class dad


This is the implementation of method2 in the child class */

18
JAVA PROGRAMS INDEX-WISE
17. Write a java program for Multi-threading.

import [Link].*;
class balu extends Thread
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
[Link](i);
}
}
class Mbalu
{
public static void main(String args[])
{
balu o1=new balu();
Thread t1=new Thread(o1);
[Link]();
}
}

/* Output: 1
2
3
4
5 */

19
JAVA PROGRAMS INDEX-WISE
18. Write a java program for String class methods

import [Link].*;
class St
{
public static void main(String args[])
{
String S1="WELCOME JAVA";
String S2="apple";
[Link]("Length of S1: "+[Link]());
[Link]([Link]());
[Link]([Link]());
[Link]([Link](S2));
[Link]([Link]('L'));
[Link]([Link](2));
String S3=" hello world ";
[Link]([Link]());
String S4="";
[Link]([Link]());
}
}

/* Output:
Length of S1: 12
welcome java
APPLE
WELCOME JAVAapple
2
L
hello world
True */

20
JAVA PROGRAMS INDEX-WISE
19. Write a java program for applet.

// Applet program

import [Link];
import [Link];
public class Demo extends Applet
{
public static void main(String[] args)
{
Demo applet = new Demo();
[Link]();
[Link]();
}

public void paint(Graphics g)


{
[Link]("Welcome to applet program", 50, 50);
}
}
//html program

<applet code=”Demo class” width=200 height=200></applet>

21
JAVA PROGRAMS INDEX-WISE
20. Write a java program for AWT component by extending frame class.

// awt example by extending frame class.

import [Link].*;
class Radha extends Frame
{
public static void main(String args[])
{
Frame F=new Frame();
Button b=new Button("click me");
[Link](30,100,80,30);
[Link](b);
[Link](300,300);
[Link](null);
[Link](true);
}
}

/* Output:

22
JAVA PROGRAMS INDEX-WISE
21. Write a java AWT example by using Association.

// awt example by Association.

import [Link].*;
class Krishna
{
public static void main(String args[])
{
Frame F=new Frame();
Button b=new Button("click me");
[Link](30,100,80,30);
[Link](b);
[Link](300,300);
[Link](null);
[Link](true);
}
}

/* Output:

23
JAVA PROGRAMS INDEX-WISE
22. Write a java AWT example for Textfield.

import [Link];
import [Link];
import [Link].*;
public class Tf
{
public static void main(String[] args)
{
Frame F = new Frame("Button Example");
TextField t1,t2;
t1=new TextField("Welcome to java");
[Link](30,100,200,30);
t2=new TextField("AWT");
[Link](30,150,200,30);
[Link](t1);
[Link](t2);
[Link](300, 300);
[Link](null);
[Link](true);
}
}

/* Output:

24
JAVA PROGRAMS INDEX-WISE
23. Write a java program for “super” class.

//Super class example

import [Link].*;
class One
{
int x;
One(int a)
{
x=a;
}
}
class Two extends One
{
int y;
Two(int a, int b)
{
super(a);
y=b;
}
void display()
{
[Link](x+y);
}
}
class MSup
{
public static void main(String args[])
{
Two obj=new Two(10,20);
[Link]();
}
}

// Output: 30

25
JAVA PROGRAMS INDEX-WISE
24. Write a java AWT example for Button.

// awt example by Association.

import [Link].*;
class Bt
{
public static void main(String args[])
{
Frame F=new Frame();
Button b=new Button("click me");
[Link](30,100,80,30);
[Link](b);
[Link](300,300);
[Link](null);
[Link](true);
}
}

/* Output:

26
JAVA PROGRAMS INDEX-WISE
25. Write a java AWT example for Text-Area.

import [Link];
import [Link];
import [Link].*;
public class Ta
{
public static void main(String[] args)
{
Frame F=new Frame("Welcome to PITT");
TextArea T=new TextArea("AWT");
[Link](30, 100, 200, 100);
[Link](T);
[Link](300, 300);
[Link](null);
[Link](true);
}
}

/* Output:

27
JAVA PROGRAMS INDEX-WISE
26. Write a java AWT example for Check-Box.

import [Link].*;
public class Cb
{
public static void main(String[] args)
{
Frame F=new Frame("Checkbox Example");
Checkbox C1=new Checkbox("c++");
Checkbox C2=new Checkbox("java", true);
Checkbox C3=new Checkbox("python",true);
[Link](C1);
[Link](C2);
[Link](C3);
[Link](300, 300);
[Link](new FlowLayout());
[Link](true);
}
}

/* Output:

28
JAVA PROGRAMS INDEX-WISE
27. Write a java AWT example for Radio-Button.

import [Link].*;
public class Rb
{
public static void main(String[] args)
{
Frame F=new Frame("Radio Button Example");
CheckboxGroup G=new CheckboxGroup();
Checkbox C1=new Checkbox("c++", G, false);
Checkbox C2=new Checkbox("java", G, true);
Checkbox C3=new Checkbox("python", G, false);
[Link](C1);
[Link](C2);
[Link](C3);
[Link](300, 300);
[Link](new FlowLayout());
[Link](true);
}
}

/* Output:

29
JAVA PROGRAMS INDEX-WISE
28. Write a java AWT example for Choice.

import [Link].*;
public class Ce
{
public static void main(String[] args)
{
Frame F=new Frame("choice example");
Choice C=new Choice();
[Link]("c++");
[Link]("java");
[Link]("python");
[Link](C);
[Link](300, 300);
[Link](new FlowLayout());
[Link](true);
}
}

/* Output:

30
JAVA PROGRAMS INDEX-WISE

29. Write a java AWT example for List.

import [Link].*;
public class Le
{
public static void main(String[] args)
{
Frame F=new Frame("List Example");
List L=new List();
[Link]("c++");
[Link]("java");
[Link]("python");
[Link](L);
[Link](300, 300);
[Link](new FlowLayout());
[Link](true);
}
}

/* Output:

31
JAVA PROGRAMS INDEX-WISE

30. Write a java AWT example for Label.

import [Link].*;
public class Lab {
public static void main(String[] args)
{
Frame F=new Frame("List Example");
Label l1,l2,l3;
l1=new Label("C++");
[Link](100, 100, 100, 100);
l2=new Label("JAVA");
[Link](100, 200, 100, 100);
l3=new Label("PYTHON");
[Link](100, 300, 100, 100);
[Link](l1);
[Link](l2);
[Link](l3);
[Link](300, 300);
[Link](new FlowLayout());
[Link](true);
}
}

/* Output:

32
JAVA PROGRAMS INDEX-WISE

31. Write a java AWT example for Scrollbar.

import [Link].*;
public class Sb
{
public static void main(String[] args)
{
Frame F=new Frame("Scrollbar Example");
Scrollbar S=new Scrollbar();
[Link](100,100,50,100);
[Link](S);
[Link](300,300);
[Link](null);
[Link](true);
}
}

/* Output:

33
JAVA PROGRAMS INDEX-WISE

32. Write a java AWT example for Menu & MenuItem & MenuBar.

import [Link].*;
public class Menui
{
public static void main(String[] args)
{
Frame F=new Frame("Menu and Menu item Example");
MenuBar MB=new MenuBar();
Menu menu=new Menu("menu");
Menu submenu=new Menu("Submenu");
MenuItem I1=new MenuItem("Item 1");
MenuItem I2=new MenuItem("Item 2");
MenuItem I3=new MenuItem("Item 3");
MenuItem I4=new MenuItem("Item 4");
[Link](I1);
[Link](I2);
[Link](I3);
[Link](I4);
[Link](submenu);
[Link](menu);
[Link](MB);
[Link](400,400);
[Link](null);
[Link](true);
}
}

/* Output:

34
JAVA PROGRAMS INDEX-WISE

33. Write a java AWT example for Canvas.

import [Link].*;
public class Can
{
public static void main(String[] args)
{
Frame F=new Frame("Canvas example");
[Link](new MyCanvas());
[Link](null);
[Link](400,400);
[Link](true);
}
}
class MyCanvas extends Canvas
{
public MyCanvas()
{
setBackground([Link]);
setSize(300,300);
}
public void paint(Graphics g)
{
[Link]([Link]);
[Link](75,75,150,150);
}
}

/* Output:

35
JAVA PROGRAMS INDEX-WISE

34. Write a java AWT example for Popup-Menu.

import [Link].*;
import [Link];
import [Link];
import [Link];
public class Pm
{
public static void main(String[] args)
{
Frame F=new Frame("Popup menu Example");
PopupMenu popupMenu=new PopupMenu("Edit");
MenuItem cut=new MenuItem("Cut");
[Link]("cut");
MenuItem copy=new MenuItem("Copy");
[Link]("copy");
MenuItem paste=new MenuItem("Paste");
[Link]("paste");
[Link](cut);
[Link](copy);
[Link](paste);
[Link](new MouseAdapter() {
public void mousePressed(MouseEvent e)
{
[Link](F, [Link](), [Link]());
}
});
[Link](popupMenu);
[Link](400,400);
[Link](null);
[Link](true);
}
}

/* Output:

36
JAVA PROGRAMS INDEX-WISE

35. Write a java AWT example for Panel.

import [Link].*;
public class panel
{
public static void main(String[] args)
{
Frame F=new Frame("Panel example");
Panel P=new Panel();
[Link]([Link]);
[Link](50,50,300,300);
Button B=new Button("Click Me");
[Link](100,100,100,50);
[Link]([Link]);
Button B2=new Button("No, Click Me");
[Link](100,200,100,50);
[Link]([Link]);
[Link](B);
[Link](B2);
[Link](P);
[Link](null);
[Link](400,400);
[Link](true);
}
}
/* Output:

37
JAVA PROGRAMS INDEX-WISE

36. Write a java AWT example for Dialog.

import [Link].*;
import [Link].*;
public class Di
{
public static void main(String[] args)
{
Frame F=new Frame("Dialog Example");
Button B=new Button("Click Me");
[Link](150,100,100,50);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Dialog D=new Dialog(F,"Dialog Box",true);
[Link](new FlowLayout());
[Link](300,200);
Label L=new Label("This is a Dialog Box");
Button B1=new Button("OK");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e)
{
[Link](false);
}
});
[Link](L);
[Link](B1);
[Link](true);
}
});
[Link](B);
[Link](400,400);
[Link](null);
[Link](true);
}
}

/* Output:

38
JAVA PROGRAMS INDEX-WISE

37. Write a java JComponents example for JPanel.

import [Link].*;
public class Jpanel
{
public static void main(String[] args)
{
JFrame F=new JFrame("Panel example");
JPanel P=new JPanel();
[Link]([Link]);
[Link](50,50,300,300);
JButton B=new JButton("Click Me");
[Link](100,100,100,50);
[Link]([Link]);
JButton B2=new JButton("No, Click Me");
[Link](100,200,100,50);
[Link]([Link]);
[Link](B);
[Link](B2);
[Link](P);
[Link](null);
[Link](400,400);
[Link](true);
}
}

/* Output:

39
JAVA PROGRAMS INDEX-WISE

38. Write a java JComponents example for JButton,

import [Link].*;
class Bt
{
public static void main(String args[])
{
JFrame F=new JFrame();
JButton b=new JButton("click me");
[Link](30,100,80,30);
[Link](b);
[Link](300,300);
[Link](null);
[Link](true);
}
}

/* Output:

40
JAVA PROGRAMS INDEX-WISE

39. Write a java JComponents example for JLabel.

import [Link].*;
public class JLab
{
public static void main(String[] args)
{
JFrame F=new JFrame("List Example");
JLabel l1,l2,l3;
l1=new JLabel("C++");
[Link](100, 100, 100, 100);
l2=new JLabel("JAVA");
[Link](100, 200, 100, 100);
l3=new JLabel("PYTHON");
[Link](100, 300, 100, 100);
[Link](l1);
[Link](l2);
[Link](l3);
[Link](300, 300);
[Link](new FlowLayout());
[Link](true);
}
}

/* Output:

41
JAVA PROGRAMS INDEX-WISE

40. Write a java JComponents example for JTextField.

import [Link].*;
import [Link].*;
import [Link];
import [Link];
public class JTf
{
public static void main(String[] args)
{
JFrame F = new JFrame();
JTextField t1,t2;
t1=new JTextField("Welcome to java");
[Link](30,100,200,30);
t2=new JTextField("AWT");
[Link](30,150,200,30);
[Link](t1);
[Link](t2);
[Link](300, 300);
[Link](null);
[Link](true);
}
}

/* Output:

42
JAVA PROGRAMS INDEX-WISE

41. Write a java JComponents example for JTextArea.

import [Link].*;
public class JTa
{
public static void main(String[] args)
{
JFrame F=new JFrame("Welcome to PITT");
JTextArea T=new JTextArea("AWT");
[Link](30, 100, 200, 100);
[Link](T);
[Link](300, 300);
[Link](null);
[Link](true);
}
}

/* Output:

43
JAVA PROGRAMS INDEX-WISE

42. Write a java JComponents example for JRadioButton.

import [Link];

import [Link].*;
public class JRb
{
public static void main(String args[])
{
JFrame F=new JFrame();
JRadioButton r1=new JRadioButton("Male");
[Link](75,50,100,30);
JRadioButton r2=new JRadioButton("Female");
[Link](75,100,100,30);
ButtonGroup bg=new ButtonGroup();
[Link](r1);
[Link](r2);
[Link](r1);
[Link](r2);
[Link](300,300);
[Link](null);
[Link](true);
}
}

/* Output:

44
JAVA PROGRAMS INDEX-WISE

43. Write a java JComponents example for JCheckBox.

import [Link].*;
public class JCb
{
public static void main(String args[])
{
JFrame F=new JFrame();
JCheckBox c1=new JCheckBox("JAVA");
[Link](100,100,100,50);
JCheckBox c2=new JCheckBox("PYTHON",true);
[Link](100,150,100,50);
[Link](c1);
[Link](c2);
[Link](400,400);
[Link](null);
[Link](true);
}
}

/* Output:

45
JAVA PROGRAMS INDEX-WISE

44. Write a java JComponents example for JPasswordField.

import [Link].*;
class JPf
{
public static void main(String args[])
{
JFrame F=new JFrame();
JPasswordField P=new JPasswordField();
JLabel l1=new JLabel("Password");
[Link](50,50,100,30);
[Link](150,50,150,30);
[Link](l1);
[Link](P);
[Link](400,400);
[Link](null);
[Link](true);
}
}

/* Output:

46
JAVA PROGRAMS INDEX-WISE

45. Write a java JComponents example for JMenuItem , JMenu & JMenuBar.

import [Link].*;
class JMe
{
public static void main(String args[])
{
JFrame F=new JFrame();
JMenuBar mb=new JMenuBar();
JMenu menu=new JMenu("Menu");
JMenu submenu=new JMenu("Submenu");
JMenuItem i1=new JMenuItem("Item1");
JMenuItem i2=new JMenuItem("Item2");
JMenuItem i3=new JMenuItem("Item3");
[Link](i1);
[Link](i2);
[Link](i3);
[Link](submenu);
[Link](menu);
[Link](mb);
[Link](400,400);
[Link](null);
[Link](true);
}
}

/* Output:

47
JAVA PROGRAMS INDEX-WISE

46. Write a java JComponents example for JPopupMenu.

import [Link].*;
class JPop
{
public static void main(String args[])
{
final JFrame F=new JFrame();
final JPopupMenu pf=new JPopupMenu("Edit");
JMenuItem m1=new JMenuItem("Cut");
JMenuItem m2=new JMenuItem("Copy");
JMenuItem m3=new JMenuItem("Paste");
[Link](m1);
[Link](m2);
[Link](m3);
[Link](new [Link]() {
public void mouseClicked([Link] evt) {
[Link](F,[Link](),[Link]());
}
});
[Link](pf);
[Link](400,400);
[Link](null);
[Link](true);
}
}

/* Output:

48
JAVA PROGRAMS INDEX-WISE

47. Write a java JComponents example for JTable.

import [Link].*;
public class Table
{
public static void main(String args[])
{
JFrame F=new JFrame("Table Example");
String data[][]={{"1","krishna","4th"},{"2","Gopi","2nd"},{"3","shyam","3rd"}};
String Column[]={"RNo","Name","Class"};
JTable T=new JTable(data,Column);
[Link](30,40,400,400);
[Link](T);
[Link](300,400);
[Link](null);
[Link](true);
}
}

/* Output:
1 krishna 4th
2 Gopi 2nd
3 shyam 3rd */

49
JAVA PROGRAMS INDEX-WISE

48. Write a java JComponents example for JFrame.

import [Link].*;
class JF
{
public static void main(String args[])
{
JFrame F=new JFrame();
JButton b=new JButton("click me");
[Link](30,100,80,30);
[Link](b);
[Link](300,300);
[Link](null);
[Link](true);
}
}

/* Output:

50
JAVA PROGRAMS INDEX-WISE

49. Write a java program to perform Arithmetic Operations.

import [Link].*;
class Aop
{
public static void main(String args[])
{
byte x=10;
byte y=20;
int z;
[Link]("Sum= "+(x+y));
[Link]("Differ= "+(x-y));
[Link]("Multiplication= "+(x*y));
z=x/y;
[Link]("Quotient= "+z);
z=x%y;
[Link]("Remainder= "+z);
}
}

/* Output:
Sum= 30
Differ= -10
Multiplication= 200
Quotient= 0
Remainder= 10 */

51
JAVA PROGRAMS INDEX-WISE

50. Write a java program to perform Unary Operations.

import [Link].*;
class Uop
{
public static void main(String[] args) {
int x=-4;
[Link]("Result of unary operator(-): "+(-x));
//x=++x;
[Link]("Result of unary operator(x++): "+(++x));
//x=--x;
[Link]("Result of unary operator(x--): "+(--x));
}
}

/* Output:
Result of unary operator(-): 4
Result of unary operator(x++): -3
Result of unary operator(x--): -4 */

52
JAVA PROGRAMS INDEX-WISE

51. Write a java program to perform Relational Operations.

import [Link].*;
class Rop
{
public static void main(String args[])
{
byte x=10;
byte y=5;
[Link]("Result of (x>y)= "+(x>y));
[Link]("Result of (x==y)= "+(x==y));
[Link]("Result of (x<y)= "+(x<y));
[Link]("Result of (x!=y): "+(x!=y));
}
}

/* Output:
Result of (x>y)= true
Result of (x==y)= false
Result of (x<y)= false
Result of (x!=y): true */

53
JAVA PROGRAMS INDEX-WISE

52. Write a java program to perform Logical Operations.

import [Link].*;
class Lop
{
public static void main(String args[])
{
byte x=10;
byte y=20;
byte z=30;
if(x<y && x<z)
[Link]("x is smaller than y,z");
if(x>y || x==10)
[Link]("x is +ve");
if(!(x<y))
[Link]("x is not lessthan y");
if(!(x>y))
[Link]("x is less than y");
}
}

/* Output:
x is smaller than y,z
x is +ve
x is less than y */

54
JAVA PROGRAMS INDEX-WISE

53. Write a java program to perform Boolean Operations.

import [Link].*;
class Boolop
{
public static void main(String args[])
{
boolean a=true;
boolean b=false;
[Link](a&b);
[Link](a|b);
[Link](!a);
}
}

/* Output:
false
true
False */

55
JAVA PROGRAMS INDEX-WISE

54. Write a java program to perform Bitwise Operations.

import [Link].*;
class Bitop
{
public static void main(String args[])
{
byte x=10;
byte y=4;
[Link](~x);
[Link](x&y);
[Link](x|y);
[Link](x^y);
[Link](x<<2);
[Link](y>>2);
}
}

/* Output:
-11
0
14
14
40
1 */

56
JAVA PROGRAMS INDEX-WISE

55. Write a java program to perform Ternary Operations.

import [Link].*;
class Top
{
public static void main(String args[])
{
byte x=10;
byte y=4;
[Link]((x>y)?"X is big":"Y is big");
}
}

/* Output:
X is big */

57
JAVA PROGRAMS INDEX-WISE

56. Write a java program by using Selection statements.

import [Link].*;
class Selection
{
public static void main(String args[])
{
int x=10;
if(x==5)
{
[Link]("Hello");
[Link]("x= "+x);
}
else if(x==7)
{
[Link]("Welcome");
[Link]("x= "+x);
}
else
{
[Link]("value of x= "+x);
x=x+10;
[Link]("value of x= "+x);
}
}
}

/* Output:
value of x= 10
value of x= 20 */

58
JAVA PROGRAMS INDEX-WISE

57. Write a java program by using Switch statements.

class Switch
{
public static void main(String[] args)
{
int a=10;
switch(a)
{
case 0: [Link]("a is zero");
break;
case 5: [Link]("a is five");
break;
case 7: [Link]("a is seven");
break;
default:[Link]("a is other");
}
}
}

/* Output:
a is other */

59
JAVA PROGRAMS INDEX-WISE

58. Write a java program by using Iterative statements.

class Istat
{
public static void main(String[] args) {
for(int i=1;i<=5;i++)
{
[Link]("i= "+i);
[Link](i+"Hello World");
}
int i=5;
while(i>0)
{
[Link]("i= "+i);
[Link]("Welcome Home");
i--;
}
do{
[Link]("i= "+i);
[Link]("Welcome Home");
i--;
}while(i>0);
}
}

/* Output:
i= 1
1Hello World
i= 2
2Hello World
i= 3
3Hello World
i= 4
4Hello World
i= 5
5Hello World
i= 5
Welcome Home
i= 4
Welcome Home
i= 3
Welcome Home
i= 2
Welcome Home
i= 1
Welcome Home
i= 0
Welcome Home */

60
JAVA PROGRAMS INDEX-WISE

59. Write a java program by using Looping Statements “Break”.

public class Break


{
public static void main(String[] args)
{
for (int i = 0; i <= 10; i++)
{
if (i == 5)
{
break; // Exit the loop when i is 6
}
[Link]("Iteration: " + i);
}
[Link]("Loop terminated.");
}
}

/* Output:
Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Loop terminated. */

61
JAVA PROGRAMS INDEX-WISE

60. Write a java program by using Looping Statements “Continue”.

public class Continue


{
public static void main(String[] args)
{
for(int i=0;i<=10;i++)
{
if(i==5)
{
continue; // Skip the iteration when i is 5
}
[Link]("Iteration: " + i);
}
}
}

/* Output:
Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 6
Iteration: 7
Iteration: 8
Iteration: 9
Iteration: 10 */

62
JAVA PROGRAMS INDEX-WISE

61. Write a java program by using Looping Statements “return”.

import [Link].*;
class Return
{
public static void main(String args[])
{
for(int i=0;i<=10;i++)
{
if(i==5)
return ;
[Link](i);
}
}
}
/* Output:
0
1
2
3
4 */

63
JAVA PROGRAMS INDEX-WISE

62. Write a java program by using Input Stream class.

import [Link].*;
import [Link].*;
import [Link].*;
class Ips
{
public static void main(String args[]) throws IOException
{
InputStreamReader i = new InputStreamReader([Link]);
BufferedReader b=new BufferedReader(i);
[Link]("Enter any character:");
char c=(char)[Link]();
[Link]("YOU entered the character c="+c);
[Link]("Hello"+ c+"welcome to java");
[Link]("Enter your name:");
String name =[Link]();
[Link]("Hello"+ name +"welcome to java");
[Link]("Enter a=");
int a=[Link]([Link]());
[Link]("Enter b=");
int input=[Link]([Link]());
int sum=a+input;
[Link]("sum of a,b="+sum);
}
}

/* Output:
Enter any character:
s
YOU entered the character c=s
Hello s welcome to java
Enter your name:
Hello welcome to java
Enter a=
12
Enter b=
14
sum of a,b=26 */

64
JAVA PROGRAMS INDEX-WISE

63. Write a java program by using Scanner class.

import [Link].*;
import [Link].*;
class sip
{
public static void main(String args[])
{
Scanner s=new Scanner([Link]);
[Link]("Enter a=");
int a=[Link]();
[Link]("Enter b=");
int b=[Link]();
int sum=a+b;
[Link]("sum of a,b="+sum);
}
}
/* Output:
Enter a=
12
Enter b=
13
sum of a,b=25 */

65
JAVA PROGRAMS INDEX-WISE

64. Write a java program using 1Dim.

import [Link].*;
class M1d
{
public static void main(String args[])
{
int x[]=new int[5];
x[0]=1;
x[1]=2;
x[2]=3;
x[3]=4;
x[4]=5;
[Link]("1d array elements are:");
for(int i=0;i<=4;i++)
{
[Link]("x[i]="+x[i]);
}
}
}

/* Output: 1D array elements are:


x[0]=1
x[1]=2
x[2]=3
x[3]=4
x[4]=5 */

66
JAVA PROGRAMS INDEX-WISE

65. Write a java program using 2Dim.

import [Link].*;
class M2d
{
public static void main(String args[])
{
int x[][]={{10,20,30},{40,50,60}};
int i,j;
[Link]("2d elements are:");
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
{
[Link](x[i][j]);
}
[Link]();
}
}
}

/* Output : 2d elements are:


102030
405060 */

67
JAVA PROGRAMS INDEX-WISE

66. Perform Arithmetic operations using Scanner class.

import [Link].*;
import [Link];
class Ao
{
void add()
{
int x,y;
[Link]("Enter two numbers: ");
Scanner sc = new Scanner([Link]);
x = [Link]();
y = [Link]();
[Link]("The sum is: " + (x+y));
}
void sub()
{
int x,y;
[Link]("Enter two numbers: ");
Scanner sc = new Scanner([Link]);
x = [Link]();
y = [Link]();
[Link]("The difference is: " + (x-y));
}
void mul()
{
int x,y;
[Link]("Enter two numbers: ");
Scanner sc = new Scanner([Link]);
x = [Link]();
y = [Link]();
[Link]("The product is: " + (x*y));
}
void div()
{
int x,y;
[Link]("Enter two numbers: ");
Scanner sc = new Scanner([Link]);
x = [Link]();
y = [Link]();
if (y != 0) {
[Link]("The quotient is: " + (x/y));
} else {
[Link]("Division by zero is not allowed.");
}
}
}

public class exam2 {


public static void main(String[] args) {
Ao obj = new Ao();
Scanner sc = new Scanner([Link]);
[Link]("Choose an operation: 1. Add 2. Subtract 3. Multiply 4. Divide");
int choice = [Link]();

68
JAVA PROGRAMS INDEX-WISE
switch (choice) {
case 1:
[Link]();
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
case 4:
[Link]();
break;
default:
[Link]("Invalid choice.");
}
}
}

/* output: Choose an operation: 1. Add 2. Subtract 3. Multiply 4. Divide


1
Enter two numbers:
2
3
The sum is: 5
*/

69

You might also like