0% found this document useful (0 votes)
9 views62 pages

Java Programming Assignment BCA III

The document is an assignment on Programming in Java submitted by Neelkamal Sahu for the Bachelor of Computer Application program at Pt. Ravishankar Shukla University, Raipur. It includes various programming tasks demonstrating concepts such as encapsulation, arrays, methods, and inheritance, along with code examples and outputs. The assignment is guided by Mr. Mannu Rawani and is part of the curriculum for the academic year 2024-25.

Uploaded by

nilkamal2266
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views62 pages

Java Programming Assignment BCA III

The document is an assignment on Programming in Java submitted by Neelkamal Sahu for the Bachelor of Computer Application program at Pt. Ravishankar Shukla University, Raipur. It includes various programming tasks demonstrating concepts such as encapsulation, arrays, methods, and inheritance, along with code examples and outputs. The assignment is guided by Mr. Mannu Rawani and is part of the curriculum for the academic year 2024-25.

Uploaded by

nilkamal2266
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

AN

ASSIGNMENT
ON

PROGRAMMING IN JAVA
BACHELOR OF COMPUTER APPLICATION

FROM
Pt. Ravishankar Shukla University Raipur (C.G)
First Semester
Year: 2024-25

Guided by Submitted by
Mr. Mannu Rawani Neelkamal sahu
(HOD Computer Science) (Class – BCA- III )

Submitted to
Pragati College Raipur (C.G)
Pt. Ravishankar Shukla University Raipur (C.G)
1|Page
CERTIFICATE OF APPROVAL

This is to certify that the assignment work on OPERATING SYSTEM is


carried out by Neelkamal sahu a student of BCA-III at PRAGATI
COLLEGE is hereby approved as a credible work in the discipline of
BCA– I IISemester during the year 2024-25 from Pt. Ravishankar Shukla
University, Raipur (CG).

Mr. Mannu Rawani


HOD Computer Science
Pragati College, Raipur (C.G.)

2|Page
Page |3

CERTIFICATE OF EVALUATION

This is to certify that the Assignment work entitled is OPERATING


SYSTEM carried out by Neelkamal sahu a student of BCA– III at
Pragati College, after proper evaluation and examination, is here
by approved as a credible work in the discipline of Practical
Assignment and is done in a satisfactory manner for its acceptance
as a requisite for the award of degree of “BCA– III "during the year
2024-25 from Pt. Ravishankar Shukla University, Raipur (CG).

Internal Examiner External Examiner

Neelkamal sahu
Page |4

DECLARATION

This is to certify that the assignment work entitled PROGRAMMING IN

JAVA which is submitted by me in the fulfilment for the award of the BCA–

III, Pragati College, comprises the original work carried out by me.

I further declare that the work reported in this process in this assignment has

not been submitted and will not be submitted, either in part of in full for the

award of any other degree or diploma in this institute or any other institute

or University.

Place - Raipur Student name : Neelkamal

Date : Roll No. :

Neelkamal sahu
Page |5

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 “Asignment Incharge” and classmates who
guided me to complete this assignment.

Neelkamal sahu
Page |6

Neelkamal sahu
CLASS- BCA-III

INDEX

PAGE REMAR
NO. K
[Link]. PROGRAMS
Write a program that implements the concept of Encapsulation.
1
Write a program that use Boolean data type and print the Prime number
2 series up to 50.
Write a program to check the given number is Armstrong number or not.
3
Write a program to sort the elements of One-Dimensional Array in
4 ascending order.
5 Write a Java method to find the smallest number among three numbers
6 Write a Java method to count all words in a string
7 Write a Java method to count all vowels in a string
8 Write a Java method to compute the sum of the digits in an integer
Write a Java method to check whether an year entered by the user is a
9 leap year or not
10 Write a Java method to check numbers is palindrome number or not
11 Write a Java method to displays prime numbers between 1 to 20
Write a program to calculate simple interest using the Wrapper class.
12
Write a program to calculate area of various geometrical figures using
13 Abstract class.
Write a program where single class implements more than one Interfaces
14 and with help of Interface reference variable user call the method.
Write a program to illustrate the use of Method Overloading .
15
Write a program to illustrate the use of Method Overriding .
16
Write a program program to demonstrate the concept of Constructor
17 Overloading.
Write a program to illustrate Single Level Inheritance.
18
Write a program to illustrate Multi-Level Inheritance.
19
Write a program to illustrate Hybrid Inheritance.
20
Write a program to illustrate Hierarchical Inheritance.
21
Write a program to implement Interface with suitable example.
22
Write a program to implement Multiple Interface in a single class.
23
Write a program to extend one Interface to another Interface .
24
Write a program to illustrate thread in java.
25
26 Write a program to illustrate Multithreading.
27 Write a program to suspend a thead for some specific time duration.
28 Write a program and use the this and super keyword.
29 Write a program to create a packege and use it in your program.
Neelkamal sahu
Page |7

Write a program to read data from using java i/o package (using
30 character class).
Write a program to write data into using java i/o package(using character
31 class).
Write a program to read data from using java i/o package (using byte
32 class).
Write a program to write data into using java i/o package(using byte
33 class).
Write a program to read and write data in a single program using java i/o
34 package.

Neelkamal sahu
Page |8

Q.1 Write a program that implements the concept of


Encapsulation.
CODE –
class Sub {
void sum() {
int a, b, c;
a = 10;
b = 20;
c = a + b;
[Link]("\n*** Concept of Encapsulation ***\n");
[Link]("a = " + a + ", b = " + b);
[Link]("Sum of a and b : " + c);
}
}
public class Assignment1 {
public static void main(String[] args) {
Sub object = new Sub();
[Link]();
}
}

OUTPUT –

Neelkamal sahu
Page |9

Q.2 Write a program that use Boolean data type and print the
Prime number series up to 50.
CODE -
public class Assignment2 {
public static void main(String[] args) {
boolean b = true;
[Link]("*** Prime Number series upto 50 ***\n");
[Link]("Prime numbers - ");
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)
[Link]( i + " ,");
}
}
}
OUTPUT -

Neelkamal sahu
P a g e | 10

Q.3 Write a program to check the given number is Armstrong


number or not.
CODE –

import [Link];
import [Link];
public class Assignment3 {
static boolean isArmstrong(int n) {
int temp, digits = 0, last = 0, sum = 0;
temp = n;
while (temp > 0) {
temp = temp / 10;
digits++;
}
temp = n;
while (temp > 0) {
last = temp % 10;
sum += ([Link](last, digits));
temp = temp / 10;
}
if (n == sum)
return true;
else
return false;
}
public static void main(String args[]) {
int num;
Scanner sc = new Scanner([Link]);

Neelkamal sahu
P a g e | 11

[Link]("*** Check given number is Armstrong or not ***\n");


[Link]("Enter the number: ");

num = [Link]();
if (isArmstrong(num)) {
[Link]("Armstrong number");
} else {
[Link]("Not Armstrong number");
}
[Link]();
}
}

OUTPUT –

Neelkamal sahu
P a g e | 12

Q.4 Write a program to sort the elements of One-Dimensional


Array in ascending order.

CODE -
public class Assignment4 {
public static void main(String[] args) {
int arr[] = { 7, 8, 3, 1, 2 };
[Link]("Before the sorting : ");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}
[Link]("\nAfter sorting :");
for (int i = 0; i < [Link] - 1; i++) {
for (int j = 0; j < [Link] - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}
}
}

Neelkamal sahu
P a g e | 13

OUTPUT –

Neelkamal sahu
P a g e | 14

Q.5 Write a Java method to find the smallest number among three
numbers.

CODE –

public class Assignment5 {


public static void main(String[] args) {
int x = 110, y = 70, z = 169;
[Link]("*** find the greatest number betweenn three numbers
***\n");
[Link]("x = " + x + ", y = " + y + ", z = " + z);
if (x <= y) {
if (x <= z)
[Link]("The smallest number is: " + x);
else
[Link]("The smallest number is: " + z);
} else {
if (y <= z)
[Link]("The smallest number is: " + y);
else
[Link]("The smallest number is: " + z);
}
}
}
OUTPUT –

Neelkamal sahu
P a g e | 15

Q.6 Write a Java method to count all words in a string.

CODE –
import [Link];
public class Assignment6 {
static int wordcount(String string) {
int count = 0;
char ch[] = new char[[Link]()];
for (int i = 0; i < [Link](); i++) {
ch[i] = [Link](i);
if (((i > 0) && (ch[i] != ' ') && (ch[i - 1] == ' ')) || ((ch[0] != ' ') && (i == 0)))
count++;
}
return count;
}
public static void main(String[] args) {
[Link]("*** count all words in a string ***\n");
[Link]("Ente few words : -");
Scanner sc = new Scanner([Link]);
String string = [Link]();
[Link](wordcount(string) + " words.");
[Link]();
}
}
OUTPUT –

Neelkamal sahu
P a g e | 16

Q.7 Write a Java method to count all vowels in a string.

CODE –

public class Assignment7 {


public static void main(String[] args) {
int vCount = 0;
String str = "This is my area.";
str = [Link]();
for (int i = 0; i < [Link](); i++) {
if ([Link](i) == 'a' || [Link](i) == 'e' || [Link](i) == 'i' || [Link](i) ==
'o'
|| [Link](i) == 'u') {
vCount++;
}
}
[Link]("*** Write a Java method to count all vowels in a string
***\n");
[Link]("sentence: " + str);
[Link]("Number of vowels: " + vCount);
}
}
OUTPUT -

Neelkamal sahu
P a g e | 17

Q.8 Write a Java method to compute the sum of the digits in an


integer.

CODE -
import [Link];

public class Assignment8 {


public static void main(String[] args) {
int number,digit,sum=0;
Scanner sc = new Scanner([Link]);
[Link]("Enter the number : ");
number = [Link]();
while(number>0){
digit = number %10;
sum = sum+digit;
number = number/10;
}
[Link]("sum of digit : "+sum);
[Link]();
}
}
OUTPUT -

Neelkamal sahu
P a g e | 18

Q.9 Write a Java method to check whether an year entered by the


user is a leap year or not.

CODE -

import [Link];

public class Assignment9 {


public static void main(String[] args) {
int year;
[Link]("*** Entered year is a leap year or not ***\n");
[Link]("Enter an Year :: ");
Scanner sc = new Scanner([Link]);
year = [Link]();
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
[Link]("Specified year is a leap year");
else
[Link]("Specified year is not a leap year");
[Link]();
}
}

OUTPUT -

Neelkamal sahu
P a g e | 19

Q.10 Write a Java method to check numbers is palindrome


number or not.

CODE -

public class Assignment10 {


public static void main(String args[]) {
int r, sum = 0, temp;
int n = 454;
[Link]("*** check numbers is palindrome number or not ***\n");
[Link]("Number :: " + n);
temp = n;
while (n > 0) {
r = n % 10;
sum = (sum * 10) + r;
n = n / 10;
}
if (temp == sum)
[Link]("palindrome number ");
else
[Link]("not palindrome");
}
}
OUTPUT –

Neelkamal sahu
P a g e | 20

Q.11 Write a Java method to displays prime numbers between 1 to


20.

CODE -

public class Assignment11 {


public static void main(String[] args) {
int num = 20, count;
[Link]("*** prime numbers between 1 to 20 ***\n");
[Link]("Prime numbers are : -");
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)
[Link](i + " , ");
}
}
}
OUTPUT -

Neelkamal sahu
P a g e | 21

Q.12 Write a program to calculate simple interest using the


Wrapper class.

CODE –

import [Link];
public class Assignment12 {
public static void main(String args[]) {
float principalAmount, rate, time, simpleIntrest;
Float p, r, t, SI;
Scanner sc = new Scanner([Link]);
[Link]("*** Simple Intrest using Wrapper class ***\n");
[Link]("Enter the principal Amount : - ");
principalAmount = [Link]();
[Link]("Enter the Rate : - ");
rate = [Link]();
[Link]("Enter the Time (in years) : - ");
time = [Link]();
p = principalAmount;
r = rate;
t = time;
simpleIntrest = (p * r * t) / 100;
SI = simpleIntrest;
[Link]("Simple Interest is: " + SI);
[Link]();
}
}

Neelkamal sahu
P a g e | 22

OUTPUT -

Neelkamal sahu
P a g e | 23

Q.13 Write a program to calculate area of various geometrical


figures using Abstract class.

CODE –

abstract class cir {


static final float pi = 3.14f;
abstract float area(float x);
}
class circle extends cir {
public float area(float x) {
[Link]("x - "+x);
[Link]("pi - "+pi);
float A = pi * x * x;
return A;
}
}
class Assignment13 {
public static void main(String[] args) {
circle obj = new circle();
float z = [Link](5f);
[Link]("Area of circle = " + z);
}
}
OUTPUT -

Neelkamal sahu
P a g e | 24

Q.14 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 i,int w);
}
class Test implements Area, RectArea{
public float compute(float x , float y){
[Link]("\npi = " +pi+" x = " +x+" y = " + y);
return(pi*x*y);
}
public int calculate(int l, int w){
[Link]("\nlength = " +l+" width = " +w);
return(l*w);
}
}
public class Assignment14 {
public static void main(String[] args) {
Area A;
RectArea R;
Test T = new Test();
A = T;

Neelkamal sahu
P a g e | 25

[Link]("Area of circle : " + [Link](10,20));


R = T;
[Link]("Area of Rectangle is : " + [Link](5, 20));
}
}

OUTPUT –

Neelkamal sahu
P a g e | 26

Q.15 Write a program to illustrate the use of Method Overloading .

CODE –

public class Assignment15 {


static int add(int a, int b) {
[Link]("a = " + a + " b = " + b);
[Link]("Addition : - ");
return a + b;
}
static int add(int a, int b, int c) {
[Link]("\na = " + a + " b = " + b + " c = " + c);
[Link]("Multiplication : - ");
return a * b * c;
}
public static void main(String[] args) {
[Link]("*** Method Overloading ***\n");
[Link](add(11, 11));
[Link](add(11, 11, 11));
}
}

OUTPUT –

Neelkamal sahu
P a g e | 27

Q.16 Write a program to illustrate the use of Method Overriding .

CODE –

class Vehicle {
void run() {
[Link]("In childwood TMKOC was my fav TV serial. s");
}
}
class Bike extends Vehicle {
void run() {
[Link]("Now OTT platform web series is my fav .");
}
public static void main(String args[]) {
[Link]("*** Method Overriding Program ***\n");
Bike obj = new Bike();
[Link]();
}
}

CODE -

Neelkamal sahu
P a g e | 28

Q.17 Write a program program to demonstrate the concept of


Constructor Overloading.

CODE –

public class Assignment17 {


int id;
String name;
int age;
Assignment17(int i,String n){
id = i;
name = n;
}
Assignment17(int i,String n , int a){
id = i;
name = n;
age = a;
}
void display(){
[Link]("ID = " +id + "\tName = " + name + " Age = " + age);
}
public static void main(String[] args) {
[Link]("*** Constructor Oveloading ***\n");
Assignment17 obj1 = new Assignment17(10, "Raj");
Assignment17 obj2 = new Assignment17(20, "Riya",9);
[Link]("Before Constructor Oveloading");
[Link]();
[Link]("\nAfter Constructor Oveloading");
[Link]();

Neelkamal sahu
P a g e | 29

}
}

OUTPUT -

Neelkamal sahu
P a g e | 30

Q.18 Write a program to illustrate Single Level Inheritance.

CODE -

class Dad{
String rs = "5 lacks";
void superMethod(){
[Link](" (parent class) Dad's properties " + rs);}
}
class Me extends Dad{
String myRs = "2 lacks";
void childMethod(){
[Link]("My properties " + myRs);
[Link]("(child class) I can access my Dad's all properties.");}
}
class Assignment18{
public static void main(String args[]){
[Link]("*** Single Inhertance Example ***\n");
Me obj=new Me();
[Link]();
[Link]();
}}
OUTPUT -

Neelkamal sahu
P a g e | 31

Q.19 Write a program to illustrate Multi-Level Inheritance.

CODE –

class Vechicle {
void VechicleTypes() {
[Link]("There are many types of Vechicle : - \n Cars , bikes , Scooty ,
Trucks , buses");
}
}
class Car extends Vechicle {
String price = "15 lacks to 40 lacks";
void CarsTypes() {
[Link]("");
[Link](" There are many famous cars compaines : ");
[Link]("Maruti , Tata , XUV , Range Rover , Invova , Toyota.");
}
}
class Invova extends Car {
String myPrice = "15 lacks to 25lacks";
void InvovaCars() {
[Link]("");
[Link]("My fav car is Invova and I can Buy it.");
[Link]("Invova Cars price " + price);
[Link]("My Bugut " + myPrice);
}
}

public class Assignment19 {

Neelkamal sahu
P a g e | 32

public static void main(String args[]) {


[Link]("Multilevel Inhertance Example - ");
Invova obj = new Invova();
[Link]();
[Link]();
[Link]();
}
}

OUTPUT –

Neelkamal sahu
P a g e | 33

Q.20 Write a program to illustrate Hybrid Inheritance.

CODE -

class ParentA {
public void displayA() {
[Link]("This is ParentA class");
}
}
class ParentB {
public void displayB() {
[Link]("This is ParentB class");
}
}
class Child extends ParentA {
public void displayA() {
[Link]("This is Child class A part");
}
}
class GrandChild extends Child {
ParentB b = new ParentB();
public void displayB() {
[Link]();
[Link]("This is GrandChild class B part");
}
}
public class Assignment20 {
public static void main(String[] args) {
[Link]("\n *** Hybrid Inheritance *** \n");
Neelkamal sahu
P a g e | 34

ParentA a = new ParentA();


[Link]();
Child c = new Child();
[Link]();
GrandChild gc = new GrandChild();
[Link]();
[Link]();
}
}

OUTPUT -

Neelkamal sahu
P a g e | 35

Q.21 Write a program to illustrate Hierarchical Inheritance.

CODE –

class Base {
public void display() {
[Link]("I am Parent class");
}
}
class Derived1 extends Base {
public void display2() {
[Link]("This is Child1 class");
}
}
class Derived2 extends Base {
public void display3() {
[Link]("This is Child2 class");
}
}
public class Assignment21 {
public static void main(String[] args) {
[Link]("\n *** Hierarchical Inheritance *** \n");
Derived1 d1 = new Derived1();
[Link]();
d1.display2();
[Link]("\n");
Derived2 d2 = new Derived2();
[Link]();
d2.display3();

Neelkamal sahu
P a g e | 36

}
}

OUTPUT -

Neelkamal sahu
P a g e | 37

Q.22 Write a program to implement Interface with suitable


example.

CODE –

interface Bank {
public void user();
public void password();
}
public class Assignment22 implements Bank {
public void user() {
[Link]("your name - : Gopal Das");
}
public void password() {
[Link]("your password - : Das@123 .... ");
}
public static void main(String[] args) {
[Link]("\n*** Interface Program ***\n");
[Link]("Show my Bank Details : - \n");
Assignment22 obj = new Assignment22();
[Link]();
[Link]();
}
}

Neelkamal sahu
P a g e | 38

OUTPUT -

Neelkamal sahu
P a g e | 39

Q.23 Write a program to implement Multiple Interface in a single


class.

CODE –

interface Printable {
void print();
}
interface Showable {
void Show();
}
public class Assignment23 implements Printable, Showable {
public void print() {
[Link]("called Method of Interface 1");
}
public void Show() {
[Link]("called Method of Interface 2");
}
public static void main(String[] args) {
Assignment23 obj = new Assignment23();
[Link]("\n*** Multiple Interface Example ***\n");
// [Link]("multiple interface example");
[Link]();
[Link]();
[Link]();
}
}

Neelkamal sahu
P a g e | 40

OUTPUT –

Neelkamal sahu
P a g e | 41

Q.24 Write a program to extend one Interface to another Interface.

CODE –

interface Shape {
double getArea();
}
interface Colorable extends Shape {
void fillColor();
}
class Square implements Colorable {
double side;
String color;
Square(double s, String c) {
side = s;
color = c;
}
public double getArea() {
return side * side;
}
public void fillColor() {
[Link]("Filling color " + color + " in square");
}
}

public class Assignment24 {


public static void main(String[] args) {
[Link]("\n *** Extend Interface to another Interface *** \n");
Square s = new Square(6, "Sky Blue");

Neelkamal sahu
P a g e | 42

[Link]("Area of Square: " + [Link]());


[Link]();
}
}

OUTPUT -

Neelkamal sahu
P a g e | 43

Q.25 Write a program to illustrate thread in java.

CODE –

class file3 extends Thread {


public void run() {
[Link]("thread class function...");
[Link]("thread name : "+[Link]().getName());
[Link]("thread class name : "+[Link]().getClass());
}
}
public class Assignment25 {
public static void main(String[] args) throws InterruptedException {
[Link]("\n **** Thread class Program ***");
file3 obj = new file3();
[Link]();
[Link]("\nmain class function...\n");
}
}

OUTPUT -

Neelkamal sahu
P a g e | 44

Q.26 Write a program to illustrate Multithreading.

CODE –

class MultiThread1 extends Thread {


public void run() {
[Link]("Thread 1 class function is called");
for (int i = 0; i < 3; i++) {
[Link]("thread 1 function : " + i);
}
}
}
class MultiThread2 extends Thread {
public void run() {
[Link]("Thread 2 class function is called");
for (int i = 0; i < 3; i++) {
[Link]("thread 2 function : " + i);
}
}
}
public class Assignment26 {
public static void main(String[] args) {
[Link]("\n*** Multithreading program ***\n");
MultiThread1 obj = new MultiThread1();
[Link]();
MultiThread2 obj2 = new MultiThread2();
[Link]();
[Link]("main thread ");
for (int i = 0; i < 3; i++) {

Neelkamal sahu
P a g e | 45

[Link](" main thread : "+i);


}
}
}

OUTPUT -

Neelkamal sahu
P a g e | 46

Q.27 Write a program to suspend a thead for some specific time


duration.

CODE –

class MyThread extends Thread {


public void run() {
for (int i = 1; i <= 5; i++) {
[Link]("Thread: " + i);
try {
[Link](1000);
} catch (InterruptedException e) {
[Link]();
}
}
}
}

public class Assignment27 {


public static void main(String[] args) {
[Link]("\n *** Thread suspend program ***\n");
MyThread t = new MyThread();
[Link]();
try {
[Link](5000);
[Link]();
[Link]("Thread suspended for 5 seconds");
Neelkamal sahu
P a g e | 47

[Link](5000);
[Link]();
[Link]("Thread resumed");
} catch (InterruptedException e) {
[Link]();
}
}
}

OUTPUT –

Neelkamal sahu
P a g e | 48

Q.28 Write a program and use the this and super keyword.

CODE –

class Parent {
int value;
Parent(int v) {
value = v;
}
}
class Child extends Parent {
int value;
Child(int v1, int v2) {
super(v1);
value = v2;
}
public void display() {
[Link](" Parent Age: " + [Link]);
[Link]("Child Age: " + [Link]);
}
}
public class practical28 {
public static void main(String[] args) {
[Link]("\n *** super and this keyword *** \n");
Child c = new Child(50, 30);
[Link]();
}
}

Neelkamal sahu
P a g e | 49

OUTPUT -

Neelkamal sahu
P a g e | 50

Q.29 Write a program to create a packege and use it in your


program.

CODE :-

Creating Package
//Package Name
package practical29p;
public class Demo {
public void show() {
[Link]("Hi Everyone");
}
public void view() {
[Link]("Hello");
}
}
Import Package

import [Link];
public class Practical29b {
public static void main(String arg[]) {
Demo d = new Demo();

[Link]();
[Link]();
}
}

Neelkamal sahu
P a g e | 51

Output :-

Neelkamal sahu
P a g e | 52

Q.30 Write a program to read data from using java i/o package
(using character class).

CODE –

import [Link].*;

public class ReadFile {


public static void main(String[] args) {
[Link]("\n *** Read data using character class *** \n");
[Link]("Data - :");
try{
FileReader obj = new FileReader("[Link]");
try{
int i;
while( (i=[Link]())!=-1){
[Link]((char)i);
}
}
finally{
[Link]();
}
}
catch(IOException e){
[Link]("Exception handled");

}
}

Neelkamal sahu
P a g e | 53

OUTPUT –

Neelkamal sahu
P a g e | 54

Q.31 Write a program to write data into using java i/o


package(using character class).

CODE –

import [Link];
import [Link];

public class MyFile {


public static void main(String[] args) throws IOException {
[Link]("\n *** Write data in file using character class *** \n");
FileWriter obj = new FileWriter("[Link]");

[Link]("Assignment no . 31");
[Link](" by - pragati college");
[Link]("file created successfully ...");
[Link]();
}
}

Neelkamal sahu
P a g e | 55

OUTPUT -

Neelkamal sahu
P a g e | 56

Q.32 Write a program to read data from using java i/o package
(using byte class).

CODE –

import [Link];
public class Assignment32 {
public static void main(String args[]){
[Link]("\n *** Read data using Byte Class *** \n");
[Link]("data : - ");
try{
FileInputStream fin=new FileInputStream("[Link]");
int i=0;
while((i=[Link]())!=-1){
[Link]((char)i);
}
[Link]();
}catch(Exception e){[Link](e);}
}
}

OUTPUT –

Neelkamal sahu
P a g e | 57

Neelkamal sahu
P a g e | 58

Q.33 Write a program to write data into using java i/o


package(using byte class).

CODE –

import [Link].*;

class Assignment33 {
public static void main(String[] args)
throws IOException
{
int i;
FileOutputStream obj = new FileOutputStream("[Link]",true);
String st = "XYZ is writing in me";
char ch[] = [Link]();
for (i = 0; i<[Link](); i++) {
[Link](ch[i]);
}
[Link]("\n *** Write in file using Byte class *** \n");
[Link]("written successfully ...");
[Link]();
}
}

Neelkamal sahu
P a g e | 59

OUTPUT –

Neelkamal sahu
P a g e | 60

Q.34 Write a program to read and write data in a single program


using java i/o package.

CODE –

import [Link].*;
import [Link].*;

public class Assignment34 {


public static void main( String args[])
{
try {
BufferedReader obj = new BufferedReader(new
InputStreamReader([Link]));
[Link]("Enter few lines : - ");
String str = [Link]();
FileWriter obj2=new FileWriter("[Link]");
[Link](str);
[Link]("Writing successful");
[Link]();
[Link]();
File file = new File("[Link]");
Scanner scn = new Scanner(file);
while ([Link]())
[Link]([Link]());
[Link]("Reading successful");

Neelkamal sahu
P a g e | 61

}
catch (Exception e){
[Link](e);
}
}
}

OUTPUT –

Neelkamal sahu
P a g e | 62

Neelkamal sahu

You might also like