1.
Write a program to find largest of 3 numbers
public class Largest {
public static void main (String[] args) { double n1 = -4.5, n2 = 3.9, n3 = 2.5; if( n1 >= n2 && n1
>= n3)
[Link](n1 + " is the largest number.");
else if (n2 >= n1 && n2 >= n3) [Link](n2 + " is the largest number.");
else
[Link](n3 + " is the largest number.");
}
}
Output: 3.9 is the largest number.
2 .Write a program to Reverse a number
public class ReverseNumber {
public static void main(String[] args) { int num = 1234567, reversed = 0; for(;num != 0; num /= 10) {
int digit = num % 10;
reversed = reversed * 10 + digit;
}
[Link]("Reversed Number: " + reversed);
}
}
Output: Reversed Number: 7654321
3. Write a Program to Check given number is prime or not
public class Main {
public static void main(String[] args) { int num = 33, i = 2;
boolean flag = false; while (i <= num / 2) {
// condition for nonprime number if (num % i == 0) {
flag = true; break;
}
++i;
}
if (!flag)
[Link](num + " is a prime number."); else
[Link](num + " is not a prime number.");
}
}
Output: 33 is not a prime number.
4. Write a program to Find GCD of two numbers
public class GCD {
public static void main(String[] args)
{ int n1 = 81, n2 = 153;
while(n1 != n2)
{
if(n1 >
n2) n1
-= n2;
else
n2 -= n1;
}
[Link]("G.C.D = " + n1);
}
}
Output: G.C.D = 9
5. Write a java program to implement Constructors
class abc
{
String language; abc()
{
[Link] = "Java";
}
abc(String language)
{
[Link] = language;
}
public void getName()
{
[Link]("Programming Langauage: " + [Link]);
}
public static void main(String[] args) { abc obj1 = new Main();
abc obj2 = new Main("Python");
[Link](); [Link]();
}
}
Output:Programming Language: Java Programming Language: Python
6. Write a java to find multiplication of two
matrices public class MatrixMultiplicationExample
{
public static void main(String args[])
{
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
int c[][]=new int[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){ c[i]
[j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
[Link](c[i][j]+" ");
}
[Link]();
}
}
Output:
666
7.2 Java
12 12Program to illustrate the use of Java Method Overriding
7 8 18
class Vehicle{
void run(){[Link]("Vehicle is running");}
}
class Bike2 extends Vehicle{
void run(){[Link]("Bike is running safely");}
public static void main(String args[])
{ Bike2 obj = new Bike2();
[Link]();
}
}
Output:
Bike is running safely
[Link] Inheritance
Overloading examplethe
by changing program
numberinofJava
arguments
class
ClassMethodOverloading
A {
{ private static void display(int a)
public void methodA()
{ [Link]("Arguments: " + a);
{}
[Link]("Base class method");
}private static void display(int a, int b)
} { [Link]("Arguments: " + a + " and " + b);
}
Class B extends A
{
public void
public staticmethodB()
void main(String[] args)
{ display(1);
display(1, 4);
}
}
Output:
Arguments: 1
Arguments: 1 and 4
{
[Link]("Child class method");
}
public static void main(String args[])
{
B obj = new B();
[Link]();
[Link]();
}
}
Output:
Base class method
Child class method
10. Multilevel Inheritance example program in Java
Class X
{
public void methodX()
{
[Link]("Class X method");
}
}
Class Y extends X
{
public void methodY()
{
[Link]("class Y method");
}
}
Class Z extends Y
{
public void methodZ()
{
[Link]("class Z method");
}
public static void main(String args[])
{
Z obj = new Z();
[Link]();
[Link]();
[Link]();
}
}
Output:
11. Write a program to implement hierarchical inheritance
class A
{
public void methodA()
{
[Link]("method of Class A");
}
}
class B extends A
{
public void methodB()
{
[Link]("method of Class B");
}
}
class C extends A
{
public void methodC()
{
[Link]("method of Class C");
}
}
class D extends A
{
public void methodD()
{
[Link]("method of Class D");
}
}
class JavaExample
{
public static void main(String args[])
{
B obj1 = new B();
C obj2 = new C();
D obj3 = new D();
[Link]();
[Link]();
[Link]();
}
}
Output:
method of Class
A method of
Class A method
of Class A
class C
{
public void disp()
{
[Link]("C");
}
}
class A extends C
{
public void disp()
{
[Link]("A");
}
}
class B extends C
{
public void disp()
{
[Link]("B");
}
class D extends A
{
public void disp()
{
[Link]("D");
}
public static void main(String args[]){
D obj = new D();
[Link]();
}
}
Output: D
13. Write a program to how to handle Exceptions
class Main {
public static void main(String[] args) {
try {
// code that generate exception
int divideByZero = 5 / 0;
[Link]("Rest of code in try block");
}
catch (ArithmeticException e) { [Link]("ArithmeticException => " + [Link]());
}
}
}
Output: ArithmeticException => / by zero
14. Java Exception Handling using finally block
class Main {
public static void main(String[] args) { try {
// code that generates exception int divideByZero = 5 / 0;
}
catch (ArithmeticException e) { [Link]("ArithmeticException => " + [Link]());
}
finally {
[Link]("This is the finally block");
}
}
}
Output:ArithmeticException => / by zero This is the finally block
15. write a program to check whether the given string is palindrome or not
public class Palindrome {
public static void main(String[] args) {
String str = "SATYA";
StringBuffer newStr =new StringBuffer();
for(int i = [Link]()-1; i >= 0 ; i--) {
newStr = [Link]([Link](i));
if([Link]([Link]())) {
[Link]("String is palindrome");
} else {
[Link]("String is not palindrome");
}
Output: String is not palindrome
16. Write a program to sort given list of name into ascending order
mport [Link];
public class SortStringArrayExample2
{
public static void main(String args[])
{
String[] countries = {"Wood apple", "Blackberry", "Date", "Naseberry", "Tamarind", "Fig",
Mulberry", "Apple", "Plum", "Orange", "Custard apple", "Apricot"};
[Link](countries);
[Link]([Link](countries));
}
}
Output:
17. Write
[Apple, a program
Apricot, for creating
Blackberry, a new
Custard thread
apple, Date, by
Fig,implementing Runnable
Mulberry, Naseberry, Orange, Plum,
Tamarind,class
interface Wood apple]
Multi3 implements Runnable{
public void run()
{ [Link]("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
[Link]();
}
}
Output: thread is running...
18. Write a program to create new Thread by extending Thread class
class Multi extends Thread
{
public void run()
{ [Link]("thread is running...");
}
public static void main(String args[])
{ Multi t1=new Multi();
[Link]();
}
}
Output:thread is running...
19. Write a java program to implement applet
import [Link];
import [Link];
public class First extends Applet{
public void paint(Graphics g)
{ [Link]("welcome to applet",150,150);
}
}
/*
<applet code="[Link]" width="300" height="300">
</applet>
*/
To execute the applet by appletviewer tool, write in command prompt:
c:\>javac [Link]
c:\>appletviewer [Link]
20. Event Handling within same class
import [Link].*;
import [Link].*;
class EventActEx1 extends Frame implements ActionListener
{
TextField txtfld;
EventActEx1()
{
txtfld= new TextField();
[Link](65,60,190,20);
Button bt=new Button("Click
me"); [Link](100,120,80,30);
[Link](this);
add(bt);add(txtfld);
setSize(350,350);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
[Link]("welcome 2 [Link]");
}
public static void main(String args[])
{
new EventActEx1();
}
}
output:
21. Write a program to implement AWT compnents : Buttons,checkboxes,labels
import [Link];
import [Link].*;
public class ComponentApplet extends Applet
{
public void init()
{
Button b = new Button("Test Button");
[Link](b);
Checkbox cb = new Checkbox("Test Checkbox");
[Link](cb);
CheckboxGroup cbg = new CheckboxGroup();
[Link](new Checkbox("CB Item 1", cbg, false));
[Link](new Checkbox("CB Item 2", cbg, false));
[Link](new Checkbox("CB Item 3", cbg, true));
Choice choice = new Choice();
[Link]("Choice Item 1");
[Link]("Choice Item 2");
[Link]("Choice Item 3");
[Link](choice);
Label l = new Label("Test Label");
[Link](l);
TextField t = new TextField("Welcome",30); [Link](t);
}
Output:
22. Write a program to create swing application using Swing package
import [Link].*;
import [Link].*;
public class PanelExample {
PanelExample()
{
JFrame f= new JFrame("Panel
Example"); JPanel panel=new JPanel();
[Link](40,80,200,200);
[Link]([Link]);
JButton b1=new JButton("Button 1");
[Link](50,100,80,30);
[Link]([Link]);
JButton b2=new JButton("Button 2");
[Link](100,100,80,30);
[Link]([Link]);
[Link](b1); [Link](b2);
[Link](panel);
[Link](400,400);
[Link](null);
[Link](true);
}
public static void main(String args[])
{ new PanelExample(); } }
Output:
23. Write a program to implement multiple inheritance in JAVA
interface AnimalEat {
void eat();
interface AnimalTravel
{ void travel();
class Animal implements AnimalEat, AnimalTravel {
public void eat() {
[Link]("Animal is eating");
public void travel() {
[Link]("Animal is travelling");
public class Demo {
public static void main(String args[]) {
Animal a = new Animal();
[Link]();
[Link]();
Output
Animal is eating
Animal is travelling