You are on page 1of 86

Name.

Kishan Kumar Goud


Roll no. 432 SYBSC.IT

PRACTICAL NO .01
Introduction to Java
PART-(A)

AIM: Write a program in java to display Hello on the screen.

PROGRAM:
class Demo
{
public static void main(String args[])
{
System.out.println("HELLO");
}
}
OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .01
PART-(B)

AIM: Write a Java Program that takes a number as input and print its
multiplication table up to 10.
PROGRAM:
public class Mult_Table1 {
public static void main(String args[])
{
int n=4;
for(int i=1; i<=10; i++)
{
System.out.println(n+"*"+i+"="+n*i);
}
}
}
OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

To take user input :

PROGRAM:
import java.util.Scanner;
public class Mult_Table1 {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.print("Enter Number: ");
int n = s.nextInt();
// int n=4;
for(int i=1; i<=10; i++)
{
System.out.println(n+"*"+i+"="+n*i);
}
}
}
OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .01
PART-(C)

AIM: Write a Java Program to print the area and perimeter of circle.

PROGRAM:
public class CircleParameters {
public static void main(String args[])
{
double radius=20;
//Area=PI*radius*radius
double area = Math.PI*(radius*radius);
System.out.println("The area of Circle is: "+area);

//Circumference=2*PI*radius
double circumference = Math.PI*2*radius;
System.out.println("The Circumference Of the Circle is: "+circumference);
}
}
OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .01
PART-(D)

AIM: Write a Java Program to display the following star pattern.

*****

****

***

**

PROGRAM:
public class StarPattern {
public static void main(String[] args)
{
int rows = 5;
for(int i = rows; i>=1; --i)
{
for(int j = 1; j<=i; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .01
PART-(E)

AIM: Write a Java Program to display the following number pattern.


55555

4444

333

22

PROGRAM:
public class StarPattern {
public static void main(String[] args)
{
int rows = 5;
for(int i = rows; i>=1; --i)
{
for(int j = 1; j<=i; ++j)
{
System.out.print(i);
}
System.out.println();
}
}
}

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .01

PART-(F)

AIM: Write a Java Program to display the following number pattern.


12345

1234

123

12

PROGRAM:
public class NumberPattern2 {
public static void main(String[] args)
{
int rows = 5;
for(int i = rows; i>=1; --i)
{
for(int j = 1; j<=i; ++j)
{
System.out.print(j);
}
System.out.println();
}
}
}

OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 02

PART A
AIM: write a java program to reverse a string

PROGRAM:
import java.util.*;

class ReverseString {
public static void main(String args[])
{
String original, reverse="";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
original = in.nextLine();
int length = original.length();

for (int i = length-1; i>=0; i--)


{
reverse = reverse + original.charAt(i);
}
System.out.println("Reverse string: "+ reverse);

}
}

OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 02

PART B
Aim: Write a java program to add two binary numbers

PROGRAM:
import java.util.Scanner;
class AddBinary {
public static void main(String args[])
{
System.out.println("Program add two binary numbers");
Scanner scnr = new Scanner(System.in);
System.out.println("Please enter the first binary number");
String first = scnr.nextLine();
System.out.println("Please enter the second binary number");
String second = scnr.nextLine();

String addition = add(first, second);


System.out.println("addition of two binary number is:"+ addition);
scnr.close();
}
public static String add(String first, String second)
{
int b1 = Integer.parseInt(first, 2);
int b2 = Integer.parseInt(second, 2);
int sum = b1 + b2;
return Integer.toBinaryString(sum);
}

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

}
OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 2

PART C

AIM: Write a java code to convert decimal to binary and vice versa

PROGRAM:
import java.util.*;
class Dec_to_Bin
{
public static void main(String arg[])
{

Scanner sc= new Scanner(System.in);


System.out.println("enter a decimal number");
int n=sc.nextInt();

int bin[]=new int[100];


int i=0;

while(n>0)
{
bin[i++]=n%2;
n=n/2;
}
System.out.println("Binary number is : ");
for(int j= i-1;j>=0;j--)
{

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

System.out.print(bin[j]);

}
OUTPUT:

PRACTICAL NO 3

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PART A

AIM: Find the smallest and largest element for array

PROGRAM:
public class LargestSmallest {
public static void main(String args[])
{
int a[] = new int[] {10,20,30,40};
int min = a[0];
int max = a[0];
for(int i=1; i<a.length; i++)

{
if(a[i]>max)
{
max = a[i];
}
if (a[i]<min)
{
min = a[i];
}
}
System.out.println("Largest Number in a given array is: "+max);
System.out.println("Smallest Number in a given array is: "+min);
}
}
OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 3

PART B
AIM: Write the java program to count the letters, spaces, number and other
characters of an input string
PROGRAM:

import java.util.*;
class IdentifyChar
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
System.out.print("Enter a string:");
String str=input.nextLine();
int letter=0,space=0,digit=0,other=0;
char ch[]=str.toCharArray();

for(int i=0;i<str.length();i++)
{
if(Character.isLetter(ch[i]))
{
letter++;
}
else if(Character.isDigit(ch[i]))
{
digit++;
}

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

else if(Character.isSpaceChar(ch[i]))
{
space++;
}
else
{
other++;
}
}

System.out.println("Letter are:"+letter);
System.out.println("Space are:"+space);
System.out.println("Digit are:"+digit);
System.out.println("Other:"+other);
}
}

OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 3

PART C
AIM: Implement a java function that calculates the sum of digits for a given
char array consisting of the digits ‘0’ to ‘9’.
PROGRAM:

import java.util.*;
class CalSumDig
{
public static void main(String args[])
{
Scanner ob= new Scanner(System.in);
System.out.print("Enter any string:");
String s=ob.nextLine();
count(s);
}
public static void count(String str)
{
int sum=0;
char ch[]=str.toCharArray();
for(int i=0;i<str.length();i++)
{
if(Character.isDigit(ch[i]))
{
sum+=Character.getNumericValue(ch[i]);
}
}
System.out.println("Your addition is:"+sum);

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

}
}
OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 4

PART A

AIM:Write a program in java to implement the concept of inheritance

PROGRAM:
class Employee {
float salary=40000;
}
class Programmer extends Employee
{
int bonus=10000;
public static void main(String[] args) {
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus salary is:"+p.bonus);

}
}

OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 4

PART B

AIM: Write a program in Java to implement single level inheritance


Q] Create a base class teacher and a sub class IT_Teacher.
Class IT_Teacher extends the designation and college_name properties and
work() method from base class techer, we need not to declare these properties
and method in sub class.
Class Teacher have college_Name, designation and work() method which are
common to all the teachers.
PROGRAM:
class Teacher

String designation = "Teacher";

String collegeName="Patkar College";

void work()

System.out.println("Teaching");

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

class IT_Teacher extends Teacher

String mainSubject = "IT";

public static void main(String args[])

IT_Teacher obj= new IT_Teacher();

System.out.println(obj.collegeName);

System.out.println(obj.designation);

System.out.println(obj.mainSubject);

obj.work();

}
OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 4

PART C

AIM: Write a program in java to implement multi level inheritance.

PROGRAM:
class Animal
{
void eat()
{

System.out.println("eaing..");
}

class Dog extends Animal


{

void bark()
{

System.out.println("barking..");
}

class BabyDog extends Dog


{

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

void weep()
{

System.out.println("weeping..");
}

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

BabyDog d= new BabyDog();


d.weep();
d.bark();
d.eat();
}
}
OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 4

PART D

AIM: Write a program in java to implement hierarical inheritance.

PROGRAM:
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 TestInheritance3
{

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

public static void main(String args[])


{
Cat c=new Cat();
c.meow();
//c.bark();//C.T.Error
}
}
OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .05

Topic: Packages and Arrays


PART-(A)

AIM: Write a Java Program to add two matrices and print the resultant matrix.

PROGRAM:
class ArrayExm
{
public static void main(String args[])
{
int [][] a = {{1,2,3}, {4,5,6}, {7,8,9}};
int [][] b = {{1,2,2}, {2,5,2}, {2,1,1}};
int [][] c = new int[3][3];

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


{
for(int j=0; j<3; j++)
{
c[i][j] = a[i][j] + b[i][j];
System.out.print(c[i][j] + "");
}
System.out.println();
}

}
}

Output:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .05

PART-(B)

AIM: Write a Java Program to Multiply of 2 Matrices.

PROGRAM:
class ArrayExm
{
public static void main(String args[])
{
int [][] a = {{1,2,3}, {4,5,6}, {7,8,9}};
int [][] b = {{1,2,2}, {2,5,2}, {2,1,1}};
int [][] c = new int[3][3];

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


{
for(int j=0; j<3; j++)
{
c[i][j] = a[i][j] + b[i][j];
System.out.print(c[i][j] + " ");
}
System.out.println();
}

}
}

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .06

Topic: Methods and Constructors


PART-(A)

AIM: Write a Java Program to Designed a class SortData that contains the
method asec() and desc().
PROGRAM:
import java.util.*;
class SortData
{
Scanner input = new Scanner(System.in);
int num, i;
int arr[];
int temp = 0;
public void getData()
{
System.out.print("Enter the size of array: ");
num = input.nextInt();
arr = new int[num];

System.out.print("Enter the number: ");


for(i=0; i<num; i++)
{
arr[i] = input.nextInt();
}
}
void putData()
{

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

System.out.print("Given numbers are: ");


for(i=0; i<num; i++)
{
System.out.println(arr[i]);
}
}
void asce()
{
for(i=0; i<num; i++)
{
for(int j=i+1; j<num; j++)
{
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.print("Ascending order of number are: ");
for(int i=0; i<num; i++)
{
System.out.println(arr[i]);
}
}
void desc()

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

{
for(i=0; i<num; i++)
{
for(int j=i; j<num; j++)
{
if(arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.print("Descending order of number are: ");
for(int i=0; i<num; i++)
{
System.out.println(arr[i]);
}
}
public static void main(String args[])
{
SortData ob = new SortData();
ob.getData();
ob.putData();
ob.asce();
ob.desc();
}

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

}
OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 6
PART B
AIM: Design a class that demonstrates the use of constructor and destructor.
PROGRAM:
class xyz
{
xyz()
{
System.out.println("Constructor method");
}
protected void finalize()
{
System.out.print("Garbage Collected");
}
}
class ConsDes
{
public static void main(String args[])
{
xyz ob=new xyz();
ob=null;
System.gc();
}
}
Output:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 6
PART C
AIM: Write a java program to demonstrate the implementation of abstract class.
PROGRAM:
import java.util.Scanner;
abstract class test
{
abstract void get();
}
class test1 extends test
{
void get()
{
int a,b;
Scanner ob=new Scanner(System.in);
System.out.print("Enter 1st Number:");
a=ob.nextInt();
System.out.print("Enter 2nd Number:");
b=ob.nextInt();
System.out.print("Addition is:"+(a+b));
}
}
class prac4c
{
public static void main(String args[])
{
test1 obj=new test1();

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

obj.get();
}
}
Output :

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 7
PART A
AIM: Write a java program to create an array and display all elements from
array.
PROGRAM:
class Array1
{
public static void main(String args [])
{
//create an array
int[] age = {12, 4, 5};
System.out.println("Using for-each Loop: ");
for(int a : age)
{
System.out.println(a);
}
}
}
Output :

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 7
PART B
AIM: Compute sum and average of array elements
PROGRAM:
class Array1
{
public static void main(String[] args)
{
int[]age={12,4,5};
int s=0;
int avg=0;
for(int a: age)
{
s=s+a;
}

System.out.println("Sum:"+s);
avg = s/age.length;
System.out.println("Avg:"+avg);
}
}
Output:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 7
PART C
AIM: Java code to concatenate two arrays.
PROGRAM:
import java.util.Arrays;
public class ConcatArray
{
public static void main(String agrs[])
{
int[] array1 = {1,2,3};
int[] array2 = {4, 5, 6};
int length = array1.length + array2.length;
int[] result = new int[length];
int pos = 0;
for(int element: array1)
{
result[pos] = element;
pos++;
}
for(int element: array2)
{
result[pos] = element;
pos++;
}
System.out.println(Arrays.toString(result));
}
}

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Output :

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 7
PART D
AIM: Write a java code to change an array element.
PROGRAM:
import java.util.*;
public class ArrayElement
{
public static void main(String args[])
{
int A[] = {1, 2, 3, 4, 5};
A[1] = 100;
System.out.println("Updated array is: ");
for(int i=0; i<A.length; i++)
{
System.out.println(A[i] +" ");
}
}
}
Output:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 7
PART E
AIM: Find array length.
PROGRAM:
import java.util.*;
public class Length
{
public static void main(String args[])
{
System.out.print("Enter size of array n:");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int A[]=new int[n];
System.out.print("Enter elements:");
for(int i=0;i<n;i++)
{
A[i]=s.nextInt();
}
System.out.print("Length is"+ A.length);
}
}
Output:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 8
PART A
AIM: Create a package, add the necessary classes and import the package in
java class.
Program:
package pack;
public class A
{
public void msg()
{
System.out.println("Hello");
}

public static void main(String args[])

A obj=new A();
obj.msg();
}
}
Output :

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 8
PART B
AIM: To create package of class A
Program :
package pack;
public class A
{
public void msg()
{

System.out.println("Hello");
}

}
package mypack;
import pack.*;
class B
{
public static void main(String args[])
{

A obj = new A();


obj.msg();
}
}
OUTPUT :

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 8
PART C
AIM: To import a particular class from package
PROGRAM:
package pack1;
public class Demo
{
public void msg()
{

System.out.println("Hello");
}
}
package mypack1;
import pack1.Demo;
class DemoRun
{
public static void main(String args[])

{
Demo obj = new Demo();
obj.msg();
}

}
Output:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 8
PART D
AIM: To use the package without importing
PROGRAM:
package pack2;
public class Demo2
{
public void msg()
{

System.out.println("Hello");
}
}

package mypack2;
class DemoRun2
{
public static void main(String args[])

{
pack2.Demo2 obj= new pack2.Demo2();
obj.msg();
}

}
Output:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 8
PART E
Aim: write a program in java to implement interface
Program :
package pack1;
public interface I1
{
public void msg();
}

package mypack1;
import pack1.*;
public class InterfaceUse implements I1
{
public void msg()
{

System.out.println("Interface Example");
}

public static void main(String args[])

InterfaceUse obj = new InterfaceUse();


obj.msg();
}

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

}
Output:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 8
PART F
Aim :
Program :
package animals;
public interface animal1
{
public void eat();
public void travel();
}

package mammals;
import animals.*;
public class mammal1 implements animal1
{
public void eat()
{
System.out.println("An animals eating");
}
public void travel()
{
System.out.println("An animals travelling");
}
public static void main(String args[])
{

mammal1 obj=new mammal1();

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

obj.eat();
obj.travel();
}

}
Output :

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO 9
PART A
AIM: Write a Java Program to implement the concept of an Interface.

PROGRAM:
interface Language
{
void getName(String name);
}
class ProgrammingLanguage implements Language
{
public void getName(String name)
{
System.out.println("Programming Language:" + name);
}
}
class Main
{
public static void main(String[] args)
{
ProgrammingLanguage language = new ProgrammingLanguage();
language.getName("Java");
}
}

OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .09

PART-(B)

AIM: Write a Java Program to create a class or design a class that implement
two interfaces.
PROGRAM:
interface Base
{
public int getBase(int b);
}

interface Height
{
public int getHeight(int h);
}
class Triangle implements Base,Height
{
public int getBase(int b)
{
return b;
}
public int getHeight( int h)
{
return h;
}
public void getArea(int B, int H)
{
System.out.println("Area=" + 0.5*B*H);

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

}
}
public class Demoin
{
public static void main(String args[])
{
Triangle a = new Triangle();
int b = a.getBase(5);
int h = a.getHeight(6);
a.getArea(b,h);
}
}

OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .09

PART-(C)

AIM: Write a Java Program to design an interface that implements an interface.

PROGRAM:
interface Base
{
public int getBase(int b);
}
interface Height
{
public int getHeight(int h);
}
interface Area extends Base,Height
{
public void getArea (int B, int H);
}
class Triangle implements Area
{
public int getBase(int b)
{
return b;
}
public int getHeight(int h)
{
return h;
}
public void getArea(int B, int H)

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

{
System.out.println("Area=" + 0.5*B*H);
}
}
public class Demoin1
{
public static void main(String args[])
{
Triangle a = new Triangle();
int b = a.getBase(5);
int h = a.getHeight(6);
a.getArea(b,h);
}
}

OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .10

File Operations
PART-(A)

AIM: Write a Java Program to open a file and display the contents in the
console window.
PROGRAM:
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class FileReadDemo
{
public static void main(String [] args) throws IOException
{
FileReader fr = new FileReader("Oceans.txt");
Scanner inFile = new Scanner(fr);
String line;
line = inFile.nextLine();
//Display the line.
System.out.println(line);
inFile.close();
}
}

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .10

PART-(B)

AIM: Write a Java Program to perform write operation on file.

PROGRAM:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileExample


{
public static void main(String args[])
{
try
{
String content = "Hello, How are you?";
File file = new
File("C:\\Program Files\\Java\\jdk1.8.0_51\\bin\\abc.txt");
if(!file.exists())
{
file.createNewFile();
}
FileWriter fw = new
FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

System.out.println("DONE...");
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .10

PART-(C)

AIM: Write a Java Program to copy the contents from one file to another file.

PROGRAM:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopyExample


{
public static void main(String args[])
{
FileInputStream instream = null;
FileOutputStream outstream = null;
try
{
File infile = new
File("C:\\Program Files\\Java\\jdk1.8.0_51\\bin\\MyInputFile.txt");
File outfile = new
File("C:\\Program Files\\Java\\jdk1.8.0_51\\bin\\MyOutputFile.txt");

instream = new FileInputStream(infile);


outstream = new FileOutputStream(outfile);
int length;
byte[] buffer = new byte[1024];

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

while ((length = instream.read(buffer)) > 0)


{
outstream.write(buffer,0,length);
}
instream.close();
outstream.close();
System.out.println("File copied successfully!!!");
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}

OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .11

PART-(A)

AIM: Write a Java Program to display a button in frame.

PROGRAM:
import java.awt.*;
public class ButtonExample
{
public static void main(String[] args)
{
Frame f=new Frame("Button Example");
Button b=new Button("OK");
b.setBounds(50,100,80,30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .11

PART-(B)

AIM: Write a Java Program to display a label in frame. [LABELEXAMPLE]

PROGRAM:
import java.awt.*;
public class LabelExample
{
public static void main(String args[])
{
Frame f=new Frame("Label Example");
Label l1,l2;
l1 = new Label("First Label.");
l1.setBounds(50,100,100,30);

l2 = new Label("Second Label.");


l2.setBounds(50,150,100,30);

f.add(l1);
f.add(l2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .11

PART-(C)

AIM: Write a Java Program to design a AWT program to print the factorial for
an input value.
PROGRAM:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Test implements ActionListener


{
Button b1;
TextField t1;
Label lb1, lb3;
Frame f;
Test()
{
f = new Frame("Awt Find Factorial");
lb1 = new Label("Enter a Number: ");
lb1.setBounds(5,50,150,30);
f.add(lb1);

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

t1 = new TextField();
t1.setBounds(200,50,150,30);
f.add(t1);

lb3 = new Label("Result: ");


lb3.setBounds(90,140,150,30);
f.add(lb3);

b1 = new Button("Find");
b1.setBounds(90,200,100,30);
f.add(b1);

b1.addActionListener(this);
f.addWindowListener (new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});

f.setLayout(null);
f.setSize(500,500);
f.setVisible(true);
}

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

@Override
public void actionPerformed(ActionEvent e)
{
int n = Integer.parseInt(t1.getText());
int f = 1;
if (e.getSource().equals(b1))
{
for (int i = 1; i<= n; i++)
{
f = f * i;
}
lb3.setText(String.valueOf("Factorials : " +f));
}
}
public static void main(String args[])
{
Test t = new Test();
}
}
OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .11

PART-(D)

AIM: Write a Java Program to design a AWT program to perform various


string operations - Concatination.
PROGRAM:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Test1 implements ActionListener


{
Button b1;
TextField t1,t2;
Label lb1,lb3;
Frame f;
Test1()
{
f = new Frame("awt Find Factorial");

lb1 = new Label("Enter a String: ");


lb1.setBounds(5,50,150,30);
f.add(lb1);

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

t1 = new TextField();
t1.setBounds(200,50,150,30);
f.add(t1);

t2 = new TextField();
t2.setBounds(200,100,150,80);
f.add(t2);

lb3 = new Label("Result: ");


lb3.setBounds(90,140,150,30);
f.add(lb3);

b1 = new Button("Find");
b1.setBounds(90,200,100,30);
f.add(b1);

b1.addActionListener(this);
f.addWindowListener (new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

f.setLayout(null);
f.setSize(500,500);
f.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
String n1=t1.getText();
String n2=t2.getText();
String n3=n1+n2;
if(e.getSource().equals(b1))
{
lb3.setText(n3);
}
}
public static void main(String args[])
{
Test1 t = new Test1();
}
}
OUTPUT:

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .11

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PART-(E)

AIM: Write a Java Program to handle Arithmetic Exception.

PROGRAM:
public class TestFinallyBlock2
{
public static void main(String args[])
{
try
{
int data = 25/0;
System.out.println(data);
}
catch(ArithmeticException e)
{
System.out.println("STOP!");
}
}
}
OUTPUT:

PRACTICAL NO .11

CORE JAVA
Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PART-(F)

AIM: Write a Java Program to handle Array Index out of Bounds Exception.

PROGRAM:
public class Excep6
{
public static void main(String args[])
{
try
{
int a[]=new int[5];
a[5]=4;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
}
}
OUTPUT:

CORE JAVA

You might also like