0% found this document useful (0 votes)
24 views33 pages

Java File

The document contains programs written in Java with outputs and signatures. It includes programs to print 'Hello World', find the largest of three numbers, calculate the factorial of a number, print the multiplication table of a number, convert between binary and decimal numbers, and use a switch statement for a basic calculator.

Uploaded by

akashbaloni10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
24 views33 pages

Java File

The document contains programs written in Java with outputs and signatures. It includes programs to print 'Hello World', find the largest of three numbers, calculate the factorial of a number, print the multiplication table of a number, convert between binary and decimal numbers, and use a switch statement for a basic calculator.

Uploaded by

akashbaloni10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 33

1

WORLD COLLEGE OF TECHNOLOGY AND


MANAGEMENT

B.TECH (AI&DS)
OOPs With Java (PRACTICAL)

Submitted by:- Submitted To:-

Name: - Ms. Shweta Mallick

Course:- B.TECH

Branch:-AI&DS(4TH sem)

_____
2

INDEX:-
S.NO. NAME OF PROGRAM SIGN
1. Write a program to print “Hello World”

2. Write a program to find largest of three numbers

3. Write a program to find factorial of a number

4. Write a program to take a number as input and print its


multiplication table upto 10
5. Write a program to convert a decimal number to binary number and
vice versa
6. Write a program for simple calculator using switch case
7. Write a program to find largest and smallest elements in array

8. Write a program to print diagonal matrix.

9. Write a program to sort the element of array in ascending and


descending order
10. Write a program to perform multiplication of matrix
11. Write a program to calculate area of rectangle using class and object
12. Write a program to show the use of ‘this’ keyword
13. Write a program to print the username by using Scanner class
14. Write a program to take the input data from user Bufferedclass

15. Write a program to calculatearea of rectangle using constructor


overloading
16. Write a program to implement single inheritance
17. Write a program to implement method overloading

18. Write a program to demonstrate the use of ‘super ’ keyword


19. Write a program to implement interface
20. Write a program to implement multiple inheritance interface

21. Write a program to implement abstract class


22. Write a program to illustrate the use of Wrapper class
23. Write a program to create and import a package to calculate
marks and print grade of student
24. Write a program to set the priority of thread in multithreading
25. Write a program for handling the uncaught exception using
finally

26. Write a program to implement exception handling using try catch

27. Write a program to implement sum of two numbers by applet


28. Write a program using :- A. List :-
B. Hashset :-

29. Write a program in which read a string and reverse its character
3

1. Write a program to print “Hello World”


class hello {
public static void main(String []args)
{
System.out.println("Hello World");
}
}

Output :-

2. Write a program to find largest of three numbers


import java.util.Scanner; class largest {
public static void main(String []args)
{
Scanner sc= new Scanner(System.in); System.out.println("Enter three numbers");
int x= sc.nextInt(); int y= sc.nextInt();
int z= sc.nextInt();
if(x>y && x>z)
{
System.out.println("Largest of three numbers is : "+x);
}
else if(y>x && y>z)
{
System.out.println("Largest of three numbers is : "+y);
} else {
System.out.println("Largest of three numbers is : "+z);
}
}
}
4

Output:-
5

3. Write a program to find factorial of a number


import java.util.Scanner; class factorial {
public static void main(String[] args)
{
System.out.println("Enter any number"); Scanner sc= new
Scanner(System.in);
int x= sc.nextInt(); int fact= 1;
for(int i=1; i<=x; i++)
{
fact = fact*i;
}
System.out.println("Factorial of "+x+" is : "+fact);
}
}
Output:-
6

4. Write a program to take a number as input and print its multiplication table upto 10

import java.util.Scanner; class table


{
public static void main(String []args)
{
Scanner sc = new Scanner(System.in); System.out.println("Enter any number");
int x= sc.nextInt();
System.out.println("Multiplication table of "+x+" upto 10 times : "); for(int i=1; i<=10;
i++)
{
System.out.println(x*i);
}
}
}
Output :-
7

5. Write a program to convert a decimal number to binary number and


vice versa
import java.util.Scanner;
class BinaryDecimal
{
public void binary()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter any Decimal number"); int x= sc.nextInt();
System.out.println(x+" = "+Integer.toBinaryString(x));
}
public void decimal()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter any Binary number"); String x=
sc.nextLine(); int dec= Integer.parseInt(x, 2);
System.out.println(x+" = "+dec);
}
public static void main(String []args)
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter 1 - Binary to Decimal"); System.out.println("Enter 2 - Decimal to
Binary");
int x= sc.nextInt();
BinaryDecimal b= new BinaryDecimal();
if(x==1)
b.binary(); else if(x==2)
b.decimal();
else
System.out.println("Invalid input");
}
}

Output :-
8

6. Write a program for simple calculator using switch case


import java.util.Scanner;
class t {
public void addition()
{
Scanner sc= new Scanner(System.in);
System.out.println("You have Selected for Addition"); System.out.println("Enter two number");
int a= sc.nextInt(); int b= sc.nextInt();
int sum= a+b;
System.out.println(a+" + "+b+" = "+(a+b));
}
public void subtraction()
{
Scanner sc= new Scanner(System.in);
System.out.println("You have Selected for Subtraction"); System.out.println("Enter two
number");
int a= sc.nextInt(); int b= sc.nextInt();
System.out.println(a+" - "+b+" = "+(a-b));
}
public void multiplication()
{
Scanner sc= new Scanner(System.in);
System.out.println("You have Selected for Multiplication"); System.out.println("Enter two
number");
int a= sc.nextInt(); int b= sc.nextInt();
System.out.println(a+" * "+b+" = "+(a*b));
}
public void division()
{
Scanner sc= new Scanner(System.in);
System.out.println("You have Selected for Division"); System.out.println("Enter two number");
int a= sc.nextInt(); int b= sc.nextInt();
System.out.println(a+" / "+b+" = "+(a/b));
}

public static void main(String []args)


{
System.out.println("Enter 1 - Addition; 2 - Subtraction; 3 - Multiplication; 4 - Division"); Scanner sc= new
Scanner(System.in);
int x= sc.nextInt(); t cal= new t();
switch(x)
{
case 1: cal.addition();
break; case 2: cal.subtraction();
break; case 3:
cal.multiplication();
break; case 4:
cal.division();
break; default :
System.out.println("Invalid input");
}
}
}
9

Output :-

7. Write a program to find largest and smallest elements in array

import java.util.Scanner; class large


{
public static void main(String []args)
{
int p=0;
Scanner sc= new Scanner(System.in);
System.out.println("Enter the size of array");
int x= sc.nextInt(); int a[]= new int[x];
System.out.println("Enter values in array");
for(int i=0; i<x; i++)
{
a[i]= sc.nextInt();
}
System.out.println("Elements in array :");
for(int i=0; i<x; i++)
{
System.out.println(a[i]);
}
10

for(int i=0; i<x; i++)


{ if(a[i]>p) p= a[i];
}
System.out.println("Largest element in array is : "+p); int t= a[0];
for(int i=0; i<x; i++)
{ if(a[i]<t) t= a[i];
}
System.out.println("Smallest element in array is : "+t);
}
}

Output :-
11

8. Write a program to print diagonal matrix.


import java.io.*;
public class printmatrix
{
public static void main(String[] args)
{
int i, j, k, n=3, m=3;
int arr[][]= {{3, 7, 9}, {2, 1, 5}, {8, 6, 4}};
System.out.println("Matrix elements in diagonal form are : "); for(k=0; k<=m-1; k++)
{ i=k;
j=0; while(i>=0)
{
System.out.println(arr[i][j]+"\t");
i=i-1; j=j+1;
}
}
for(k=1; k<=n-1; k++)
{
i=m-1; j=k;
while(j<=n-1)
{
System.out.println(arr[i][j]+"\t");
i=i-1;
j=j+1;
}
}
}
}
Output :-
12

9. Write a program to sort the element of array in ascending and


descending order
import java.util.Scanner; class sorting
{
public static void main(String[] args)
{
int temp=0;
Scanner sc= new Scanner(System.in); System.out.println("Enter size of Array");
int x= sc.nextInt(); int a[]= new int[x];
System.out.println("Enter elements in Array");
for(int i=0; i<x; i++)
{
a[i]= sc.nextInt();
}
System.out.println("elements in array are : ");
for(int i=0; i<x; i++)
{
System.out.print(a[i]+" ");
}
for(int i=0; i<x; i++)
{
for(int j=i+1; j<x; j++)
{
if(a[i]> a[j])
{
temp= a[i];
a[i]= a[j];
a[j]= temp;
}
}
}
System.out.println();
System.out.println("Elements of array sorted in ascending order :"); for(int i=0; i<x; i++)
{
System.out.print(a[i]+" ");
}
for(int i=0; i<x; i++)
{
for(int j=i+1; j<x; j++)
{
if(a[i]< a[j])
{
temp= a[i]; a[i]= a[j];
a[j]= temp;
}
}
}
System.out.println();
System.out.println("Elements of array sorted in descending order :"); for(int i=0; i<x; i++)
{
System.out.print(a[i]+" ");
}
}
13

}
Output:-
14

10.Write a program to perform multiplication of matrix

class matrixmultiply
{ 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 :-
15

11.Write a program to calculate area of rectangle using class and object


import java.util.Scanner; class area
{ public void display()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter length and breadth "); int l= sc.nextInt();
int b= sc.nextInt();
System.out.println("Area of Rectangle is : "+(l*b));
}
} class rectangle
{
public static void main(String[] args)
{
area a= new area();
a.display();
}
}

Output :-
16

12.Write a program to show the use of ‘this’ keyword


class Student
{
int roll;
String name; float fee;
Student(int roll, String name, float fee)
{ this.roll= roll;
this.name= name;
this.fee= fee;
}
void display()
{
System.out.println(roll+" "+name+" "+fee);
}
}
class testthis
{
public static void main(String[] args)
{
Student s1= new Student(101, "Gaurav", 5000); Student s2=
new Student(109, "Saurav", 8000); s1.display(); s2.display();
}
}

Output :-
17

13.Write a program to print the username by using Scanner class


import java.util.Scanner;
class username
{
public static void main(String[] args)
{
System.out.println("Enter username");
Scanner sc= new Scanner(System.in);
String user= sc.nextLine();
System.out.println("Username : "+user);
}
}

Output :-

14.Write a program to take the input data from user Bufferedclass


import java.io.*;
class buffer
{
public static void main(String []args) throws IOException
{
System.out.println(" Enter name ");
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String name= br.readLine();
System.out.println("name is "+name);
}
}

Output:-
18

15.Write a program to calculatearea of rectangle using constructor


overloading
class volume
{ int l, b, h, len, bth;
volume(int l, int b, int h)
{ this.l= l; this.b= b;
this.h= h;
System.out.println("Volume of cuboid : "+(l*b*h));
}
volume(int len, int bth)
{
this.len= len; this.bth= bth;
System.out.println("Area of rectangle : "+(len*bth));
}
public static void main(String[] args)
{
volume vol= new volume(5,4,8);
volume v= new volume(9,8);
}
}

Output :-
19

16.Write a program to implement single inheritance


class data
{ int x=25; int y= 20; public void
value()
{
System.out.println("x = "+x+", y = "+y);
}
}
class calculation extends data
{
public void calc()
{
System.out.println("Addition : x+y = "+(x+y));
System.out.println("Subtraction : x-y = "+(x-y));
System.out.println("Multiplication : x*y = "+(x*y));
System.out.println("Division : x/y = "+(x/y));
}
}
class inheritanceex
{
public static void main(String...args)
{

calculation c= new calculation(); c.value();


c.calc();
}
}

Output :-
20

17.Write a program to implement method overloading


import java.util.Scanner;
class overload
{
public void name()
{
System.out.println("Person details :-");
}
public void name(String name)
{
System.out.println("Name : "+name);
}
public void name(int age)
{
System.out.println("Age : "+age);
}
public static void main(String []args)
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter name and age of person"); String a=
sc.nextLine(); int b= sc.nextInt(); overload o= new overload();
o.name();
o.name(a);
o.name(b);
}
}

Output:-
21

18.Write a program to demonstrate the use of ‘super ’ keyword


class animal
{
String color= "white"; animal()
{
System.out.println("animal is created");
}
void eat()
{
System.out.println("eating");
}
}
class dog extends animal
{
String color = "black";
dog()
{
super(); //call parent class constructor ie. animal();
System.out.println("dog is created");
}
void eat()
{
System.out.println("eating bread");
}
void bark()
{
System.out.println("barking");
}
void work()
{
super.eat();
bark();
}
void printcolor()
{
System.out.println(color);
System.out.println(super.color);
}
}
class testsuper
{
public static void main(String[] args)
{
dog d= new dog(); d.work();
d.printcolor();

}
}
22

Output :-

19. Write a program to implement interface


interface interf
{
void method1(); void method2();
}
class test implements interf
{
public void method1()
{
System.out.println("Method one is called");
}
public void method2()
{
System.out.println("Method two is called");
}
} class inter
{
public static void main(String []args)
{
test t= new test(); t.method1();
t.method2();
}
}

Output :-
23

20. Write a program to implement multiple inheritance interface


interface interf1
{
int add(int a, int b); int sub(int a, int b);
}
interface interf2
{
int mul(int a, int b);
}
class test implements interf1, interf2
{
public int add(int a, int b)
{ return a+b;
}
public int sub(int a, int b)
{
return a-b;
}
public int mul(int a, int b)
{
return a*b;
}
} class multi
{
public static void main(String []args)
{
test t= new test();
System.out.println("Addition is "+ t.add(56, 63));
System.out.println("Addition is "+t.sub(59, 44));
System.out.println("Addition is "+t.mul(9, 7));
}
}

Output :-
24

21. Write a program to implement abstract class


import java.util.*;
abstract class shape
{ int l, b, r;
abstract void printarea();
Scanner sc= new Scanner(System.in);
}
class rectangle extends shape
{
void printarea()
{
System.out.println("Enter length and breadth of rectangle"); l= sc.nextInt(); b=
sc.nextInt();
System.out.println("Area of rectangle :- "+(l*b));
}
}
class circle extends shape
{ void printarea() {
System.out.println("Enter radius of circle");
r= sc.nextInt();
System.out.println("Area of circle :- "+(22*r*r/7));
}
}
class abstractarea
{
public static void main(String []args)
{
rectangle r= new rectangle(); r.printarea();
circle c= new circle(); c.printarea();
}
}

Output :-
25

22.Write a program to illustrate the use of Wrapper class


class wrap
{
public static void main(String []args)
{
int a= 10;
int i= Integer.valueOf(a);
Integer j= a;
System.out.println("a = "+a);
System.out.println("i = "+i);
System.out.println("j = "+j);
}
}

Output :-

23.Write a program to create and import a package to calculate marks and print grade
of student

➔ Creating package:-
package data; import java.util.*;
public class mark
{
public void input()
{
int m, p, c, e, h;
String name;
Scanner sc= new Scanner(System.in);
System.out.println("Enter name and marks of 5 subject"); name=
sc.nextLine(); m= sc.nextInt(); p= sc.nextInt(); c= sc.nextInt(); h=
sc.nextInt(); e= sc.nextInt();
System.out.println("name of student : "+name);
System.out.println("Marks of 5 subject :- ");
System.out.println("english :"+e);
System.out.println("maths :"+m);
System.out.println("physics :"+p);
System.out.println("chemistry :"+c);
System.out.println("hindi :"+h); int tot= (e+m+p+c+h);
int g= tot/5;
System.out.println("Total Marks :"+tot); if(g>=60)
26

System.out.println("A grade"); else if(g>=45 &&


g<60) System.out.println("B grade"); else if(g>=35
&& g<55) System.out.println("C grade"); else
System.out.println("fail");
}
public static void main(String []args)
{
mark m= new mark(); m.input();
}}

➔ Accessing data.mark package :-


import data.mark;
public class grade
{
public static void main(String []args)
{
System.out.println("we are importing data package"); mark m= new
mark(); m.input();
}
}

Output:-
27

24.Write a program to set the priority of thread in multithreading


class priority extends Thread
{
public void run()
{
System.out.println("run method");
}
public static void main(String []args)
{
priority p1= new priority(); priority p2= new
priority(); p1.setPriority(4); p2.setPriority(7);
System.out.println("priority of thread 1 is : "+p1.getPriority());
System.out.println("priority of thread 2 is : "+p2.getPriority()); p1.start();
p2.start();
}
}

Output :-
28

25. Write a program for handling the uncaught exception using finally
import java.util.*; public class finallyex
{
public static void main(String []args) throws Exception
{
System.out.println("Enter two number"); Scanner sc= new
Scanner(System.in);
int a= sc.nextInt();
int b= sc.nextInt();
try{ int c= a/b;
System.out.println("a/b = "+c);
}
catch(Exception e)
{
System.out.println("Arithmetic Exception - Divide by 0");
} finally{
System.out.println("Finally
Bye");
}
}
}

Output :-
29

26.Write a program to implement exception handling using try catch


class exphandle
{
public static void main(String []args)
{ try{
int x= 19/0;
}
catch(Exception e)
{
System.out.println("Divide by zero");
}
System.out.println("try - catch example");
}
}

Output :-

27. Write a program to implement sum of two numbers by applet

➔ Java file :-
import java.awt.*; import java.awt.event.*;

import java.applet.*; public class summ

extends Applet implements ActionListener

{ int x, y, z;

Label l1= new Label("Enter first number");

TextField t1= new TextField(10);

Label l2= new Label("Enter second number");


30

TextField t2= new TextField(10); Button b= new

Button("Add"); public void init()

setBackground(Color.yellow); add(l1);

add(t1); add(l2);

add(t2); add(b);

b.addActionListener(this);

public void paint(Graphics g)

g.drawString("Sum= "+z, 50, 70);


}

public void actionPerformed(ActionEvent ae)


{

x= Integer.parseInt(t1.getText()); y=

Integer.parseInt(t2.getText()); z= x+y; repaint();

➔ HTML file :-
<html>

<title> my first applet</title>

<body>

<applet code= "summ.class" width= 500 height= 600>

</applet>

</body>

</html>
31

Output :-

28.Write a program using :A. List :-


import java.util.*; class listex {
public static void main(String []args)
{
List<String> l= new ArrayList<String> (); l.add("Mango");
l.add("Apple");
l.add("Guava");
l.add("Grapes");
l.add("Banana");
for(String fruit:l)
{
System.out.println(fruit);
}
}
}

Output :-
32

B. Hashset :-
import java.util.*;
class hashex
{
public static void main(String []args)
{
HashSet<String> h= new HashSet<String>(); h.add("Mango");
h.add("Apple");
h.add("Guava");
h.add("Grapes");
h.add("Banana"); Iterator<String> i= h.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
Output :-
33

29.Write a program in which read a string and reverse its character

import java.util.*; class stringrev


{ public static void main(String []args)
{
Scanner sc= new Scanner(System.in);
System.out.println("input string ");
String s= sc.nextLine(); String rev= "";
for(int i= s.length()-1; i>=0; i--)
{
rev= rev+s.charAt(i);
}

System.out.println("original string : "+s);


System.out.println("Reverse of string : "+rev);
}
}

# output

You might also like