You are on page 1of 48

JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)

LAB MANUAL

Core Java
SUBJECT:__________________________________________________________________________________

IV
: SY BSc.IT
YEAR ______________________
SEMESTER __________________________
: ____________

2017-2018
ACADEMIC YEAR
: _________________________________________________________________________

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
Practical : 1

1.Title:
Java Basics
2. Prior Concepts:
1. Data types
2. Loops
3. New Concepts:
Class:
It is user defined data type which binds the data and its associated functions together.
Access Specifier:
It specifies the mode of access to members of class. Class members should be declared either private or public.
private:
Private members can be accessed only from within class through member function of that class which are declared as
public section. By default members are private. public:
Public members can be accessed in class through functions as well as it is accessible outside of class through objects.
Scanner Class:
Specifies the method for user input.
4. Objectives:
1. Understand the declaration of class.
2. Understand the declaration and implementation of Scanner class
5. Procedure:
1. Declare a class
2. Declare data members
4. Create a main function to test and implement the class.
6. Implementation :
Done by students in the lab.
7. Results:
O/P of the program
8. Application:
Concepts can be implemented in building software’s.
9. Questions:
a. Write a Java program that takes a number as input and prints its multiplication table up to 10.

Code:
import java.util.Scanner;
class Pract1a
{
public static void main(String args[])
{
int num,i;
Scanner st=new Scanner(System.in);
System.out.print("Enter Number:-"); num=st.nextInt();
NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14
JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
System.out.println("Table of "+num); for(i=1;i<=10;i++)
{
System.out.println(num+"*"+i+"="+num*i);
}
}
}

Output:

Enter Number:-7
Table of 7
7*1=7
7*2=14
7*3=21
7*4=28
7*5=35
7*6=42
7*7=49
7*8=56
7*9=63
7*10=70

b. Write a Java program to display the following pattern.

*****
****
***
**
*
Code:
import java.util.Scanner;
class Pract1b
{
public static void main(String args[])
{ int i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14
JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)

}
}
}

Output:

*****
****
***
**
*

b. Write a Java program to print the area and perimeter of a circle.


Code:
import java.util.Scanner;
class Pract1c
{
public static void main(String args[])
{
double r,cp,ca;
Scanner st=new Scanner(System.in);
System.out.print("Enter Radius of a circle Number:-");
r=st.nextDouble(); cp=2*3.14*r; ca=3.14*r*r;
System.out.println("Perimeter="+cp);
System.out.println("Area="+ca);
}
}

Output:

Enter Radius of a circle Number:-5


Perimeter=31.400000000000002 Area=78.5

Practical : 2
1.Title:

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
Use of Operators 2.
Prior Concepts:
1. Data types
2. Loops
3. The declaration and implementation of Scanner class

3. New Concepts:
Use of different Operators.
Use of different inbuilt functions of Scanner Class and Wrapper Classes.
Use of different inbuilt String functions.
4. Objectives:
1. Understand the way to use binary and decimal numbers
2. Understanding the use of different String functions.
5. Procedure:
1. Declare a class
2. Declare data members
4. Create a main function to test and implement the class.
6. Implementation :
Done by students in the lab.
7. Results:
O/P of the program
8. Application:
Concepts can be implemented in building software’s.
9. Questions:
a. Write a Java program to add to binary numbers Code:

import java.util.Scanner;
class Pract2a
{
public static void main(String args[])
{
long b1,b2; int i=0,rem=0;
int[] sum=new int[20]; Scanner in
=new Scanner(System.in); System.out.print("Enter
first binary number:-");
b1=in.nextLong();
System.out.print("Enter second binary number:-");
b2=in.nextLong(); while(b1!=0||b2!=0)
{
sum[i++]=(int)((b1%10+b2%10+rem)%2);
rem=(int)((b1%10+b2%10+rem)/2);

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
b1=b1/10;
b2=b2/10;
}
if(rem!=0)
{
sum[i++]=rem;
}
--i;
System.out.print("Sum of b1+b2= ");
while(i>=0)
{
System.out.print(sum[i--]);
}
}
}

Output:

Enter first binary number:-1101


Enter second binary number:-1001 Sum
of b1+b2= 10110

b. Write a Java program to convert a decimal number to binary and vice versa.
Code:
import java.util.Scanner; class
Pract2b
{
public static void main(String args[])
{ int
num2;
String num1;
Scanner in =new Scanner(System.in);
System.out.print("Enter binary number");
num1=in.nextLine();
System.out.print("Enter decimal number");
num2=in.nextInt();
System.out.println("Binary to Decimal:-"+Integer.parseInt(num1,2));
System.out.println("Decimal to Binary:-"+Integer.toBinaryString(num2));

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
}
}

Output:

Enter binary number1100


Enter decimal number25
Binary to Decimal:-12
Decimal to Binary:-11001

c. Write a Java program to reverse a string

Code:

import java.util.Scanner; class


Pract2c
{
public static void main(String args[])
{
String s,t="";
int len,i;
Scanner in =new Scanner(System.in);
System.out.print("Enter a string:-");
s=in.nextLine();
len=s.length(); for(i=len-
1;i>=0;i--)
t=t+s.charAt(i);
System.out.println("Reverse:-"+t);
}
}

Output:

Enter a string:-JAVA Reverse:-


AVAJ

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)

Practical : 3
1.Title:
Java Data Types 2.
Prior Concepts:
1. Data types
2. Loops and Conditional Statements.
3. The declaration and implementation of Scanner class

3. New Concepts:
Use of different types of Datatypes.
Use of different inbuilt functions of Scanner Class and Wrapper Classes.
Use of arrays in java.
Defining a function.

4. Objectives:
1. Understand the way to implement functions
2. Understanding the use of character class.
3. Understanding the use of arrays.
5. Procedure:
1. Declare a class
2. Declare data members
3. Declare and call the user defined functions.
4. Create a main function to test and implement the class.
6. Implementation :
Done by students in the lab.
7. Results:
O/P of the program
8. Application:
Concepts can be implemented in building software’s.
9. Questions:

a. Write a Java program to count the letters, spaces, numbers and other characters of an input
string.

Code:
import java.util.Scanner;

class Pract3a
{
public static void main(String args[])

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
{
String st;

Scanner in =new Scanner(System.in);


System.out.print("Enter a string:-");
st=in.nextLine();
count(st);

}
public static void count(String x)
{
char[] ch=x.toCharArray();
int i,l=0,s=0,d=0,a=0;
for(i=0;i<x.length();i++)
{
if(Character.isLetter(ch[i]))
{
l++;
}
else if(Character.isDigit(ch[i]))
{
d++;
}
else if(Character.isSpaceChar(ch[i]))
{
s++;
}
else
{
a++;
}
}
System.out.println("Letter:-"+l);
System.out.println("Space:-"+s);
System.out.println("Number:-"+d);
System.out.println("Other:-"+a);
} }

Output:
Enter a string:-Hello SYIT-2017-18
Letter:-9
Space:-1
Number:-6
NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14
JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
Other:-2

b. Implement a Java function that calculates the sum of digits for a given char array consisting
of the digits '0' to '9'. The function should return the digit sum as a long value Code:
import java.util.Scanner; class
Pract3b
{
public static long calc()
{ int i;
long x=0L,sum=0L;
char c[]={'0','1','2','3','4','5','6','7','8','9'};
for(i=c.length-1;i>=0;i--)
{
x=c[i]-'0';
sum=sum+x;
}
return sum;
}
public static void main(String args[])
{
long s=calc();
System.out.print("Sum is "+s);

Output:

Sum is 45

c. Find the smallest and largest element from the array

Code:

import java.util.Scanner;

class Pract3c
{
public static void main(String args[])
{

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
int num[]=new int[]{13,22,27,11,4};
int s=num[0];
int l=num[0];
int i;
System.out.println("Array Elements are:-");
for(i=0;i<num.length-1;i++)
{
System.out.println(num[i]);

}
for(i=1;i<num.length-1;i++)
{
if(num[i]>l)
l=num[i];
else if(num[i]<s)
s=num[i];
}
System.out.println("Largest Number in array:-"+l);
System.out.println("Smallest Number in array:-"+s);
}
}

Output:

Array Elements are:-


13
22
27
11
Largest Number in array:-27
Smallest Number in array:-11

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)

Practical : 4
1.Title:
Methods and Constructors
2. Prior Concepts:
1. Data types
2. Loops and Conditional Statements.
3. The declaration and implementation of Scanner class
4. Defining and using function.
3. New Concepts:
Use of Constructors and Destructor Implementation
of Abstract Classes
4. Objectives:
1. Understand the way to declare and use Constructor and destructor
2. Understanding the use of Abstract Class
5. Procedure:
1. Declare a class
2. Declare data members
3. Declare and call the user defined functions.
4. Create a main function to test and implement the class.
6. Implementation :
Done by students in the lab.
7. Results:
O/P of the program
8. Application:
Concepts can be implemented in building software’s.
9. Questions:
a. Design a class SortData that contains the method asec and desc Code:
public class SortData { int n,temp,i,j;
public void asec(int num[]) { for (int i =
0; i < num.length; i++) { for (int j =
i+1; j < num.length ; j++)
{ if(num[i]>num[j])
{ temp= num[i];
num[i]=num[j]; num[j]=temp;
}
}
}
System.out.println("Assending Order");
for (int i = 0; i < num.length ; i++) {
System.out.println(num[i]+" ");

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
}
}
public void desc(int num[]) { for (int
i = 0; i < num.length; i++) { for (int j
= i+1; j < num.length ; j++)
{ if(num[i]<num[j])
{
temp= num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
System.out.println("Descending Order");
for (int i = 0; i < num.length ; i++) {
System.out.println(num[i]+" ");
}
}

}
public class Prac4a {
public static void main(String[] args) {
SortData obj=new SortData(); int
arr[]={13,22,27,11,4}; obj.asec(arr);
obj.desc(arr);

}}

Output:

Assending Order
4
11
13
22
27
Descending Order
27
22
13
11
4

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)

b. Design a class that demonstrates the use of constructor and destructor

Code:

public class Prac4b {


public Prac4b()
{
System.out.println("Hello");
}
public void finalize()
{
System.out.println("Destroyed");
}
public static void main(String[] args) {
Prac4b obj=new Prac4b(); obj=null;
System.gc();
}

Output:

Hello
Destroyed

c. Write a java program to demonstrate the implementation of abstract class. Code:

public abstract class Calc


{ public abstract int sqr(int n1);
public abstract int cube(int n1);
public void show()
{
System.out.println("Hello");
}
}
public class Prac4c extends Calc
{
@Override public
int sqr(int n1) {
return n1*n1;
}

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
@Override public int
cube(int n1) {
return n1*n1*n1;
}
public static void main(String[] args) {
Prac4c obj=new Prac4c(); int sq=
obj.sqr(4);
System.out.println("4's Square= "+sq);
int cube= obj.cube(4);
System.out.println("4's Cube= "+cube);
}
}

Output:

4's Square= 16
4's Cube= 64

Practical : 5
1.Title:
Inheritance 2.
Prior Concepts:
1. Data types
2. Loops and Conditional Statements.
3. The declaration and implementation of Scanner class
4. Defining and using function.
5. Use of Constructors and Destructor
3. New Concepts: Types of Inheritance
Method Overriding
Interfaces
4. Objectives:
1. Understand the way to inherit a class.
2. Implementation of Method Overriding 3. Implementing multiple inheritance
using interface.
5. Procedure:
1. Declare a class
2. Declare data members
3. Create different classes and inherit using the extends keyword.
4. Create a main function to test and implement the class.

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
6. Implementation :
Done by students in the lab.
7. Results:
O/P of the program
8. Application:
Concepts can be implemented in building software’s.
9. Questions:

a. Write a java program to implement single level inheritance.


Code:
class Demo
{ float pi=3.142f;
void show()
{
System.out.println("Area of circle");
}
}
public class Prac5a extends Demo
{ float
r=2.0f; void
area()
{
System.out.println(pi*r*r);
}
public static void main(String[] args) {
Prac5a obj=new Prac5a(); obj.show();
obj.area();
}
}

Output:

Area of circle
12.568

b. Write a java program to implement method overriding.


Code:

public class A
{ void show()
{
NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14
JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
System.out.println("Base Class");
}
}
public class B extends A
{
void show()
{
System.out.println("Derived Class");
}
}
public class Prac5b {
public static void main(String[] args) {
B ob=new B(); ob.show();
}
}

Output:

Derived Class
c. Write a java program to implement multiple inheritance.
Code:

public interface S {
public void show();
}
public interface T extends S
{
public void display();
}
public class Prac5c implements T
{
@Override public
void display() {
System.out.println("From Interface T");
}
@Override
public void show() {
System.out.println("From Interface S");
}
public static void main(String[] args)
{ Prac5c ob=new Prac5c();
ob.show();
ob.display();
}

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
}

Output:

From Interface S
From Interface T

Practical : 6
1.Title:
Packages and Arrays 2.
Prior Concepts:
1. Data types
2. Loops and Conditional Statements.
3. The declaration and implementation of Scanner class
4. Defining and using function.
3. New Concepts:
Packages
Arrays
4. Objectives:
1. Understand the steps to create Packages.
2. Create and use of different types of arrays
5. Procedure:

1. Create a package
2. Declare a class
2. Declare data members and arrays
3. Create a main function to test and implement the class.
6. Implementation :
Done by students in the lab.
7. Results:
O/P of the program
8. Application:
Concepts can be implemented in building software’s.
9. Questions:
a. Create a package, add the necessary classes and import the package Code:

Step 1: First create the necessary folder named mypackage; under this folder create sub folder
named as mathematics.

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
Step 2: To create Mathmethods.java as follows and save this file in mathematics folder.
package mathematics; public class Mathematics { public static float sqr(float n)
{
return (n*n);
}
public static int sqr(int n)
{
return (n*n);
}
public static double sqr(double n)
{
return (n*n);
}
public static long sqr(long n)
{
return (n*n);
}
}

Step 3: Then we create a sqr class which is supposed to be saved in mypackage folder.

Step 4: After we have finished both the files, we then compile the “sqr.java” directly.

b. Write a program to add two matrices and print the resultant matrix. Code:

import java.util.Scanner;
public class MatAdd
{
public static void main(String args[])
{ int
i, j;
int mat1[][] = new int[2][2]; int
mat2[][] = new int[2][2]; int mat3[][]
= new int[2][2]; Scanner sc = new
Scanner(System.in);

System.out.print("Enter Matrix 1 Elements : ");


for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
mat1[i][j] = sc.nextInt();
NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14
JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
}
}

System.out.print("Enter Matrix 2 Elements : ");


for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
mat2[i][j] = scan.nextInt();
}
}

System.out.print("Adding both Matrix to form the Third Matrix...\n");


for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}

System.out.print("The Two Matrix Added Successfully..!!\n");

System.out.print("The New Matrix will be :\n");


for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
System.out.print(mat3[i][j]+ " ");
}
System.out.println();
}
}
}

Output:

Enter Matrix 1 Elements :


1
2
3
4
Enter Matrix 2 Elements :
1
2
3
4
NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14
JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
Adding both Matrix to form the Third Matrix...
The Two Matrix Added Successfully..!!
The New Matrix will be :
24
6 8

c. Write a java program for multiplying two matrices and print the product for the same. Code:

import java.util.Scanner;
public class MatrixMultiply
{
public static void main(String args[])
{
int n;
Scanner input = new Scanner(System.in);
System.out.println("Enter the base of squared matrices");
n = input.nextInt(); int[][] a = new int[n][n]; int[][] b =
new int[n][n]; int[][] c = new int[n][n];
System.out.println("Enter the elements of 1st martix row wise \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = input.nextInt();
}
}
System.out.println("Enter the elements of 2nd martix row wise \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = input.nextInt();
}
}
System.out.println("Multiplying the matrices...");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
System.out.println("The product is:");
for (int i = 0; i < n; i++)
{

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
for (int j = 0; j < n; j++)
{
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
}

Output:

Enter the base of squared matrices:


3
Enter the elements of 1st martix row wise:
1 2 3
4 5 6
7 8 9
Enter the elements of 2nd martix row wise:
2 3 4
5 6 7
8 9 1
Multiplying the matrices...
The product is: 36
42 21
81 96 57
126 150 93

Practical : 7
1.Title:
Vectors and Multithreading
2. Prior Concepts:
1. Data types
2. Loops and Conditional Statements.
3. The declaration and implementation of Scanner class
4. Defining and using function.
5. Inheritance
3. New Concepts:
Vectors
Thread lifecycle
Multi-threading
4. Objectives:
1. Understand the concept of Vectors.

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
2. Create and use threads.
3. Implementing multithreading.
5. Procedure:
1. Create a package
2. Declare a class
2. Declare data members and arrays
3. Create a main function to test and implement the class.
6. Implementation :
Done by students in the lab.
7. Results:
O/P of the program
8. Application:
Concepts can be implemented in building software’s.
9. Questions:

a. Write a java program to implement vector Code:

import java.util.Vector;
class Pract7a
{
public static void main(String args[])
{
Vector<String> v=new Vector<String>();
v.add("Red");
v.add("Green");
v.add("Blue");
System.out.println("Vector Elements are:-"+v);
v.add(2,"Yellow");
System.out.println("After Adding Element at second position:-"+v);
System.out.println("Element at third position:-"+v.get(3));
System.out.println("First Element:-"+v.firstElement());
System.out.println("Last Element:-"+v.lastElement());
System.out.println("Is this vector empty?"+v.isEmpty());
}
}

Output:

Vector Elements are:-[Red, Green, Blue]


After Adding Element at second position:-[Red, Green, Yellow, Blue]
Element at third position:-Blue
NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14
JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
First Element:-Red
Last Element:-Blue
Is this vector empty?false

b. Write a java program to implement thread life cycle.

Code:

import java.util.Vector; class


Pract7b
{
public static void main(String args[])
{
System.out.println(Thread.currentThread().getName());
for(int i=0;i<10;i++)
{
new Thread(""+i)
{
public void run()
{
System.out.println("Thread:"+getName()+"running");
}
}.start();
}
}
}

Output:

main
Thread:2running
Thread:5running
Thread:0running
Thread:9running
Thread:3running
Thread:1running
Thread:8running
Thread:7running
Thread:4running
Thread:6running

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)

c. Write a java program to implement multithreading.

Code:

import java.io.*; class


A extends Thread
{
public void run()
{
System.out.println("Start A");
for(int i=1;i<=5;i++)
{
System.out.println("Thread A i:"+i);
}
System.out.println("Exit A");

}
}

class B extends Thread


{
public void run()
{
System.out.println("Start B");
for(int j=1;j<=5;j++)
{
System.out.println("Thread B j:"+j);
}
System.out.println("Exit B");
}
}

class C extends Thread


{
public void run()
{
System.out.println("Start C");
for(int k=1;k<=5;k++)
{
System.out.println("Thread C k:"+k);
}
System.out.println("Exit C");
NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14
JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)

}
}

class D extends Thread


{
public void run()
{
System.out.println("Start D");
for(int m=1;m<=5;m++)
{
System.out.println("Thread D m:"+m);
}
System.out.println("Exit D");
}
}

class Pract7c
{
public static void main(String args[])
{
new A().start();
new B().start();
new C().start();
new D().start();

}
}

Output:

Start A
Start B
Thread A i:1
Thread B j:1
Start C
Thread C k:1
Thread A i:2
Thread C k:2
Start D
Thread B j:2
Thread D m:1
NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14
JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
Thread C k:3
Thread A i:3
Thread C k:4
Thread D m:2
Thread B j:3
Thread D m:3
Thread C k:5
Thread A i:4
Exit C
Thread D m:4
Thread B j:4
Thread D m:5
Thread A i:5
Exit D
Thread B j:5
Exit A Exit
B

Practical : 8
1.Title:
File Handling 2.
Prior Concepts:
1. Data types
2. Loops and Conditional Statements.
3. The declaration and implementation of Scanner class
4. Defining and using function.
3. New Concepts: File Manipulation
4. Objectives:
1. Understand the concept Byte Stream and Character Stream
2. Implementing ReaderClass and WriterClass
3. Implementing the use of Primitive Datatypes using DataInputStream and DataOutputStream
5. Procedure:
1. Create a package
2. Declare a class
2. Declare and initialize File class objects.
3. Create a main function to test and implement the class.
6. Implementation :
Done by students in the lab.
7. Results:
O/P of the program

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
8. Application:
Concepts can be implemented in building software’s.
9. Questions:
a. Write a Java program to open a file and display the contents in the console window.

Code:

import java.io.*; public class Prac8a { public static void main(String[] args) throws

FileNotFoundException, IOException {

InputStream is=new FileInputStream("D:\\Core Java\\Demo1.txt");

DataInputStream dis=new DataInputStream(is); int count=

is.available(); byte a[]=new byte[count];

is.read(a);

for (byte b : a) {

char k=(char)b;

System.out.print(k+"-");

Output:

H-e-l-l-o-

b. Write a java program to copy the contents from one file to another file.

Code:

import java.io.*;
public class prac8b { public static void main(String[] args) throws

FileNotFoundException, IOException {

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
FileInputStream fis=null;

FileOutputStream fos=null;

try {

File inF=new File("D:\\Core Java\\Demo1.txt");

File outF=new File("D:\\Core Java\\Demo2.txt");

fis=new FileInputStream(inF); fos=new

FileOutputStream(outF); byte buff[]=new

byte[1024]; int length;

while((length=fis.read(buff))>0)

fos.write(buff,0,length);

fis.close();

fos.close();

System.out.println("Contents Coppied...");

} catch (Exception e)

{ e.printStackTrace();

} } }

Output:

Contents Coppied...

c. Write a java program to read the student data from user and store it in the file.

Code:

import java.io.*;
import java.util.Scanner; public class prac8c { public static void main(String[] args)

throws FileNotFoundException, IOException { String s1,s2,s3;


NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14
JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
Scanner sc=new Scanner(System.in);

System.out.println("Enter name: ");

s1= sc.nextLine();

System.out.println("Enter phone number");

s2= sc.nextLine();

System.out.println("Enter address: ");

s3= sc.nextLine();

OutputStream fos=new FileOutputStream("D:\\Core Java\\Student.txt");

byte b1[]=s1.getBytes(); fos.write(b1); byte b2[]=s2.getBytes();

fos.write(b2); byte b3[]=s3.getBytes(); fos.write(b3);

fos.close();

System.out.println("File Created");

Output:

Enter name:

John

Enter phone number

89897899

Enter address:

California,USA

File Created

Practical : 9
1.Title:
GUI and Exception Handling 2.
Prior Concepts:
NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14
JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
1. Data types
2. Loops and Conditional Statements.
3. The declaration and implementation of Scanner class
4. Defining and using function.
5. Inheritance
6. Abstract Class and methods
3. New Concepts:
Graphical User Interface
Abstract Window Toolkit (AWT)
File Handling
4. Objectives:
1. Understand the concept of AWT and different inbuilt classes.
2. Understand the concept of Layout Managers 3. Understand the use of File Handling
5. Procedure:
1. Create a package
2. Declare a class
3. Inherit the Frame class or any other Parent class from java.awt package
4. Use different inbuilt classes and methods
3. Create a main function to test and implement the class.
6. Implementation :
Done by students in the lab.
7. Results:
O/P of the program
8. Application:
Concepts can be implemented in building software’s.
9. Questions:
a. Design a AWT program to print the factorial for an input value.

Code:

import java.awt.*; import


java.awt.event.ActionEvent; import
java.awt.event.ActionListener;
import
java.awt.event.WindowListener;
import
java.awt.event.WindowAdapter;
import
java.awt.event.WindowEvent;

public class Pract9a extends Frame implements ActionListener


{
NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14
JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
Label l1,l2;
TextField t1,t2;
Button b1,b2;
public Pract9a()
{ t1=new TextField(3);
t2=new TextField(10);
b1=new Button("Factorial");
b2=new Button("Clear");
l1=new Label("Enter Number");
l2=new Label("Result");
setLayout(new GridLayout(3,2));
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(b2);
setSize(200,200);
setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public static void main(String args[])
{ new
Pract9a();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
int num,fact,i;
String result;
fact=1;
num=Integer.parseInt(t1.getText());
for(i=1;i<=num;i++)

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
{
fact=fact*i;
}
result=String.valueOf(fact);
t2.setText(result);
}
else
{
t1.setText("");
t2.setText("");
}
}

Output:

b. Design an AWT program to perform various string operations like reverse string, string
concatenation etc..

Code:

import java.awt.*; import

java.awt.event.ActionEvent;

import

java.awt.event.ActionListener;

import

java.awt.event.WindowListener;

import
NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14
JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
java.awt.event.WindowAdapter;

import

java.awt.event.WindowEvent;

public class Pract9b extends

Frame implements ActionListener

Label l1,l2,l3;

TextField t1,t2,t3;

Button b1,b2,b3,b4;

public Pract9b()

l1=new Label("1st String:");

l2=new Label("2nd String:"); l3=new

Label("3rd String"); t1=new TextField(10);

t2=new TextField(10);

t3=new TextField(10); b1=new

Button("Reverse for 1st"); b2=new

Button("Concat 1st+2nd"); b3=new

Button("Length for 1st string"); b4=new

Button("Clear"); setLayout(new

GridLayout(5,2));

add(l1);

add(t1);

add(l2);

add(t2);

add(l3);

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
add(t3);

add(b1);

add(b2);

add(b3);

add(b4);

setSize(200,200);

setVisible(true);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

addWindowListener(new WindowAdapter()

public void windowClosing(WindowEvent we)

System.exit(0);

});

public static void main(String args[])

new Pract9b();

public void actionPerformed(ActionEvent e)

String result;

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
StringBuilder str;
if(e.getSource()==b1)

str=new StringBuilder();

str.append(t1.getText());

str.reverse();

t3.setText(str.toString());

else if(e.getSource()==b2)

result=(t1.getText()).concat(t2.getText());

t3.setText(result);

else if(e.getSource()==b3)

str=new StringBuilder();

str.append(t1.getText());

int a=str.length();

t3.setText(String.valueOf(a));

else

t1.setText("");

t2.setText("");

t3.setText("");

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
}

Output:

c. Write a java program to implement exception handling.

Code:

class Pract9c

public static void main(String args[])

int a[]={5,10};

int b=5;
NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14
JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
try

int x=a[2]/(b-a[0]);
}

catch(ArithmeticException e)

System.out.println("Divison By Zero Not Possible");

catch(ArrayIndexOutOfBoundsException e)

System.out.println("Array Index Error");

finally

int y=a[1]/a[0];

System.out.println("Result:-"+y);

Output:

Array Index Error

Result:-2

Practical : 10
1.Title:
NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14
JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
GUI Programming 2. Prior Concepts:
1. Data types
2. Loops and Conditional Statements.
3. The declaration and implementation of Scanner class
4. Defining and using function.
5. Inheritance
6. Abstract Class and methods
7. File Handling
3. New Concepts:
Graphical User Interface Abstract
Window Toolkit (AWT)
4. Objectives:
1. Understand the concept of AWT and different inbuilt classes.
2. Understand the concept of Layout Managers
5. Procedure:
1. Create a package
2. Declare a class
3. Inherit the Frame class or any other Parent class from java.awt package
4. Use different inbuilt classes and methods
3. Create a main function to test and implement the class.
6. Implementation :
Done by students in the lab.
7. Results:
O/P of the program
8. Application:
Concepts can be implemented in building software’s.
9. Questions:
a. Design an AWT application that contains the interface to add student information and display the same.
Code:

import java.awt.*; import


java.awt.event.ActionEvent; import
java.awt.event.ActionListener;
public class Prac10a extends Frame implements ActionListener {
Label l1,l2,l3,l4,l5;
Button b1,b2;
TextField t1,t2,t3;

public Prac10a()
{
l1=new Label("Name");
l2=new Label("Phone");
l3=new Label("Address");
l4=new Label("Result");
NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14
JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
l5=new Label(""); b1=new
Button("Submit"); b2=new
Button("Clear"); t1=new
TextField(10); t2=new
TextField(10); t3=new
TextField(10);
setLayout(new GridLayout(5, 2));
add(l1);add(t1);
add(l2);add(t2);
add(l3);add(t3);
add(b1);add(b2);
add(l4);add(l5);
b1.addActionListener(this);
b2.addActionListener(this);
setSize(300, 300);
setVisible(true); }
@Override
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==b1)
{
String data= "Student Name: "+t1.getText();
l4.setText(data);

String data1= "Phone: "+t2.getText()+" Address: "+t3.getText();


l5.setText(data1); t1.setText(""); t2.setText("");
t3.setText("");
} else
{ t1.setText("
");
t2.setText("");
t3.setText("");
l4.setText("");
l5.setText("");
}
}
public static void main(String[] args) {
new Prac10a();
}

Output:

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)

b. Design a calculator based on AWT application.

Code:

import java.awt.*; import java.awt.event.ActionEvent; import

java.awt.event.ActionListener; public class Prac10b extends

Frame implements ActionListener

Label l1,l2,l3;

TextField t1,t2; Button

b1,b2,b3,b4; public

Prac10b()

l1=new Label("Enter no 1");

l2=new Label("Enter no 2");

l3=new Label(""); t1=new

TextField(5); t2=new

TextField(5); b1=new

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
Button("+"); b2=new

Button("-"); b3=new

Button("*"); b4=new

Button("/"); setLayout(new

FlowLayout());

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

add(l1);add(t1);

add(l2);add(t2);

add(b1);add(b2);add(b3);add(

b4); add(l3);

setSize(300, 300);

setVisible(true);

@Override public void

actionPerformed(ActionEvent e) { int

no1= Integer.parseInt(t1.getText()); int

no2= Integer.parseInt(t2.getText()); int

ans=0; if(e.getSource()==b1)

ans= no1+no2;
l3.setText("Addition= "+ans);

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
if(e.getSource()==b2)

ans= no1-no2;

l3.setText("Subtraction= "+ans);

if(e.getSource()==b3)

ans= no1*no2;

l3.setText("Multiplication= "+ans);

if(e.getSource()==b4)

ans= no1/no2;

l3.setText("Division= "+ans);

public static void main(String[] args) {

new Prac10b();

Output:

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)

c. Design an AWT application to generate result marks sheet.

Code:

import java.awt.*; import java.awt.event.ActionEvent; import

java.awt.event.ActionListener; public class prac10c extends

Frame implements ActionListener

Label l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,tot,per;

Button b1,b2;

TextField t1,t2,t3,t4,t5;

Checkbox cb1,cb2;

public prac10c()

l1=new Label("RollNo:");

l2=new Label("Name");

l3=new Label("Sub1 Marks");

l4=new Label("Sub2 Marks");

l5=new Label("Sub3 Marks");

l6=new Label("Total:"); l7=new

Label("Percentage:"); l8=new

Label(""); l9=new Label("");

l10=new Label(""); l11=new

Label("");

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
CheckboxGroup cg1=new CheckboxGroup();

cb1=new Checkbox("Male",cg1,false);

cb2=new Checkbox("Female",cg1,true);

b1=new Button("Submit"); b2=new

Button("Clear"); t1=new

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

t3=new TextField(10); t4=new

TextField(10); t5=new TextField(10);

tot=new Label(""); per=new

Label(""); setLayout(new

GridLayout(11, 2)); add(l1);

add(t1); add(l2); add(t2);

add(cb1); add(cb2); add(l3);

add(t3); add(l4); add(t4);

add(l5); add(t5); add(l6); add(tot);

add(l7); add(per);

add(l8); add(l9);

add(l10); add(l11);

add(b1); add(b2);

b1.addActionListener(this);

b2.addActionListener(this);

setSize(300, 300);

setVisible(true);

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
@Override public void

actionPerformed(ActionEvent e)

{ if(e.getSource()==b1)

int sub1= Integer.parseInt(t3.getText());

int sub2= Integer.parseInt(t4.getText());

int sub3= Integer.parseInt(t5.getText());

int sum= sub1+sub2+sub3; float

percentage=sum/3;

tot.setText(String.valueOf(sum));

per.setText(String.valueOf(percentage));

String data= "Roll No: "+t1.getText();

l8.setText(data);

String data1= "Name: "+t2.getText();

l9.setText(data1);

String data2= "Sub1: "+t3.getText()+"Sub2: "+t4.getText()+"Sub3: "+t4.getText(); l10.setText(data2);

String data3= "Total: "+tot.getText()+" Per: "+per.getText();

l11.setText(data3);

else

t1.setText("");

t2.setText("");

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)
t3.setText("");

t4.setText("");

t5.setText("");

tot.setText("");

per.setText("");

l8.setText("");

l9.setText("");

l10.setText("");

l11.setText("");

public static void main(String[] args) {

new prac10c();

Output:

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14


JAVA PRACTICALS 2020-2021 Sem -IV(SYIT)

NAME :- PRIYANKA ANAND KADIYAL ROLL NO :- 14

You might also like