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)
System.out.println(n1 + " is the largest number.");
else if (n2 >= n1 && n2 >= n3) System.out.println(n2 + " is the largest number.");
else
System.out.println(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;
}
System.out.println("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)
System.out.println(num + " is a prime number."); else
System.out.println(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;
}
System.out.println("G.C.D = " + n1);
}
}
Output: G.C.D = 9
5. Write a java program to implement Constructors
class abc
{
String language; abc()
{
this.language = "Java";
}
abc(String language)
{
this.language = language;
}
public void getName()
{
System.out.println("Programming Langauage: " + this.language);
}
public static void main(String[] args) { abc obj1 = new Main();
abc obj2 = new Main("Python");
obj1.getName(); obj2.getName();
}
}
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];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
Output:
666
7.2 Java
12 12Program to illustrate the use of Java Method Overriding
7 8 18
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
class Bike2 extends Vehicle{
void run(){System.out.println("Bike is running safely");}
public static void main(String args[])
{ Bike2 obj = new Bike2();
obj.run();
}
}
Output:
Bike is running safely
9.8.Single Inheritance
Overloading examplethe
by changing program
numberinofJava
arguments
class
ClassMethodOverloading
A {
{ private static void display(int a)
public void methodA()
{ System.out.println("Arguments: " + a);
{}
System.out.println("Base class method");
}private static void display(int a, int b)
} { System.out.println("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
{
System.out.println("Child class method");
}
public static void main(String args[])
{
B obj = new B();
obj.methodA();
obj.methodB();
}
}
Output:
Base class method
Child class method
10. Multilevel Inheritance example program in Java
Class X
{
public void methodX()
{
System.out.println("Class X method");
}
}
Class Y extends X
{
public void methodY()
{
System.out.println("class Y method");
}
}
Class Z extends Y
{
public void methodZ()
{
System.out.println("class Z method");
}
public static void main(String args[])
{
Z obj = new Z();
obj.methodX();
obj.methodY();
obj.methodZ();
}
}
Output:
11. Write a program to implement hierarchical inheritance
class A
{
public void methodA()
{
System.out.println("method of Class A");
}
}
class B extends A
{
public void methodB()
{
System.out.println("method of Class B");
}
}
class C extends A
{
public void methodC()
{
System.out.println("method of Class C");
}
}
class D extends A
{
public void methodD()
{
System.out.println("method of Class D");
}
}
class JavaExample
{
public static void main(String args[])
{
B obj1 = new B();
C obj2 = new C();
D obj3 = new D();
obj1.methodA();
obj2.methodA();
obj3.methodA();
}
}
Output:
method of Class
A method of
Class A method
of Class A
class C
{
public void disp()
{
System.out.println("C");
}
}
class A extends C
{
public void disp()
{
System.out.println("A");
}
}
class B extends C
{
public void disp()
{
System.out.println("B");
}
class D extends A
{
public void disp()
{
System.out.println("D");
}
public static void main(String args[]){
D obj = new D();
obj.disp();
}
}
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;
System.out.println("Rest of code in try block");
}
catch (ArithmeticException e) { System.out.println("ArithmeticException => " + e.getMessage());
}
}
}
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) { System.out.println("ArithmeticException => " + e.getMessage());
}
finally {
System.out.println("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 = str.length()-1; i >= 0 ; i--) {
newStr = newStr.append(str.charAt(i));
if(str.equalsIgnoreCase(newStr.toString())) {
System.out.println("String is palindrome");
} else {
System.out.println("String is not palindrome");
}
Output: String is not palindrome
16. Write a program to sort given list of name into ascending order
mport java.util.Arrays;
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"};
Arrays.sort(countries);
System.out.println(Arrays.toString(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()
{ System.out.println("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
t1.start();
}
}
Output: thread is running...
18. Write a program to create new Thread by extending Thread class
class Multi extends Thread
{
public void run()
{ System.out.println("thread is running...");
}
public static void main(String args[])
{ Multi t1=new Multi();
t1.start();
}
}
Output:thread is running...
19. Write a java program to implement applet
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g)
{ g.drawString("welcome to applet",150,150);
}
}
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
To execute the applet by appletviewer tool, write in command prompt:
c:\>javac First.java
c:\>appletviewer First.java
20. Event Handling within same class
import java.awt.event.*;
import java.awt.*;
class EventActEx1 extends Frame implements ActionListener
{
TextField txtfld;
EventActEx1()
{
txtfld= new TextField();
txtfld.setBounds(65,60,190,20);
Button bt=new Button("Click
me"); bt.setBounds(100,120,80,30);
bt.addActionListener(this);
add(bt);add(txtfld);
setSize(350,350);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
txtfld.setText("welcome 2 c-sharpcorner.com");
}
public static void main(String args[])
{
new EventActEx1();
}
}
output:
21. Write a program to implement AWT compnents : Buttons,checkboxes,labels
import java.applet.Applet;
import java.awt.*;
public class ComponentApplet extends Applet
{
public void init()
{
Button b = new Button("Test Button");
this.add(b);
Checkbox cb = new Checkbox("Test Checkbox");
this.add(cb);
CheckboxGroup cbg = new CheckboxGroup();
this.add(new Checkbox("CB Item 1", cbg, false));
this.add(new Checkbox("CB Item 2", cbg, false));
this.add(new Checkbox("CB Item 3", cbg, true));
Choice choice = new Choice();
choice.addItem("Choice Item 1");
choice.addItem("Choice Item 2");
choice.addItem("Choice Item 3");
this.add(choice);
Label l = new Label("Test Label");
this.add(l);
TextField t = new TextField("Welcome",30); this.add(t);
}
Output:
22. Write a program to create swing application using Swing package
import java.awt.*;
import javax.swing.*;
public class PanelExample {
PanelExample()
{
JFrame f= new JFrame("Panel
Example"); JPanel panel=new JPanel();
panel.setBounds(40,80,200,200);
panel.setBackground(Color.gray);
JButton b1=new JButton("Button 1");
b1.setBounds(50,100,80,30);
b1.setBackground(Color.yellow);
JButton b2=new JButton("Button 2");
b2.setBounds(100,100,80,30);
b2.setBackground(Color.green);
panel.add(b1); panel.add(b2);
f.add(panel);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(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() {
System.out.println("Animal is eating");
public void travel() {
System.out.println("Animal is travelling");
public class Demo {
public static void main(String args[]) {
Animal a = new Animal();
a.eat();
a.travel();
Output
Animal is eating
Animal is travelling