My Java File (Rudra)
My Java File (Rudra)
Assignment 1
1. Write a program that implements the concept of
Encapsulation. Coding:-
package pro1;
import java.util.*;
public class Pro1
{
public static void main(String[] args)
{
float ar,r;
Scanner sc=new Scanner(System.in);
System.out.print("Enter radius: = ");
r=sc.nextFloat();
ar=(float) (3.14*r*r);
System.out.println("area of a circle is : " + ar);
}
}
Output:
Page | 1
Path:- C:\Users\SVPC\Documents\NetBeansProjects\pro2 Date:- 19 jul , 2022
Assignment 2
2. Write a program to demonstrate concept of function overloading of
Polymorphism. Coding:-
package pro2;
import java.util.*;
public class Pro2 {
public static void sum(int a,int b)
{
int c = a + b;
System.out.println("The Sum is: "+c);
}
public static void sum(int a,int b,int c)
{
int d = a + b + c;
System.out.println("The Sum is: "+d);
}
public static void main(String[] args) {
Pro2 f= new Pro2();
f.sum(6,7);
f.sum(5,7,12);
}
}
Output:-
Page |
Path:- C:\Users\SVPC\Documents\NetBeansProjects\pro4 Date:- 30 jul , 2022
Assignment 3
3. Write a program the use Boolean data type and print the Prime number
series up to 50.
Coding:-
package pro 4;
import java.util.*;
public class Pro4
{
public static void main(String[] args) {
int n= 50;
boolean status = true;
int num = 3;
if (n >= 1)
{ System.out.println("First "+n+" prime numbers are:");
System.out.println(2); }
for ( int i = 2 ; i <=n ; )
{
for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
{
if ( num%j == 0 )
{ status = false;
break;
} }
if ( status != false )
{
System.out.print(num +" ");
i++; }
status = true;
num++;
}}}
Output:-
Page | 3
Path:- C:\Users\SVPC\Documents\NetBeansProjects\pro5 Date:- 20 Sep , 2022
Assignment 4
4. Write a program to print first 10 number of the following series using Do-While loops
0,1,1,2,3,5,8,11…
Coding:-
package pro5;
import java.util.*;
public class Pro5 {
public static void main(String[] args) {
int a=0,b=1,sum=0,n;
int i=3;
System.out.print("Enter Number = ");
Scanner sc= new
Scanner(System.in); n=sc.nextInt();
System.out.print(a);
System.out.print(" ");
System.out.print(b);
do
{ sum=a+b;
a=b;
b=sum; i+
+;
System.out.print(" ");
System.out.print(sum);
}while(i<=n);
}
}
Output:-
Page | 4
Path:- C:\Users\SVPC\Documents\NetBeansProjects\pro6 Date:- 30 jul , 2022
Assignment 5
5. Write a program to check whether a given number is Armstrong or
not. CODING:-
package pro6;
import java.util.*;
public class Pro6 {
public static void main(String[] args)
{
int num,check,rem,sum=0;
System.out.println("Enter three digit number = ");
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
check = num;
while(check!=0)
{
rem = check%10;
sum = sum +
(rem*rem*rem); check =
check/10;
}
if(sum==num)
{
System.out.println(num + " Is Amstong Number");
}
else
{
System.out.println(num + " Is Not An Amstong Number");
}}
}
Output:-
Page | 5
Path:- C:\Users\SVPC\Documents\NetBeansProjects\pro7 Date:- 20 Sep , 2022
Assignment 6
CODING:-
package pro7;
import java.util.*;
public class Pro7 {
public static void main(String[] args) {
int fact=1,i,n;
Scanner sc= new
Scanner(System.in);
System.out.print("Enter number = ");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println("Factorial of "+n+" is: "+ fact);
}
}
Output:-
Page | 6
Path:- C:\Users\SVPC\Documents\NetBeansProjects\pro8 Date:- 01 Aug , 2022
Assignment 7
7. WAP to sort the elements of One Dimensional Array in Ascending
Order. CODING:-
package pro8;
public class Pro8 {
public static void main(String[] args) {
int a[] ={5,2,4,3,1,9,8};
int temp=0;
System.out.print("Entered array elements are: = ");
for(int i=0;i<a.length;i++)
System.out.print(a[i] +
""); for (int i=0;i<a.length;i++){
for(int j=i+1;j<a.length;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}}
System.out.println();
System.out.print ("sorted elements are : ");
for(int i=0;i<a.length;i++)
System.out.print(a[i] + "");
}
}
Output:-
Page | 7
Path:- C:\Users\SVPC\Documents\NetBeansProjects\pro9 Date:- 03 Aug , 2022
Assignment 8
Page | 8
}
for(i=0;i<1;i++)
{
for(j=0;j<1;j++)
{
System.out.println(c[i][j]+ " ");
}
System.out.println(" ");
}
}
}
}
Output:-
Page | 9
Path:- C:\Users\SVPC\Documents\NetBeansProjects\pro10 Date:- 03 Aug , 2022
Assignment 9
CODING:-
package pro10;
import java.util.*;
public class Pro10 {
public static void main(String[] args) {
int i,j;
int a[][]=new int [2][2];
int b[][]=new int [2][2];
int c[][]=new int [2][2];
System.out.println( "Enter elements for 1st array: = ");
Scanner sc = new Scanner (System.in);
for (i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
a[i][j]= sc.nextInt();
}
System.out.print("");
}
System.out.println( "Enter elements for 2nd array: = ");
for (i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
b[i][j]= sc.nextInt();
}
System.out.print("");
}
System.out.println( "Sum of elements of 1st array & 2nd array: = ");
Page |
for (i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]= a[i][j]+b[i][j];
System.out.println(c[i][j] +" ");
}
System.out.print("");
}
}
}
Output:-
Page |
Path:- C:\Users\SVPC\Documents\NetBeansProjects\pro11 Date:- 03 Aug , 2022
Assignment 10
10. WAP for matrix transpose using input/output stream
class. CODING:-
package pro11;
import java.util.*;
public class Pro11 {
public static void main(String[] args) {
int a[][]=new int[2][2];
int i,j;
Scanner sc= new Scanner(System.in);
System.out.print("Enter array elements: = ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
a[i][j]=sc.nextInt();
}}
System.out.println("Array elements are: = ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(a[i][j] +" ");
}
System.out.println();
}
System.out.println("Transposed array elements are: = ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(a[j][i] +" ");
}
System.out.println();
} } }
Output:-
Page |
Path:- C:\Users\SVPC\Documents\NetBeansProjects\pro12 Date:- 02 Sep , 2022
Assignment 11
11. WAP to add the elements of Vector as arguments of main method (Run Time)
and Rearrange them, and copy it into an Array.
CODING:-
package pro12;
import java.util.*;
public class Pro12
{
public static void main(String[] args)
{
Vector<Integer> vec = new Vector<Integer>();
vec.add(10);
vec.add(15);
vec.add(30);
vec.add(5);
System.out.println("Vector: " +
vec); Object[] arr = vec.toArray();
System.out.println("Array: " + arr);
for(int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
}
}
Output:-
Page |
Path:- C:\Users\SVPC\Documents\NetBeansProjects\pro13 Date:- 02 Aug , 2022
Assignment 12
12.WAP to check that the given String is palindrome or
not. CODING:-
package pro13;
public class Pro13 {
public static void main(String[] args) {
String s1="ANNA", revs1="";
int s1len=s1.length();
for(int i=(s1len-1);i>=0;--i)
{
revs1=revs1+s1.charAt(i);
}
if(s1.equals(revs1))
{
System.out.println("Palindrome");
}
else
{
System.out.println("Not palindrome");
}
}
}
Output:-
Page |
Path:- C:\Users\SVPC\Documents\NetBeansProjects\pro13 Date:- 08 Aug , 2022
Assignment 13
13. WAP to arrange the String is alphabetical
order. CODING:-
package prog13;
import java.util.*;
import java.util.Scanner;
public class Prog13 {
public static void main(String[] args) {
int n, a;
String temp;
Scanner sc = new Scanner (System.in);
System.out.println("Enter numbers of name you want to enter : ");
n = sc.nextInt();
String names[]=new String[n];
Scanner s1 = new Scanner (System.in);
System.out.println("Enter all the names : ");
for(int i=0;i<n;i++) {
names[i]=s1.nextLine();
}
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if (names [i].compareTo(names[i])>0)
{
temp = names[i];
names[i]=names[j];
names[j]=temp;
}}}
System.out.println("Names in Sorted Order : ");
for(int i=0;i<n;i++)
{
System.out.print(names[i]+" , ");
}
System.out.println(names[n-1]);
}}
Output:-
Page |
Path:- C:\Users\SVPC\Documents\NetBeansProjects\pro15 Date:- 26 Aug , 2022
Assignment 14
14. WAP for StringBuffer class which performs all methods of that class.
CODING:-
package pro15;
import java.util.*;
public class Pro15 {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
StringBuffer s1 = new StringBuffer("Object Oriented");
int a = s1.length();
System.out.println("Length of string=
"+a); s1.setCharAt(6,'-');
s1.append(" Programming");
System.out.println(s1);
s1.delete(7,14);
System.out.println(s1);
s1.insert(7,"Oriented");
System.out.println(s1);
}
}
Output:-
Page |
Path:- C:\Users\SVPC\Documents\NetBeansProjects\wrappperclass Date:- 16 Sep , 2022
Assignment 15
15. WAP to calculate Simple Interest using the Wrapper
Class. CODING:-
package wrapper.classs;
import java.io.DataInputStream;
import java.util.Scanner;
public class WrapperClasss {
public static void main(String[] args) {
int t;
float p,r,SI;
try
{
DataInputStream y= new DataInputStream(System.in);
p=Float.parseFloat(y.readLine());
System.out.println("Principle amount is: "+p);
r= Float.parseFloat(y.readLine());
System.out.println("rate is: "+r);
t=Integer.parseInt(y.readLine());
System.out.println("Enter Time: "+t);
SI=(p*r*t)/100;
System.out.println("Simple Interest is: "+SI);
}
catch(Exception e){}
}
}
Output:-
Page |
Path:- C:\Users\SVPC\Documents\NetBeansProjects\abstract Date:- 11 Nov , 2022
Assignment 16
16. WAP to calculate Area of geometrical figures using the abstract
class. CODING:-
package pkgabstract;
import java.util.*;
abstract class shape
{
int a,b;
abstract public void printarea();
}
class rectangle extends shape
{
public int area_rect;
public void printarea()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the length and breadth of rectangle: ");
a=s.nextInt();
b=s.nextInt();
area_rect=a*b;
System.out.println("Length of rectangle "+ a +" breadth of rectangle "+ b);
System.out.println("The area of rectangle is: "+area_rect);
}
}
class triangle extends shape
{
double area_tri;
public void printarea()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the base and height of triangle: ");
a=s.nextInt();
b=s.nextInt();
System.out.println("Base of triangle "+a +" height of triangle "+b);
area_tri=(0.5*a*b);
System.out.println("The area of triangle is: "+area_tri);
}
}
class circle extends shape
{
double area_circle;
Page |
public void printarea()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the radius of circle: ");
a=s.nextInt();
area_circle=(3.14*a*a);
System.out.println("Radius of circle; "+a);
System.out.println("The area of circle is: "+area_circle);
}
}
public class Abstract {
public static void main(String[] args) {
rectangle r=new rectangle();
r.printarea();
triangle t=new triangle();
t.printarea();
circle r1=new circle();
r1.printarea();
}
}
Output:-
Page |
Path:- C:\Users\SVPC\Documents\NetBeansProjects\pro18 Date:- 17 Nov , 2022
Assignment 17
17. WAP where Single class implements more than one interfaces .
CODING:-
package pro18;
import java.util.*;
interface A
{
double pi = 3.14;
void area();
}
interface B
{
int r = 5;
void circumr();
}
class C implements A,B
{
public void area()
{
double a1;
a1 = pi*r*r;
System.out.println("Area of circle = "+ a1);
}
public void circumr()
{
double a2;
a2 = 2*pi*r;
System.out.println("Area of circumr = "+ a2);
}}
public class Pro18 {
public static void main(String[] args) {
C c1 = new C();
c1.area();
c1.circumr();
}
}
Output:-
Page |
Path:- C:\Users\SVPC\Documents\NetBeansProjects\pro19 Date:- 29 Dec , 2022
Assignment 18
18). WAP that use the multiple catch statements within the try-catch mechanism.
CODING:-
package pro19;
public class Pro19 {
public static void main(String[] args) {
int a[]={5,10};
try
{
int x = a[2]/a[0]-a[1];
}
catch(ArithmeticException e)
{
System.out.println("Divided by zero error");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index error");
}
catch(ArrayStoreException e)
{
System.out.println("Invalid value is store");
}
int y = a[1]/a[0];
System.out.println("value of y = "+y);
}
}
Output:-
Page |
Path:- C:\Users\SVPC\Documents\NetBeansProjects\pro20 Date:- 13 Dec , 2022
Assignment 19
19. WAP where user will create a self-Exception using the “throw”
keyword. CODING:-
package pro20;
import java.util.*;
class A
{
public void check(int x)
{
if (x<18)
{
throw new ArithmeticException("Value should be grrater then 18");
}
else
{
System.out.println("You can Vote");
}
} }
public class Pro20 {
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your age");
n= sc.nextInt();
A a1 = new
A();
a1.check(n);
}
}
Output:-
Page |
Path:- C:\Users\SVPC\Documents\NetBeansProjects\pro21 Date:- 12 Jan , 2023
Assignment 20
20. WAP for multithread using the synchronized() methods of Thread class.
CODING:-
package pro21;
import java.lang.*;
class Table
{
synchronized void ptable(int n)
{
for(int i=1;i<5;i++)
{
System.out.println(n*i);
try
{
Thread.sleep(100);
}
catch(Exception e){}
}
}
}
class A extends Thread
{
Table t;
A(Table t)
{
this.t=t;
}
public void run()
{
t.ptable(3);
}
}
class B extends Thread
{
Table t;
Page |
B(Table t)
{
this.t=t;
}
public void run()
{
t.ptable(10);
}
}
public class Pro21 {
public static void main(String[] args)
{ Table t1 = new Table ();
A a1 = new
A(t1); B b1 =
new B(t1);
a1.start();
b1.start();
if(a1.isAlive()==true)
{
System.out.println("Thread is alive");
}
else
{
System.out.println("A Thread is dead");
}
}
}
Output:-
Page |
Path:- C:\Users\SVPC\Documents\NetBeansProjects\thread2 Date:- 09 Jan , 2023
Assignment 21
21. WAP for multithreading using the suspend(), resume(), sleep(t) method of
Thread class.
CODING:-
package thread2;
import java.lang.*;
class A extends Thread
{
public void run()
{
for(int i=1;i<5;i++)
{
if(i==2)
yield();
System.out.println("Thread A : i = "+ i);
}
System.out.println("Exit of Thread A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<5;j++)
{
System.out.println("Thread B : j = "+
j); if (j==2)
stop();
}
System.out.println("Exit from Thread B");
}
}
class c extends Thread
{
Page |
try
{
sleep(1000);
}
catch(Exception e){}
}
System.out.println("Exit Thread c");
}
}
public class Thread2
{
public static void main(String[] args)
{
A a1 = new A();
B b1 = new B();
c c1= new c();
a1.start();
b1.start();
a1.suspend();
c1.start();
a1.resume();
}
}
Output:-
Page |
Path:- C:\Users\SVPC\Documents\NetBeansProjects\p1 Date:- 28 Nov , 2022
Assignment 22
22. WAP to create a package using command and one package will import another
package.
CODING:-
Code for package1:
package P1;
public class ADD {
public void sum()
{
int a=10;
int b=20;
int c=a+b;
System.out.println("Addition="+c);
} }
Import package:
package P1;
public class SUB {
public void sub()
{
int a=15;
int b=2;
int c=a-b;
System.out.println("Subtraction="+c);
}}
Import package
package addition;
import P1.*;
public class Addition
{
public static void main(String[] args)
{
ADD a1=new ADD();
SUB s1=new SUB();
s1.sub();
a1.sum();
}}
Output:-
Page |
Path:- C:\Users\SVPC\Documents\NetBeansProjects\jdbc Date:- 23 Jan , 2023
Assignment 23
23. WAP to access the record from database by using JDBC connectivity.
CODING:-
package jdbc;
import java.sql.*;
public class Jdbc
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Page |
Connection c1 = DriverManager.getConnection("jdbc:odbc:test");
Page |
Statement s1 = c1.createStatement();
ResultSet rs = s1.executeQuery("select * from student");
while(rs.next())
{
System.out.println(rs.getInt(1)+" "+ rs.getString(2)+" " + rs.getString(3));
c1.close();
}
}
catch(Exception e){
System.out.print("error");
}
}
}
Output
Page |
Path:- C:\Users\SVPC\Documents\NetBeansProjects\switchpro25 Date:- 29 Jul , 2022
Assignment 24
24. WAP for demonstration of switch statement, continue and
break. CODING:-
package switchpro25;
public class Switchpro25 {
public static void main(String[] args) {
char choice ;
System.out.println ("Select your
choice"); System.out.println("M -->
Madras");
System.out.println("B --> Bombay");
System.out.println("C --> Calcutta");
System.out.println("CHOICE ---> ");
try {
switch(choice = (char) System.in.read())
{
case 'M':
case 'm': System.out.println("Madras : Booklet 5");
break;
case 'B':
case 'b': System.out.println("Bombay : Booklet 9");
break; case 'C':
case 'c': System.out.println("Calcutta : Booklet 15");
break;
}
func p = new func();
p.printnumber();
}
catch (Exception e)
{
System.out.println("I/O Error");
}
}
Page |
}
Page |
class func{
public void printnumber()
{
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
if( x == 30 ) {
continue;
}
System.out.print( x );
System.out.print("\n");
}
}
}
Output:-
Page |
Path:- C:\Users\SVPC\Documents\NetBeansProjects\filereader Date:- 19 Jan , 2023
Assignment 25
25. WAP a program to read and write on file using File Reader and file Writer
class. CODING:-
package filereader;
import java.io.*;
import java.util.*;
public class Filereader {
public static void main(String[] args)throws IOException {
String str="Good Morning India";
FileWriter fw= new FileWriter("output.txt");
for(int i=0;i<str.length();i++)
{
fw.write(str.charAt(i));
}
fw.close();
System.out.println("Data Written to file successfully");
int ch;
FileReader fr=new FileReader("output.txt");
while((ch=fr.read())!=-1)
{
System.out.println((char)ch);
}
fr.close();
}
}
Output:-
Page |