You are on page 1of 20

Practical - Java

1. Write a java program to find Largest among 3 numbers.


Code:
import java.util.Scanner;
public class Biggest_Number
{
public static void main(String[] args)
{
int x, y, z;
Scanner s = new Scanner(System.in);
System.out.print("Enter the first number:");
x = s.nextInt();
System.out.print("Enter the second number:");
y = s.nextInt();
System.out.print("Enter the third number:");
z = s.nextInt();
if(x > y && x > z)
{
System.out.println("Largest number is:"+x);
}
else if(y > z)
{
System.out.println("Largest number is:"+y);
}
else
{
System.out.println("Largest number is:"+z);
}

}
}
Output:
2. Write a program in java to take two numbers as input from command line interface and
display their sum.
Code:
public class sum
{
public static void main (String ar[])
{
int x,y,s;
x=Integer.parseInt(ar[0]);
y=Integer.parseInt(ar[1]);
s=x+y;
System.out.println("sum of "+x+"and "+y+" is "+s);
}
}
Output:
3. Write a program for finding factorial of a given number.

Code:
public class FactorialExample
{
public static void main(String[] args)
{
int i,fact=1;
int number =5;
for (i=1;i<=number;i++)
{
fact=fact*i;
}
System.out.println("Factorial of "+number+"is:"+fact);
}
}
Output:

4. Write a program to find the area of circle using constructor.

Code:
import java .util.*;
class Area
{
double area;
Area(double r)
{
area=(22*r*r)/7;
}
}
class AreaOfCircle
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the radius");
double rad=sc.nextDouble();
Area a= new Area(rad);
System.out.println("Area of circle :"+a.area);
}

Output:

5. Write a program in java to show the concept of method overloading.

Code:
class Overloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c,int num)
{
System.out.println(c+" " +num);
}
}
class DisplayOverloading
{
public static void main (String args[])
{
Overloading obj = new Overloading();
obj.disp('a');
obj.disp('a',10);
}
}

Output:

6. Write a program to print table of an integer number using array.

Code:
import java.util.Scanner;
public class TableExample
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a[]=new int[10];
System.out.print("Enter number: ");
//reading a number whose table is to be print
int num=sc.nextInt();
//loop start execution form and execute until the condition i<=10 becomes false
for(int i=1; i <= 10; i++)
{
a[i]=num*i;
//prints table of the entered number
System.out.println(num+" * "+i+" = "a[i]);
}
}
}

Output:

7. Write a program in java to create thread by extending thread class.

Code:
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:
8. Write a program to show concept of multilevel inheritance.

Code:
class Car
{
public Car()
{
System.out.print("Class car");
}
public void vehicleType()
{
System.out.println("Vehicle Type:Car");
}
}
class maruti extends Car
{
public maruti()
{
System.out.println("class maurti");
}
public void brand()
{
System.out.println("brand: maruti");
}
public void speed()
{
System.out.println("Max:90kmph");
}
}
public class maruti800 extends maruti
{
public static void main (String args[])
{
maruti800 obj=new maruti800();
obj.vehicleType();
obj.brand();
obj.speed();
}
}

Output:

9. Write a program in java to show the concept of method overriding.

Code:
class Vehicle
{
void run ()
{
System.out.println("Vehicle is running");
}
}
class Bike2 extends Vehicle
{
void run ()
{
System.out.println("Bike isrunning safely");
}
public static void main(String args[])
{
Bike2 obj=new Bike2();
obj.run();
}
}

Output:

10. Write a program to illustrate an interface.

Code:
interface printable
{
void print();
}
class A6 implements printable
{
public void print()
{
System.out.println("Hello");
}
public static void main(String args[])
{
A6 obj=new A6();
obj.print();
}
}

Output:

11. Write a java program to show Abstraction in Class.

abstract class Bike


{
abstract void run();
}
class Honda extends Bike
{
void run()
{
System.out.println("running safely");
}
public static void main(String args[])
{
Bike obj = new Honda();
obj.run();
}
}

Output:

12. Write a program to handle the Exception using try and multiple catch blocks.

Code:
public class MultipleCatchBlock
{
public static void main(String[] args)
{
try
{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
catch+(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
}
Output:

13. Write a program to create user defined package and use it in other program.

Code:
package student;
public class Num
{
int a,b,c;
public void set(int i,int j)
{
a=i;
b=j;
}
public void add()
{
c=a+b;
}
public void show()
{
System.out.println("Sum="+c);
}
}
Compile by Command
Javac –d . Num.java
Now use this package in following Program
import student.Num;
class AddNum
{
public static void main(String []s)
{
Num n=new Num();
n.set(10,20);
n.add();
n.show();
}
}
Output:

14. Write a program to draw the line, rectangle, oval, text etc using the graphics method.

import java.applet.*;
import java.awt.*;
public class GApplet extends Applet
{
public void paint(Graphics g)
{
g.drawLine(20, 20, 500, 20);
g.drawRect(20, 40, 200, 40);
g.fillRect(300, 40, 200, 40);
g.drawRoundRect(20, 100, 200, 40, 10, 10);
g.fillRoundRect(300, 100, 200, 40, 10, 10);
g.setColor(Color.RED);
g.drawOval(20, 160, 200, 100);
g.fillOval(300, 160, 200, 100);
g.drawString("Hello World",150,150);
}
}
/*
<applet code="GApplet" height="500" width="700" border="1">
</applet>
*/
Output

15. Write a program using AWT to create two buttons named “Blue” and “Green”. When
a button is pressed the background colour should be set to the colour named by the
button’s label.
import java.awt.*;
import java.awt.event.*;
public class Main extends Frame implements ActionListener
{
Button btnBlue, btnGreen;
Main()
{
super("AWT Buttons");
btnBlue = new Button("Blue");
btnBlue.setBounds(25, 50, 250, 30);
btnBlue.addActionListener(this);
this.add(btnBlue);
btnGreen = new Button("Green");
btnGreen.setBounds(25, 100, 250, 30);
btnGreen.addActionListener(this);
this.add(btnGreen);
this.setSize(300, 160);
this.setLayout(null);
this.setVisible(true);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});
}
public static void main(String[] args) {
new Main();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btnBlue) {
this.setBackground(Color.BLUE);
} else if (e.getSource() == btnGreen) {
this.setBackground(Color.GREEN);
}
}
}

Output:

16. Write a program in java to illustrate a basic applet.

Code:
import java.applet.Applet;
import java.awt.Graphics;
public class TestApplet extends Applet
{
public void paint(Graphics g){
g.drawString("Hello World",150,150);
}
}
/*
<applet code=" TestApplet.class" width="400" height="400">
</applet>
*/
Output:
17. Write a java program to read data of various types using Scanner class.

Code:
import java.util.Scanner;
public class ScannerDemo
{
public static void main(String[] args)
{
// Declare the object and initialize with
// predefined standard input object
Scanner sc = new Scanner(System.in);

// String input
String name = sc.nextLine();

// Character input
char gender = sc.next().charAt(0);

// Numerical data input


// byte, short and float can be read
// using similar-named functions.
int age = sc.nextInt();
long mobileNo = sc.nextLong();
double cgpa = sc.nextDouble();

// Print the values to check if the input was correctly obtained.


System.out.println("Name: "+name);
System.out.println("Gender: "+gender);
System.out.println("Age: "+age);
System.out.println("Mobile Number: "+mobileNo);
System.out.println("CGPA: "+cgpa);
}
}

Output:

18. Write a java program to add two numbers using BufferedReader.

Code:
import java.io.*;
public class SumOfTwoNumbers
{
public static void main(String[] args) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
double a, b, sum;
System.out.print("Enter the value of a: ");
a = Double.parseDouble(in.readLine());
System.out.print("Enter the value of b: ");
b = Double.parseDouble(in.readLine());
sum=a+b;
System.out.println("Value of sum: " + sum);
in.close();
}
}

Output:

19. Write a program to create a text File using FileWriter.

Code:
import java.io.FileWriter;
public class FileWriterExample {
public static void main(String args[])
{
try
{
FileWriter fw=new FileWriter("D:\\test.txt");
fw.write("Example of FileWriter Class");
fw.close();
}catch(Exception e){System.out.println(e);}
System.out.println("Success...");
}
}

Output:
20. Write a program to read from text File using FileReader.

Code:
import java.io.FileReader;
public class FileReaderExample {
public static void main(String args[])throws Exception{
FileReader fr=new FileReader("D:\\test.txt");
int i;
while((i=fr.read())!=-1)
System.out.print((char)i);
fr.close();
}
}

Output:

You might also like