Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

Java File

Uploaded by

Piet Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Java File

Uploaded by

Piet Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

INDEX

Sr
Program Name Date Grade Signature
No.
WAP to find total, percentage and
1 grade of a student using class in
Java.

WAP to check whether a no is prime


2
or not using function.

WAP to check whether a no is


3 palindrome or not using loops in
Java.

WAP to define a class, create objects


4
and call the methods of the class.

WAP to explain method


5
Overloading.

WAP to explain Constructor


6
Overloading.

7 WAP to explain method Overriding.

WAP to explain different types of


8 inheritance in Java.

WAP to multiple inheritance in Java


9
using interface

WAP to create and use packages In


10
Java.
INDEX
WAP to explain the GUI
11 Programming using text field, label
and command button AWT tools
WAP to explain the GUI
12 Programming using radio buttons
and check boxes AWT tools
WAP to explain the GUI
13 Programming using lists and
combo boxes AWT tools.

WAP to explain the GUI


14 Programming using password field
and text area boxes AWT tools

15 WAP to explain File Handling in Java.

WAP to explain Exception Handling


16
in Java.

WAP to explain the use of finalize()


17
method in Java

WAP to explain multi-threading in


18
Java
Program - 1
PROGRAM TO FIND TOTAL, PERCENTAGE AND GRADE
OF A STUDENT USING CLASS IN JAVA

package javafile;
import java.util.Scanner;

public class FindGrade {


public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
System.out.println("Program to Total, Percentage and Grade of a Student in a class\n");
System.out.println("By- Manan Pasricha(22702)\n");
float sum=0, percentage;
float marks[] = new float[6];
Scanner sc = new Scanner(System.in);
for(int i=0; i<6; i++){
System.out.print("Enter Subject " + (int)(i+1) + " marks : ");
marks[i] = sc.nextFloat();
sum = sum + marks[i];
System.out.println();
}
System.out.println("Total is : " + sum);
percentage = sum/6.0f;
System.out.println("percentage is : " + percentage);
if(percentage>=90){
System.out.println("Grade is A+");
}
else if(percentage>=80){
System.out.println("Grade is A");
}
else if(percentage>=70){
System.out.println("Grade is B+");
}

Page 1 of 2
Program - 1
else if(percentage>=60){
System.out.println("Grade is B");
}
else if(percentage>=50){
System.out.println("Grade is C");
}
else if(percentage>=33){
System.out.println("Grade is D");
}
else{
System.out.println("Grade is F");
}
}
}

OUTPUT

Page 2 of 2
Program - 2
PROGRAM TO CHECK WHETHER A NO IS PRIME OR NOT
USING FUNCTION

package javafile;
import java.util.Scanner;

public class PrimeOrNot {


public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
int number, flag=0;
System.out.print("Enter the number : ");
Scanner sc = new Scanner(System.in);
number = sc.nextInt();
if(number == 0 || number == 1){
System.out.println(number + " is not a prime number");
}
else{
for(int i=2; i<=(number/2); i++){
if(number%i == 0){
System.out.println(number + " is not a prime number");
flag = 1;
break;
}
}
if(flag == 0){
System.out.println(number + " is a prime number");
}
}
}
}

Page 1 of 2
Program - 2
OUTPUT

Page 2 of 2
Program - 3
PROGRAM TO CHECK WHETHER A NO IS PALINDROME
OR NOT USING LOOPS IN JAVA

package javafile;

import java.util.Scanner;

class PallindromeOrNot{
public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
int sum=0,number, temp;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number : ");
number = sc.nextInt();
temp = number;
while(temp > 0){
int r = temp%10;
sum = (sum*10) + r;
temp /= 10;
}
if(sum == number){
System.out.println(number + " is a pallindrome number");
}
else{
System.out.println(number + " is not a pallindrome number");
}
}
}

Page 1 of 2
Program - 3
OUTPUT

Page 2 of 2
Program - 4
PROGRAM TO DEFINE A CLASS, CREATE OBJECTS AND
CALL THE METHODS OF THE CLASS

package javafile;

public class UseOfClass {


boolean isOn;
void switchLight(){
isOn = isOn == false;
}
public static void main(String[] args){
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
UseOfClass Led = new UseOfClass();
System.out.println("Led on? - " + Led.isOn);
Led.switchLight();
System.out.println("Led on? - " + Led.isOn);
Led.switchLight();
System.out.println("Led on? - " + Led.isOn);
}
}

OUTPUT

Page 1 of 1
Program - 5
PROGRAM TO EXPLAIN METHOD OVERLOADING

package javafile;

public class MethodOverloading {


public int sum(int x, int y){
return (x + y);
}
public int sum(int x, int y, int z){
return (x + y + z);
}
public double sum(double x, double y){
return (x + y);
}
public static void main(String[] args){
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
MethodOverloading mt = new MethodOverloading();
System.out.println(mt.sum(10, 20));
System.out.println(mt.sum(10, 20, 30));
System.out.println(mt.sum(10.5, 20.5));
}
}

OUTPUT

Page 1 of 1
Program - 6
PROGRAM TO EXPLAIN CONSTRUCTOR OVERLOADING

package javafile;
public class ConstructorOverloading {
int id;
String name;
ConstructorOverloading() {
System.out.println("Default Constructor");
}
ConstructorOverloading(int id, String name) {
this.id = id;
this.name = name;
}
public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
ConstructorOverloading St1 = new ConstructorOverloading();
System.out.println("Student 1 Details");
System.out.println("\t id is : " + St1.id);
System.out.println("\t name is : " + St1.name);

ConstructorOverloading St2 = new ConstructorOverloading(10,"Manan Pasricha");


System.out.println("Student 2 Details");
System.out.println("\t id is : " + St2.id);
System.out.println("\t name is : " + St2.name);
}
}

Page 1 of 2
Program - 6
OUTPUT

Page 2 of 2
Program - 7
PROGRAM TO EXPLAIN METHOD OVERRIDING

package javafile;

class Bank{
int getRateOfInterest() {
return 0;
}
}
//Creating child classes.
class SBI extends Bank{
int getRateOfInterest() {
return 8;
}
}
class ICICI extends Bank{
int getRateOfInterest() {
return 7;
}
}
class AXIS extends Bank{
int getRateOfInterest() {
return 9;
}
}
public class MethodOverriding {
public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());

Page 1 of 2
Program - 7
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
}
}

OUTPUT

Page 2 of 2
Program - 8
PROGRAM TO EXPLAIN DIFFERENT TYPES OF
INHERITANCE IN JAVA

package javafile;

import java.util.Scanner;

class Student {
int roll;
String name;
Scanner sc = new Scanner(System.in);
public void getData() {
System.out.print("\nEnter Roll no. : ");
roll = sc.nextInt();
System.out.print("\nEnter Name : ");
name = sc.next();
}
public void display(){
System.out.println("\nRoll No. is : " + roll);
System.out.println("\nName is : " + name);
}
}
class BCA extends Student {
int sub1, sub2;
public void readData() {
getData();
System.out.print("\nEnter Subject 1 Marks : ");
sub1 = sc.nextInt();
System.out.print("\nEnter Subject 2 Marks : ");
sub2 = sc.nextInt();
}
public void putData() {

Page 1 of 4
Program - 8
display();
System.out.println("\nSubject 1 marks is : " + sub1);
System.out.println("\nSubject 2 marks is : " + sub2);
}
}
class BcaTotal extends BCA {
int total;
public void findTotal() {
readData();
total = sub1 + sub2;
System.out.println("\nBCA Student data");
putData();
System.out.println("\nTotal Marks are : " + total);
}
}
class BSC extends Student {
int sub1, sub2;
public void readData() {
getData();
System.out.print("\nEnter Subject 1 Marks : ");
sub1 = sc.nextInt();
System.out.print("\nEnter Subject 2 Marks : ");
sub2 = sc.nextInt();
}
public void putData() {
display();
System.out.println("\nSubject 1 marks is : " + sub1);
System.out.println("\nSubject 2 marks is : " + sub2);
}
}
class BscTotal extends BSC {
int total;

Page 2 of 4
Program - 8
public void findTotal() {
readData();
total = sub1 + sub2;
System.out.println("\nBSC Student data");
putData();
System.out.println("\nTotal Marks are : " + total);
}
}
public class UseOfInheritance {
public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
BcaTotal b1 = new BcaTotal();
System.out.println("\nEnter BCA Student data");
b1.findTotal();

BscTotal b2 = new BscTotal();


System.out.println("\nEnter BSC Student data");
b2.findTotal();
}
}

Page 3 of 4
Program - 8
OUTPUT

Page 4 of 4
Program - 9
PROGRAM TO MULTIPLE INHERITANCE IN JAVA USING
INTERFACE

package javafile;

interface First {
int X = 200;
void display();
}
interface Second{
int X = 300;
void display();
}
class Demo implements First, Second{
@Override
public void display(){
System.out.println("Implemented Multiple Inheritance");
}
}
public class UseOfInterface {
public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
Demo d1 = new Demo();
d1.display();
System.out.println("Interface 1 x value is : " + First.X);
System.out.println("Interface 2 x value is : " + Second.X);
}
}

Page 1 of 2
Program - 9
OUTPUT

Page 2 of 2
Program - 10
PROGRAM TO CREATE AND USE PACKAGES IN JAVA

//UseOfPackage.java
package mypackage;
import javafile.CreatePackage;

public class UseOfPackage {


public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
CreatePackage c1 = new CreatePackage();
c1.display();
}
}

//CreatePackage.java
package javafile;

public class CreatePackage {


public void display() {
System.out.println("java file package output");
}
}

OUTPUT

Page 1 of 1
Program - 11
PROGRAM TO EXPLAIN THE GUI PROGRAMMING USING
TEXT FIELD, LABEL AND COMMAND BUTTON AWT
TOOLS

package javafile;

import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class GuiProgramming1 {


public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
MainFrame1 obj= new MainFrame1("String Concatenation");
}
}
class MainFrame1 extends Frame{
public MainFrame1(String title)
{
super(title);
Label[] label = new Label[3];
TextField[] objtxt=new TextField[3];
String[] str= {"Enter First Name : ","Enter Last Name : ","Result : "};
int x=40,y=80;
for (int i=0;i<2;i++)
{
label[i]=new Label(str[i]);

Page 1 of 3
Program - 11
label[i].setBounds(x, y, 120, 25);
add(label[i]);
y=y+60;
}
y=80;
for(int i=0;i<objtxt.length-1;i++)
{
objtxt[i]=new TextField();
objtxt[i].setBounds(165, y, 120, 25);
add(objtxt[i]);
y=y+60;
objtxt[i].setEditable(true);
}

Button objbtn=new Button("Concatenate");


objbtn.setBounds(80,200,120,35);
add(objbtn);
label[2]=new Label(str[2]);
label[2].setBounds(40, 260, 120, 25);
add(label[2]);
objtxt[2]=new TextField();
objtxt[2].setBounds(165, 260, 120, 25);
objtxt[2].setEditable(false);
add(objtxt[2]);
setSize(360,360);
setLayout(null);
setVisible(true);
//event handling
objbtn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
objtxt[2].setText(objtxt[0].getText() +" "+ objtxt[1].getText());

Page 2 of 3
Program - 11
}
});
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
dispose();
}
});
}
}

OUTPUT

Page 3 of 3
Program - 12
PROGRAM TO EXPLAIN THE GUI PROGRAMMING USING
RADIO BUTTONS AND CHECK BOXES AWT TOOLS

package javafile;

import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class GuiProgramming2 {


public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
MainFrame2 obj= new MainFrame2("Student Details");
}
}
class MainFrame2 extends Frame{
MainFrame2(String title){
super(title);
Label[] label = new Label[6];
TextField[] objtxt=new TextField[3];
String[] str= {"Enter First Name : ","Enter Last Name : ","e-Mail id : "};
int x=40,y=80;
for (int i=0;i<3;i++)
{

Page 1 of 4
Program - 12
label[i]=new Label(str[i]);
label[i].setBounds(x, y, 120, 25);
add(label[i]);
y=y+60;
}
y=80;
for(int i=0;i<objtxt.length;i++)
{
objtxt[i]=new TextField();
objtxt[i].setBounds(165, y, 180, 25);
add(objtxt[i]);
y=y+60;
objtxt[i].setEditable(true);
}

label[3] = new Label("Select Gender");


label[3].setBounds(40, 260, 120, 25);
add(label[3]);
CheckboxGroup cgp = new CheckboxGroup();
Checkbox radio1=new Checkbox("Male",cgp,false);
radio1.setBounds(165, 260, 80, 20);
add(radio1);
Checkbox radio2=new Checkbox("Female",cgp,true);
radio2.setBounds(260, 260, 80, 20);
add(radio2);

label[4] = new Label("Select Sports");


label[4].setBounds(40, 320, 120, 25);
add(label[4]);
Checkbox checkbox1=new Checkbox("Volleyball");
checkbox1.setBounds(165, 320, 80, 20);

Page 2 of 4
Program - 12
add(checkbox1);
Checkbox checkbox2=new Checkbox("Basketball");
checkbox2.setBounds(260, 320, 80, 20);
add(checkbox2);
Checkbox checkbox3=new Checkbox("Swimming");
checkbox3.setBounds(165, 380, 80, 20);
add(checkbox3);
Checkbox checkbox4=new Checkbox("Cycling");
checkbox4.setBounds(260, 380, 80, 20);
add(checkbox4);
Button objbtn=new Button("Upload");
objbtn.setBounds(100,440,180,35);
add(objbtn);
setSize(400,600);
setLayout(null);
setVisible(true);
//event handling
objbtn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
label[5] = new Label();
label[5].setBounds(130,500,115,20);
label[5].setText("Details Submitted!!!");
label[5].setBackground(Color.BLACK);
label[5].setForeground(Color.white);
add(label[5]);

}
});
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){

Page 3 of 4
Program - 12
dispose();
}
});
}
}

OUTPUT

Page 4 of 4
Program - 13
PROGRAM TO EXPLAIN THE GUI PROGRAMMING USING
LISTS AND COMBO BOXES AWT TOOLS

package javafile;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.List;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
*
* @author Manan
*/
public class GuiProgramming3 {
public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
MainFrame3 obj= new MainFrame3("Student Details");
}
}
class MainFrame3 extends Frame{
MainFrame3(String title){
super(title);
Label[] label = new Label[6];
TextField[] objtxt=new TextField[3];

Page 1 of 4
Program - 13
String[] str= {"Enter First Name : ","Enter Last Name : ","e-Mail id : "};
int x=40,y=80;
for (int i=0;i<3;i++)
{
label[i]=new Label(str[i]);
label[i].setBounds(x, y, 120, 25);
add(label[i]);
y=y+60;
}
y=80;
for(int i=0;i<objtxt.length;i++)
{
objtxt[i]=new TextField();
objtxt[i].setBounds(165, y, 180, 25);
add(objtxt[i]);
y=y+60;
objtxt[i].setEditable(true);
}

label[3] = new Label("Select Gender");


label[3].setBounds(40, 260, 120, 25);
add(label[3]);
CheckboxGroup cgp = new CheckboxGroup();
Checkbox radio1=new Checkbox("Male",cgp,false);
radio1.setBounds(165, 260, 80, 25);
add(radio1);
Checkbox radio2=new Checkbox("Female",cgp,true);
radio2.setBounds(260, 260, 80, 25);
add(radio2);

label[4] = new Label("Select Sports");

Page 2 of 4
Program - 13
label[4].setBounds(40, 320, 120, 25);
add(label[4]);
Checkbox checkbox1=new Checkbox("Volleyball");
checkbox1.setBounds(165, 320, 80, 25);
add(checkbox1);
Checkbox checkbox2=new Checkbox("Basketball");
checkbox2.setBounds(260, 320, 80, 25);
add(checkbox2);
Checkbox checkbox3=new Checkbox("Swimming");
checkbox3.setBounds(165, 380, 80, 25);
add(checkbox3);
Checkbox checkbox4=new Checkbox("Cycling");
checkbox4.setBounds(260, 380, 80, 25);
add(checkbox4);
label[5] = new Label("Select Class");
label[5].setBounds(40, 440, 120, 25);
add(label[5]);
List l1 = new List(4);
l1.add("9th");
l1.add("10th");
l1.add("11th");
l1.add("12th");
add(l1);
l1.setBounds(165,440,120,35);
Button objbtn=new Button("Upload");
objbtn.setBounds(100,500,180,35);
add(objbtn);
setSize(400,600);
setLayout(null);
setVisible(true);
//event handling
objbtn.addActionListener(new ActionListener(){

Page 3 of 4
Program - 13
@Override
public void actionPerformed(ActionEvent e) {
label[5] = new Label();
label[5].setBounds(130,560,115,25);
label[5].setText("Details Submitted!!!");
label[5].setBackground(Color.BLACK);
label[5].setForeground(Color.white);
add(label[5]);

}
});
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
dispose();
}
});
}
}

OUTPUT

Page 4 of 4
Program - 14
PROGRAM TO EXPLAIN THE GUI PROGRAMMING USING
PASSWORD FIELD AND TEXTAREA BOXES AWT TOOLS

package javafile;

import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import static java.awt.Font.BOLD;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JPasswordField;
import java.awt.TextArea;
public class GuiProgramming4 {
public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
MainFrame4 obj= new MainFrame4("Student Details");
}
}
class MainFrame4 extends Frame{
MainFrame4(String title){
super(title);
setLayout(null);
Label head = new Label("Login Form");
add(head);
head.setBounds(150, 45, 140, 35);
Font f = new Font("Courier", Font.PLAIN, 20);

Page 1 of 3
Program - 14
head.setFont(f);
Label objlbl=new Label("Enter name");
objlbl.setBounds(60, 100, 80,25);
add(objlbl);

Label obj2=new Label("Enter password");


obj2.setBounds(60, 140, 90, 25);
add(obj2);

TextField objtxt=new TextField();


objtxt.setBounds(200, 100, 150, 25);
add(objtxt);

JPasswordField obJPasswordField1=new JPasswordField();


obJPasswordField1.setBounds(200, 140, 150, 25);
add(obJPasswordField1);

Button objbtn=new Button("Login");


objbtn.setBounds(150, 200, 80, 35);
add(objbtn);

Label obj3 = new Label("Result");


obj3.setBounds(60, 240, 100, 25);
add(obj3);

TextArea txt = new TextArea();


txt.setBounds(60, 275, 290, 100);
add(txt);

setVisible(true);
setSize(400, 400);
//event handling

Page 2 of 3
Program - 14
objbtn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
txt.setRows(7);
txt.setText("Your Login Details are : \n\n");
txt.setText(txt.getText()+ "Name : " + objtxt.getText());
txt.setText(txt.getText()+ "\nPassword : " +obJPasswordField1.getText());
}
});
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
dispose();
}
});
}
}

OUTPUT

Page 3 of 3
Program - 15
PROGRAM TO EXPLAIN FILE HANDLING IN JAVA

package javafile;

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

public class FileHandlingMain {


public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
FileHandling objFile=new FileHandling();
objFile.CreateNewFile();
objFile.WriteIntoFile();
objFile.readDataFromFile();
objFile.GetListOfFile();
objFile.deleteFile();
}
}
class FileHandling {
public void CreateNewFile() {
try {
File objfile=new File("D:\\file\\demo.txt");
if(objfile.createNewFile())
{
System.out.println("*****file successfully created "+objfile.getCanonicalPath() +
"*****");
}
else {
System.out.println("*****file already exist "+objfile.getCanonicalPath() +
"*****");

Page 1 of 4
Program - 15
}
}
catch (IOException e) {
System.out.println("Error : Can't create file!!!");
e.printStackTrace();
}
}

public void WriteIntoFile()


{
String string="welcome to java Software edition ";
System.out.println("*****Opening File and Writing Data*****");
try {
FileOutputStream objfos=new FileOutputStream("D:\\file\\demo.txt");
byte b[]=string.getBytes();
objfos.write(b);
objfos.close();
System.out.println("*****Data written successfully and File Closed*****");
}
catch (IOException e) {
System.out.println("Error : Can't wrtie error occured!!!");
e.printStackTrace();
}
}

public void readDataFromFile(){


int i = 0;
try {
System.out.println("*****Opening File and Reading Data*****");
FileInputStream objfis=new FileInputStream("D:\\file\\demo.txt");
while((i=objfis.read())!=-1){
System.out.print((char)i);

Page 2 of 4
Program - 15
}
objfis.close();
System.out.println("\n*****Data reading successful and File Closed*****");
}
catch (IOException e) {
e.printStackTrace();
}
}

public void GetListOfFile(){


System.out.println("*****Listing all the files in current folder*****");
File objfile=new File("D:\\file");
String content[]=objfile.list();
for(int i=0;i<content.length;i++){
System.out.println(content[i]);
}
}

public void deleteFile(){


System.out.println("*****Deleting File*****");
File objfile=new File("D:\\file\\demo.txt");
if(objfile.delete()){
System.out.println("File Deleted Succesfully");
}
else{
System.out.println("Error : Can't delete file!!!");
}
}
}

Page 3 of 4
Program - 15
OUTPUT

Page 4 of 4
Program - 16
PROGRAM TO EXPLAIN EXCEPTION HANDLING IN JAVA

package javafile;
import java.io.*;

class Sample{
void myMethod() throws IOException{
System.out.println("In myMethod() before exception occurs");
throw new IOException("Device Not Working");
}
}
public class ExceptionHandling {
public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
try{
Sample obj = new Sample();
obj.myMethod();
}
catch(IOException e){
System.out.println("Exception Handled");
}
finally{
System.out.println("Finally Block Executed");
}
System.out.println("Last Statement");
}
}

Page 1 of 2
Program - 16
OUTPUT

Page 2 of 2
Program - 17
PROGRAM TO EXPLAIN THE USE OF FINALIZE()
METHOD IN JAVA

package javafile;

class FinalizeDemo {

@Override
protected void finalize() throws Throwable {
try {
System.out.println("inside Finalizedemo's Class finalize()");
}
catch (Throwable e) {
throw e;
}
finally {
System.out.println("Calling finalize method" + " of the Object class");
super.finalize(); // Calling finalize() of Object class
}
}
}
public class FinalizeMethod {
public static void main(String[] args) throws Throwable {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
FinalizeDemo d = new FinalizeDemo();
d.finalize();
}
}

Page 1 of 2
Program - 17
OUTPUT

Page 2 of 2
Program - 18
PROGRAM TO EXPLAIN MULTI THREADING IN JAVA

package javafile;

import static java.lang.Thread.*;

class Thread1 extends Thread {


@Override
public void run() {
for(int i=1; i<5; i++) {
if(i==3)
yield();
System.out.println("Value of i = " + i);
}
}
}
class Thread2 extends Thread{
@Override
public void run() {
for(int j=1; j<5; j++){
try{
if(j==2)
sleep(1000);
}
catch(InterruptedException e){
System.out.println(e);
}
System.out.println("Value of j = " + j);
}
}
}
public class MultithreadingExample {

Page 1 of 2
Program - 18
public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
t1.start();
t2.start();
}
}

OUTPUT

Page 2 of 2

You might also like