Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ritikrajproject

Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

AN

ASSIGNMENT
ON
PROGRAMING IN JAVA
BACHELOR OF COMPUTER APPLICATIONS
FROM
Pt. Ravishankar Shukla University Raipur (C.G)
final Year
Year: 2022-2023

Guided by Submitted by
Mr. Mannu Rawani RITIK RAJ
HOD(comp.sc)
CLASS – BCAIII

Submitted to
Pragati College Raipur (C.G)
Pt. Ravishankar Shukla University Raipur (C.G)
ACKNOWLEDEMENT

No work can be completed successfully without the help, inspiration and


encouragement by the elders and seniors. So my heartily thanks to all of
them who helped me while preparing this assignment. I would begin by
thanking to my parents who provided me the required support for
completing this job.

I Acknowledge and thanks for the help received from

Mr. Mannu Rawani our “Assignment In charge” and classmates who


guided me to complete this assignment.
CERTIFICATE OF APPROVAL

This is to certify that the Assignment work on “PROGRAMMING IN Java”


is carried out by Ritik Raj a student of BCA-III year at PRAGATI COLLEGE
is hereby approved as a credible work in the discipline of programming
in Java for the award of degree of BCA– III year during the year 2022-23
from Pt. Ravishankar Shukla University, Raipur (CG).

Mr. Mannu Rawani

HOD-Computer Science

Pragati College, Raipur (C.G.)


CERTIFICATE OF EVALUATION

This is to certify that the Project work entitled is “PROGRAMING IN


Java” carried out by Ritik Raj a student of BCA-III Year at Pragati
College, Raipur after proper evaluation and examination, is here by
approved as a credible work in the discipline of programming in
Java and is done in a satisfactory manner for its acceptance as a
requisite for the award of degree of “BCA-III Year "during the year
2022-2023 from Pt. Ravishankar Shukla University, Raipur (CG).

Internal Examiner External Examiner


Index
No. Questions Page Remarks
No.
1 Write a program that implements the concept 8
of Encapsulation.
2 Write a program to check the given number is 9
Armstrong number or not.
3 Write a program to sort the elements of One- 10-11
Dimensional Array in ascending order.
4 Write a Java method to find the smallest 12
number among three numbers.
5 Write a Java method to count all words in a 13
string.
6 Write a Java method to count all vowels in a 14
string.

7 Write a Java method to compute the sum of 15


the digits in an integer.
8 Write a Java method to check whether an year 16
entered by the user is a leap year or not.

9 Write a Java method to check numbers is 17


palindrome number or not.
10 Write a Java method to displays prime 18
numbers between 1 to 20.
11 Write a program to calculate simple interest 19
using the Wrapper class.

12 Write a program to illustrate the use of 20


Method Overloading .

13 Write a program to illustrate the use of 21


Method Overriding .
14 Write a program program to demonstrate the 22
concept of Constructor Overloading.

Page|. 5 Ritik Raj


15 Write a program to illustrate Multi-Level 24
Inheritance.
16 Write a program to illustrate Hierarchical 25
Inheritance.
17 Write a program to implement Interface with 26
suitable example.
18 Write a program to implement Multiple 27
Interface in a single class.
19 Write a program to illustrate Single Level 23
Inheritance.
20 Write a program to extend one Interface to 28
another Interface .
21 Write a program to illustrate thread in java. 29

22 Write a program to illustrate Multithreading. 30

23 Write a program to suspend a thead for some 31-32


specific time duration.
24 Write a program that use Boolean data type 33
and print the Prime number series up to 50.
25 Write a program to calculate area of various 34
geometrical figures using Abstract class.
26 Write a program where single class 35-36
implements more than one Interfaces and with
help of Interface reference variable user call
the method.
27 Write a program and use the this and super 37
keyword.
28 Write a program to read data from using java 38
i/o package (using character class).
29 Write a program to write data into using java 39
i/o package(using character class).
30 Write a program to read data from using java 40
i/o package (using byte class).
31 Write a program to write data into using java 41
i/o package(using byte class).
32 Write a program to illustrate Hybrid 42
Inheritance.
33 Write a program to read and write data in a 43
single program using java i/o package.

Page|. 6 Ritik Raj


34 Write a program to create a packege and use it 44
in your program.
35 Write a program to JDBC Connectivity. 45

Page|. 7 Ritik Raj


1.Write a program that implements the concept of Encapsulation.

Code-
class Encap
{
private int age;
public int getAge()
{
return age;
}
public void setAge( int newAge)
{
age = newAge;
}
}
public class RunEncap
{
public static void main(String args[])
{
Encap encap = new Encap();
encap.setAge(20);
System.out.print(" Age : " + encap.getAge());
}
}

Page|. 8 Ritik Raj


2.Write a program to check the given number is Armstrong number or not.

Code-
class Armstrong
{
public static void main(String[] args)
{
int number = 1634, originalNumber, remainder, result = 0, n = 0;
originalNumber = number;
for (;originalNumber != 0; originalNumber /= 10, ++n);
originalNumber = number;
for (;originalNumber != 0; originalNumber /= 10)
{
remainder = originalNumber % 10;
result += Math.pow(remainder, n);
}
if(result == number)
System.out.println(number + " is an Armstrong number.");
else
System.out.println(number + " is not an Armstrong number.");
}
}

Page|. 9 Ritik Raj


3. Write a program to sort the elements of One-Dimensional Array in
ascending order.

Code-
import java.util.Scanner;
public class ExArraySort
{
public static void main(String args[])
{
int n, i, j, temp;
int arr[] = new int[50];
Scanner scan = new Scanner(System.in);
System.out.print("Enter number for the array elements : ");
n = scan.nextInt();
System.out.println("Enter " + n + " Numbers : ");
for (i = 0; i < n; i++)
{
arr[i] = scan.nextInt();
}
System.out.print("Sorting array : \n");
for (i = 0; i < (n - 1); i++)
{
for (j = 0; j < (n - i - 1); j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.print("Sorted List in Ascending Order : \n");
for (i = 0; i < n; i++)
{
System.out.print(arr[i] + " ");
}
}

Page|. 10 Ritik Raj


Page|. 11 Ritik Raj
4.Write a Java method to find the smallest number among three numbers.

Code-
import java.util.Scanner;
class Smallnumber
{
public void small()
{
int a, b, c, smallest, temp;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number:");
a = sc.nextInt();
System.out.println("Enter the second number:");
b = sc.nextInt();
System.out.println("Enter the third number:");
c = sc.nextInt();
temp=a<b?a:b;
smallest=c<temp?c:temp;
System.out.println("The smallest number is: "+smallest);
}
public static void main(String[] args)
{
Smallnumber a1 = new Smallnumber();
a1.small();
}
}

Page|. 12 Ritik Raj


5.Write a Java method to count all words in a string.

Code-
class Count
{
static int wordcount(String a)
{
int count=0;
char ch[]= new char[a.length()];
for(int i=0;i<a.length();i++)
{
ch[i]= a.charAt(i);
if( ((i>0)&&(ch[i]!=' ')&&(ch[i-1]==' ')) || ((ch[0]!=' ')&&(i==0)) )
count++;
}
return count;
}
public static void main(String[] args)
{
String a ="BCA Final Year Student ";
System.out.println(wordcount(a) + " words.");
}
}

Page|. 13 Ritik Raj


6.Write a Java method to count all vowels in a string.
Code-
import java.util.Scanner;
public class CountVowels
{
public static void main(String args[])
{
int count = 0;
System.out.println("Enter a sentence :");
Scanner sc = new Scanner(System.in);
String sentence = sc.nextLine();
for (int i=0 ; i<sentence.length(); i++)
{
char ch = sentence.charAt(i);
if(ch == 'a'|| ch == 'e'|| ch == 'i' ||ch == 'o' ||ch == 'u'||ch == ' ')
{
count ++;
}
}
System.out.println("Number of vowels in the given sentence is "+count);
}
}

Page|. 14 Ritik Raj


7.Write a Java method to compute the sum of the digits in an integer.

Code-
import java.util.Scanner;
public class SumOfDigits
{
public static void main(String args[])
{
int number, digit, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
number = sc.nextInt();
while(number > 0)
{
digit = number % 10;
sum = sum + digit;
number = number / 10;
}
System.out.println("Sum of Digits: "+sum);
}
}

Page|. 15 Ritik Raj


8.Write a Java method to check whether an year entered by the user is a
leap year or not.

Code-
import java.util.Scanner;
public class LeapYear
{
public static void main(String[] args)
{
int year;
System.out.println("Enter an Year :: ");
Scanner sc = new Scanner(System.in);
year = sc.nextInt();
if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
System.out.println("Specified year is a leap year");
else
System.out.println("Specified year is not a leap year");
}
}

Page|. 16 Ritik Raj


9.Write a Java method to check numbers is palindrome number or not.
Code-
class PalindromeNumber
{
public static void main(String args[])
{
int r,sum=0,temp;
int n=151;
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}

Page|. 17 Ritik Raj


10.Write a Java method to displays prime numbers between 1 to 20.
Code-
class PrimeNumbers
{
public static void main(String[] args)
{
int num = 20, count;
for (int i = 1; i <= num; i++) {
count = 0;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
count++;
break;
}
}
if (count == 0) {
System.out.println(i);
}
}
}
}

Page|. 18 Ritik Raj


11.Write a program to calculate simple interest using the Wrapper class.

Code-
class Wrapper
{
public static void main(String args[])
{
float p=Float.parseFloat(args[0]);
System.out.println("Principle amount is: "+p);
float r= Float.parseFloat(args[1]);
System.out.println("rate is: "+r);
int t=Integer.parseInt(args[2]);
System.out.println("Enter Time: "+t);
float SI=(p*r*t)/100;
System.out.println("Simple Interest is: "+SI);
}
}

Page|. 19 Ritik Raj


12.Write a program to illustrate the use of Method Overloading .
Code-
class MethodOverloading
{
private static void display(int a)
{
System.out.println("Arguments: " + a);
}
private static void display(int a, int b)
{
System.out.println("Arguments: " + a + " and " + b);
}
public static void main(String[] args)
{
display(1);
display(1, 4);
}
}

Page|. 20 Ritik Raj


13.Write a program to illustrate the use of Method Overriding .

Code-
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();
}
}

Page|. 21 Ritik Raj


14.Write a program program to demonstrate the concept of Constructor
Overloading.

Code-
class Box
{
double width, height, depth;
int boxNo;
Box(double w, double h, double d, int num)
{
width = w;
height = h;
depth = d;
boxNo = num;
}
Box()
{
width = height = depth = 0;
}
Box(int num)
{
this();
boxNo = num;
}
public static void main(String[] args)
{
Box box1 = new Box(1);
System.out.println(box1.width);
}
}

Page|. 22 Ritik Raj


15.Write a program to illustrate Single Level Inheritance.

Code-
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class TestInheritance
{
public static void main(String args[])
{
Dog d=new Dog();
d.bark();
d.eat();
}
}

Page|. 23 Ritik Raj


16.Write a program to illustrate Multi-Level Inheritance.

Code-
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class BabyDog extends Dog
{
void weep()
{
System.out.println("weeping...");
}
}
class MultiInheritance
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}

Page|. 24 Ritik Raj


17.Write a program to illustrate Hierarchical Inheritance.
Code-
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class Cat extends Animal
{
void meow()
{
System.out.println("meowing...");
}
}
class TestHierarchical
{
public static void main(String args[])
{
Cat c=new Cat();
c.meow();
c.eat();
}
}

Page|. 25 Ritik Raj


18.Write a program to implement Interface with suitable example.
Code-
interface Language
{
void getName(String name);
}

class ProgrammingLanguage implements Language


{
public void getName(String name)
{
System.out.println("Programming Language: " + name);
}

public static void main(String[] args)


{
ProgrammingLanguage language = new ProgrammingLanguage();
language.getName("Java");
}
}

Page|. 26 Ritik Raj


19.Write a program to implement Multiple Interface in a single class.
Code-
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();
}
}

Page|. 27 Ritik Raj


20. Write a program to extend one Interface to another Interface .
Code-
interface A {
void funcA();
}
interface B extends A {
void funcB();
}
class C implements B
{
public void funcA()
{
System.out.println("This is funcA");
}
public void funcB()
{
System.out.println("This is funcB");
}
}
class ExtendInterface
{
public static void main(String args[]) {
C obj = new C();
obj.funcA();
obj.funcB();
}
}

Page|. 28 Ritik Raj


21.Write a program to illustrate thread in java.

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();
}
}

Page|. 29 Ritik Raj


22.Write a program to illustrate Multithreading.

Code-
class Multithreading extends Thread
{
public void run()
{
Try
{
System.out.println("Thread " + Thread.currentThread().getId()+ " is
running");
}
catch (Exception e)
{
System.out.println("Exception is caught");
}
}
public static void main(String[] args)
{
int n = 8;
for (int i = 0; i < n; i++)
{
Multithreading object = new Multithreading();
object.start();
}
}
}

Page|. 30 Ritik Raj


23. Write a program to suspend a thread for some specific time
duration.
Code-
public class JavaSuspend extends Thread
{
public void run()
{
for(int i=1; i<5; i++)
{
Try
{
sleep(500);
System.out.println(Thread.currentThread().getName());
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println(i);
}}
public static void main(String args[])
{
JavaSuspend t1=new JavaSuspend ();
JavaSuspend t2=new JavaSuspend ();
JavaSuspend t3=new JavaSuspend ();
t1.start();
t2.start();
t2.suspend();
t3.start();
}}

Page|. 31 Ritik Raj


Page|. 32 Ritik Raj
24.Write a program that use Boolean data type and print the Prime number
series up to 50.

Code-
class Prime
{
public static void main(String args[])
{
boolean b=true;
for(int i= 1; i<=50;i++)
{
b=true;
for(int j=2;j<=i/2;j++)
{
if(i%j==0)
b=false;
}
if(b)
System.out.println("Prime Number"+i);
}
}
}

Page|. 33 Ritik Raj


25. Write a program to calculate area of various geometrical figures
using Abstract class.
Code-
abstract class Circle
{
static final float pi=3.14f;
abstract float area(float x);
}
class Circle2 extends Circle
{
public float area(float x)
{
float A=pi*x*x;
return A;
}
}
class Valua
{
public static void main(String args[])
{
Circle2 a1 = new Circle2();
float z= a1.area(5f);
System.out.println("Area of circle="+z);
}
}

Page|. 34 Ritik Raj


26. Write a program where single class implements more than one
Interfaces and with help of Interface reference variable user call the
method.
Code-
interface Area
{
final static float pi=3.14f;
float compute(float x , float y);
}
interface Rectarea
{
int calculate(int l, int w);
}
class Test implements Area , Rectarea
{
public float compute(float x , float y)
{
return(pi*x*y);
}
public int calculate(int l, int w)
{
return(l*w);
}
}
class Interfacetest
{
public static void main(String args[])
{
Area o;
Rectarea u;
Test t=new Test();
o=t;
System.out.println("Area of circle="+o.compute(10,20));
u=t;
System.out.println("Area of Rectangle="+u.calculate(5,20));
}
}

Page|. 35 Ritik Raj


Page|. 36 Ritik Raj
27. Write a program and use the this and super keyword.
Code-
class Parent {
int a = 10;
static int b = 20;
int c = 1100;
static int d = 200;
void GFG()
{
this.a = 100;
System.out.println(a);
this.b = 600;
System.out.println(b);
}
}
class Base3 extends Parent {
void rr()
{
System.out.println(super.c);
System.out.println(super.d);
}
public static void main(String[] args)
{
new Base3().rr();
new Base3().GFG();
}
}

Page|. 37 Ritik Raj


28. Write a program to read data from using java i/o package (using
character class).
Code-
import java.io.*;
class Declare1
{
public static void main(String args[])throws
java.io.FileNotFoundException
{
try
{
FileReader a =new FileReader("C:\\Users\\ashwa\\Declare.txt");
try
{
int i;
while((i=a.read())!=-1)
{
System.out.print((char)i);
}
}
finally
{
a.close();
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}

Page|. 38 Ritik Raj


29. Write a program to write data into using java i/o package(using
character class).
Code-
import java.io.*;
class Writedata
{
public static void main(String args[])
{
try
{
FileWriter a = new FileWriter("C:\\Users\\ashwa\\Declare.txt");
BufferedWriter b= new BufferedWriter(a);
b.write("Write data");
b.close();
a.close();
System.out.println("Done");
}
catch(IOException e)
{
System.out.println(e);
}
}
}

Page|. 39 Ritik Raj


30. Write a program to read data from using java i/o package (using byte
class).
Code-
import java.io.*;
class Byteread
{
public static void main(String args[]){
try{
FileInputStream a= new
FileInputStream("C:\\Users\\ashwa\\Declare.txt");
BufferedInputStream b = new BufferedInputStream(a);
int i;
while((i=b.read())!=-1)
{
System.out.println(i);
}
b.close();
a.close();
}
catch(FileNotFoundException e)
{
System.out.println("File not found");
}
catch(IOException e1)
{
System.out.println("Error");
}
}

Page|. 40 Ritik Raj


31. Write a program to write data into using java i/o package(using byte class).

Code-
import java.io.*;
class Bytewrite
{
public static void main(String args[])
{
try
{
FileOutputStream a= new
FileOutputStream("C:\\Users\\ashwa\\Declare.txt");
BufferedOutputStream b = new BufferedOutputStream(a);
String st ="Java is a programming language";
byte by[]=st.getBytes();
b.write(by);
b.close();
a.close();
System.out.println("Done");
}
catch(IOException e1)
{
System.out.println("Error");
}
}
}

Page|. 41 Ritik Raj


32. Write a program to illustrate Hybrid Inheritance.
Code-
class HumanBody1
{
public void displayHuman()
{
System.out.println("Method defined inside HumanBody class");
}}
interface Male
{
public void show();
}
interface Female
{
public void show();
}
class Child extends HumanBody1 implements Male, Female
{
public void show()
{
System.out.println("Implementation of show() method defined in
interfaces Male and Female"); }
public void displayChild()
{
System.out.println("Method defined inside Child class");
}
public static void main(String args[]) {
Child obj = new Child();
System.out.println("Implementation of Hybrid Inheritance in Java");
obj.show();
obj.displayChild();
} }

Page|. 42 Ritik Raj


33. Write a program to read and write data in a single program using
java i/o package.
Code-
import java.io.*;
class ReadWritedata
{
public static void main(String args[])throws java.io.IOException
{
try
{
FileWriter a = new FileWriter("C:\\Users\\ashwa\\Declare.txt");
BufferedWriter b= new BufferedWriter(a);
b.write(" Read Write data");
b.close();
a.close();
System.out.println("Done");
FileReader c =new FileReader("C:\\Users\\ashwa\\Declare.txt");
BufferedReader d= new BufferedReader(c);

int i;
while((i=c.read())!=-1)
{
System.out.print((char)i);
}
c.close();
d.close();
}
catch(IOException e)
{
System.out.println(e);
}
}}

Page|. 43 Ritik Raj


34. Write a program to create a packege and use it in your program.
Code-
Package-
package pack12;
public class AB
{
void show()
{
System.out.println("Hello world");
}
}
Imports Package-
package pack12;
import pack12.AB;
class D extends AB
{
public static void main(String args[])
{
D r = new D();
r.show();
}
}

Page|. 44 Ritik Raj


Page|. 45 Ritik Raj

You might also like