0% found this document useful (0 votes)
18 views

Java File

java code

Uploaded by

himani namdeo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Java File

java code

Uploaded by

himani namdeo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 84

JAVA Lab Manual

BTech: II Year

Department of Computer Science and Information Technology

Name :

Branch :

Section :

Roll No. :

CSIT Department Java Programming Lab Manual Session July-Dec2023


ACROPOLIS INSTITUTE OF TECHNOLOGY & RESEARCH, INDORE

Department of Computer Science and Information Technology

Certificate

This is to certify that the experimental work entered in this journal as per the B
Tech II year III semester, syllabus prescribed by the RGPV was done by Mr.
………………………………… in the Java Programming Lab (CSIT 306) of
Acropolis Institute during the academic year 2023- 2024.

Signature of Faculty

CSIT Department Java Programming Lab Manual Session July-Dec2023


Department of Computer Science and Information Technology

Vision of the Institute

To be an academic leader for the development of human potential so as to meet the global
challenges.

Mission of the Institute

1. To create an intellectually stimulating learning environment.


2. To impart value based, innovative, and research oriented education.
3. To develop positive attitude with communication skills.
4. To increase employability and entrepreneurship through collaboration with industries and
professional organizations.

CSIT Department Java Programming Lab Manual Session July-Dec2023


Acropolis Institute of Technology and Research, Indore.
Department of Computer Science and Information Technology

Course Objective:
The subject/course objectives support the program objectives and outcomes. This subject will help students to
learn and understand the concept of
CSIT-306.CEO1 Understand fundamentals of programming such as variables, conditional and
iterative execution, methods, etc
CSIT-306.CEO2 Understand fundamentals of object-oriented programming in Java and be familiar
of the important concepts like class, inheritance and multithreading, AWT and
JDBC.
CSIT-306.CEO3 Students will able to use the Java SDK environment to create, debug and run
simple Java programs.

Course Outcome:
Upon completion of this subject / course the student will be able to:
Bloom’s Course Outcomes
levels
CSIT-306.1 Apply The concepts of Java programming.
Analyze
understand
CSIT-306.2 Apply The basic terminology used in computer programming and write,
Analyze
compile and debug
programs in JAVA language.
understand
CSIT-306.3 Apply . The different data types, decision structures, loops, functions to design
Analyze
Java programs.

understand
CSIT-306.4 Apply Develop program using the java collection API as well as the java
Analyze standard class library.
understand
CSIT-306.5 Apply Develop Java applets.
Analyze
understand

CSIT Department Java Programming Lab Manual Session July-Dec2023


Acropolis Institute of Technology and Research, Indore.
Department of Computer Science and Information Technology

GENERAL INSTRUCTIONS FOR LABORATORY CLASSES

DO’S

❖ Without Prior permission do not enter into the Laboratory.

❖ While entering into the LAB students should wear their ID cards.

❖ The Students should come with proper uniforms.

❖ Students should sign in the LOGIN REGISTER before entering into the laboratory.

❖ Students should come with observation and record notebooks to the laboratory.

❖ Students should maintain silence inside the laboratory.

❖ After completing the laboratory exercise, make sure to shutdown the system properly

DONT’S

❖ Students bringing the bags inside the laboratory.

❖ Students using the computers in an improper way.

❖ Students scribbling on the desk and mishandling the chairs.

❖ Students using mobile phones inside the laboratory.

❖ Students making noise inside the laboratory.

CSIT Department Java Programming Lab Manual Session July-Dec2023


Department of Computer Science and Information Technology

Acropolis Institute of Technology and Research, Indore.

SYLLABUS
I I-Overview of Java, Installation, First Simple Program, Compilation process , Java
Keywords , Identifiers , Literals, Comments, Data Types, Variables, Dynamic initialization, type
conversion and casting, Operators, Control Statements.
II -Declaring Objects, Introducing Methods, Constructors, this Keyword, Garbage
Collection, finalize Method, Overloading Methods, Overloading Constructors, Using Objects as
Parameters, Inheritance, Creating a Multilevel Hierarchy, Packages and Interfaces, Exception
Handling, Multithreaded.
III The Applet Class: Applet Basics, The Applet Class, Applet Architecture, Applet
Initialization and Termination , Simple Applet Display Methods, Simple Banner Applet, Using
the Status Window, The HTML APPLET Tag, Passing Parameters to Applets, Improving the
Banner Applet.
IV -Introducing the AWT: Working with Windows, Graphics, and Text, AWT Classes,
Window Fundamentals, Component, Container, Panel, Frame, Working with Frame Windows,
Handling Events in a Frame Window, AWT Controls, Layout Managers, and Menus, Adding
and Removing Controls, Grid Layout, Border Layout, introduction to swing and servlet.
V Event Handling, Two Event Handling Mechanisms, The Delegation Event Model,
Events, Event Sources, Event Listeners, Event Classes, The Mouse Event Class and others,
JDBC: JDBCODBC bridge, the connectivity model, the driver manager, navigating the result set
object contents, the JDBC exceptional classes, connecting to remote database

CSIT Department Java Programming Lab Manual Session July-Dec2023


Department of Computer Science and Information Technology

Acropolis Institute of Technology and Research, Indore.

HARDWARE REQUIREMENTS:
Processors - 2.0 GHz or Higher
RAM - 512 MB or Higher
Hard Disk - 20 GB or Higher

SOFTWARE REQUIREMENTS
● Operating System: Windows 8/8.1/10/11
● JavaC Compiler

PREREQUISITE:-
Basics of OOPs

CSIT Department Java Programming Lab Manual Session July-Dec2023


Teaching and Examination Scheme
Subj Sub Maximum Marks Hours/ Credits
ect ject Allotted Week
Code Na Practical
me E Lab work T L T P
n sessional o
d t
S a
e l
m M
. a
r
k
s
CSIT Dot
30 20 50 4 2
406 Net

Department of Computer Science and Information Technology

Acropolis Institute of Technology and Research, Indore


RATIONALE:

Java is a multi-purpose computer-based language that is created on the ideology of write-once run anywhere. It
was invented by James Gosling and Sun Microsystems back in 1991 and at that time their only intention was to
enhance the code reusability. Java is an object-oriented programming language based on C and C++ languages,
however, it is an improved and simplified programming language with the capability of handling errors in
programming. Java is used for multiple purposes such as web development, mobile application developments,
etc.

CSIT Department Java Programming Lab Manual Session July-Dec2023


Lab Plan

S. No. Name of Experiment


1 Write two Java programs on datatypes.
2 Write a Java Program to Check Even or Odd Number.
3 Write a Java Program to Multiply two Numbers.
4 Write a Java Program to check Leap Year.
5 Write a Java Program to check whether input character is vowel or consonant.
6 Write a Java Program to calculate compound interest.
7 Write a Java Program to calculate simple interest
8 Write a Java Program to find quotient and remainder
9 Write a Java program to print table of an integer number
10 Write a Java program to print all prime numbers from 1 to N
11 Write a Java program to print ODD Numbers from 1 to N
12 Write a Java program to print EVEN Numbers from 1 to N
13 Write a Java program to count total number of words in a string
14 Write a Java program to find Largest of Three Numbers
15 Write a Java program to print numbers from 1 to 10 using while loop

CSIT Department Java Programming Lab Manual Session July-Dec2023


16 Write a Java program to print numbers from 1 to N using for loop
17 Write a Java program to generate table of an integer given by user.
18 Write a Java program for Factorial
19 Write a Java program for Fibonacci Series
20 Write a Java program for Palindrome Number
21 Write a Program to find number of elements in an array

22 Write a Java Program to Calculate average of numbers using Array


23 Write a Java Program to find duplicate elements of an array
24 Write a Java Program to find duplicate elements of an array
25 Write a Java program for Addition of Two 1-D array
26 Write a Java program to add digits of numbers stored in an array.
27 Program to demonstrate overloaded constructor for calculating box volume.
28 Program to show the details of students using the concept of inheritance.
29 Program to demonstrate package concept.
30 Program to Make Shape as an Interface and Implement it using Circle and Rectangle Class.
31 Program to demonstrate exception handling in case of division by zero error.
Program No.

Aim:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program:
//Student Name (Mandatory)

Output: //Student Name (Mandatory)

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No.1(a)

Aim:Write two programs on datatypes in java.

class Main {

public static void main(String[] args) {

int range = -4250000;

System.out.println("\nHIMANI NAMDEO ");

System.out.println("0827CI221061");

System.out.println(range); // print -4250000

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No.1(b)

Aim:Two Programs on Data Types in Java


public class Main {

public static void main(String[] args) {

long l = 1000000000L;

float f = 3.14f;

double d = 3.141592653589793;

char c = 'A';

boolean b = true;

System.out.println("HIMANI NAMDEO\n0827CI221061");

System.out.println("Long Value: " + l);

System.out.println("Float Value: " + f);

System.out.println("Double Value: " + d);

System.out.println("Char Value: " + c);

System.out.println("Boolean Value: " + b);

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 2(a)

Aim:Two Programs on Type Conversion in Java

public class WideningTypeCastingExample

public static void main(String[] args)

int x = 7;

long y = x;

float z = y;

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

System.out.println("\nBefore conversion, int value "+x);

System.out.println("After conversion, long value "+y);

System.out.println("After conversion, float value "+z);

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 2(b)

Aim:Two Programs on Type Conversion in Java


public class NarrowingTypeCastingExample

public static void main(String args[])

double d = 166.66;

long l = (long)d;

int i = (int)l;

System.out.println("\nHIMANI NAMDEO");

System.out.println("\n 0827CI221061");

System.out.println("\n Before conversion: "+d);

System.out.println("\n After conversion into long type: "+l);

System.out.println("\n After conversion into int type: "+i);

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 3(a)

Aim:Two Programs on Classes and Objects in Java


public class Studentsrecords

public String name;

public Studentsrecords(String sname)

name = sname;

public void printstud()

System.out.println("Student Name: " + name );

public static void main(String args[])

{System.out.println("\nHIMANI NAMDEO");

System. out. println("0827CI221061");

Studentsrecords s = new Studentsrecords("AKSHAT TIWARI");

s.printstud();

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 3(b)

Aim:Two Programs on Classes and Objects in Java

class Student{

int id;

String name;

class TestStudent2{

public static void main(String args[]){

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

Student s1=new Student();

s1.id=27;

s1.name="Anuj";

System.out.println(s1.id+" "+s1.name);//printing members with a white space

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 4(a)

Aim:Two Programs on Static Members in Java (Method and Variable)


class VariableDemo

static int count=0;

public void increment()

count++;

public static void main(String args[])

VariableDemo obj1=new VariableDemo();

VariableDemo obj2=new VariableDemo();

obj1.increment();

obj2.increment();

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

System.out.println("Obj1: count is="+obj1.count);

System.out.println("Obj2: count is="+obj2.count);

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 4(b)

Aim:Two Programs on Static Members in Java (Method and Variable)


class StaticTest {

static int add(int a, int b){

return a + b;

public class Main {

public static void main(String[] args) {

System.out.println("HIMANI NAMDEO");

System.out.println("0827CI221061");

StaticTest st = new StaticTest();

System.out.println(" 2 + 3 = " + StaticTest.add(2, 3));

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 5(a)

Aim: Two Programs on Wrapper Classes in Java


public class WrapperExample1

public static void main(String[] args) {

int a=20;

char b='a';

boolean d=true;

byte e=10;

short f=20;

long l=40;

float fl=50.0F;

double dl=60;

Integer i=Integer.valueOf(a);

Integer j=a;

System.out.println(a+" "+i+" "+j);

Character c=b;

Boolean g=d;

Byte h=e;

Short k=f;

Long m=l;

Float n=fl;

Double o=dl;

CSIT Department Java Programming Lab Manual Session July-Dec2023


System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

System.out.println(b+" "+c);

System.out.println(d+" "+g);

System.out.println(e+" "+h);

System.out.println(f+" "+k);

System.out.println(l+" "+m);

System.out.println(fl+" "+n);

System.out.println(dl+" "+o);

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 5(b)

Aim: Two Programs on Wrapper Classes in Java


public class WrapperExample2

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

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

Integer i=new Integer(3);

int a=i.intValue();

int b=i;

System.out.println(a+" "+i+" "+b);

Character ch=new Character('a');

char c=ch;

Boolean bl=new Boolean(true);

boolean d=bl;

Long l=new Long(40);

long g=l;

Float fl= new Float(50.0F);

float h=fl;

Double dl=new Double(60);

double j=dl;

System.out.println(ch+" "+c);

System.out.println(d+" "+bl);

System.out.println(l+" "+g);
CSIT Department Java Programming Lab Manual Session July-Dec2023
System.out.println(fl+" "+h);

System.out.println(dl+" "+j); }}

Program No. 6(a)

Aim: Two Programs on Command Line Arguments in Java


class Main {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

System.out.println("Command-Line arguments");

for(String str: args) {

System.out.println(str);

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 6(b)

Aim: Two Programs on Command Line Arguments in Java

public class CommandLineArgumentsExample {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

if (args.length > 0) {

System.out.println("Command line arguments:");

for (String arg : args) {

System.out.println(arg);

} else {

System.out.println("No command line arguments provided.");

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 7(a)

Aim: Two Programs on importing and making Packages in Java


import java.util.Scanner;

public class Main

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

Scanner sc=new Scanner(System.in);

System.out.println("Enter a number:");

int a=sc.nextInt();

if(a%2==0)

System.out.println("Even Number");

else

System.out.println("Odd Number");

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 7(b)

Aim: Two Programs on importing and making Packages in Java


public class Main

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

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

Scanner Sc=new Scanner(System.in);

System.out.println("Enter first number:");

int a=sc.nextInt();

System.out.println("Enter second number:");

int b=sc.nextInt();

int c;

c=a;

a=b;

b=c;

System.out.println("After Swapping :");

System.out.println("First no. is-"+a);

System.out.println("Second no. is-"+b);

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 8(a)

Aim: Two Programs on Access Modifiers in Java


class Animal {

// protected method

protected void display() {

System.out.println("dog is an animal");

class Dog extends Animal {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

// create an object of Dog class

Dog dog = new Dog();

// access protected method

dog.display();

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 8(b)

Aim: Two Programs on Access Modifiers in Java


class Animal

public int legCount;

protected void display()

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

System.out.println("i am an animal");

System.out.println("i have"+legCount+"legs");

public class Main{

public static void main(String[] args) {

Animal animal=new Animal();

animal.legCount=4;

animal.display();

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 9(a)

Aim: Two Programs on Constructor in Java


class Main {

private String name;

Main() {

System.out.println("\nHIMANI NAMDEOI");

System.out.println("0827CI221061");

System.out.println("Constructor Called:");

name = "Programiz";

public static void main(String[] args) {

Main obj = new Main();

System.out.println("The name is " + obj.name);

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 9(b)

Aim: Two Programs on Constructor in Java


class Main {

String languages;

Main(String lang) {

languages = lang;

System.out.println(languages + " Programming Language");

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEOI");

System.out.println("0827CI221061");

Main obj1 = new Main("Java");

Main obj2 = new Main("Python");

Main obj3 = new Main("C");

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 10(a)

Aim: Two Programs on Inheritance in Java


class Animal {

String name;

public void eat() {

System.out.println("I can cook and eat");

class Dog extends Animal {

public void display() {

System.out.println("My name is " + name);

class Main {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

Dog labrador = new Dog();

CSIT Department Java Programming Lab Manual Session July-Dec2023


labrador.name = "Anuj";

labrador.display();

labrador.eat();

} }

Program No. 10(b)

Aim: Two Programs on Inheritance in Java


class Animal {

public void eat() {

System.out.println("I can eat");

class Dog extends Animal {

@Override

public void eat() {

System.out.println("I eat dog food");

public void bark() {

System.out.println("I can bark");

class Main {

public static void main(String[] args) {

CSIT Department Java Programming Lab Manual Session July-Dec2023


System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

Dog labrador = new Dog();

labrador.eat();

labrador.bark();

Output:

Program No. 11(a)

Aim: Two Programs on Overloading and Overriding in Java


public class Ovlx {

public int add(int a, int b) {

return a + b;

public int add(int a, int b, int c) {

return a + b + c;

public double add(double a, double b) {

return a + b;

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEOI");

System.out.println("0827CI221061");

Ovlx example = new Ovlx();

System.out.println(example.add(2, 3)); // Calls the first add method

System.out.println(example.add(2, 3, 4)); // Calls the second add method

System.out.println(example.add(2.5, 3.5)); // Calls the third add method

CSIT Department Java Programming Lab Manual Session July-Dec2023


Output:

Program No. 11(b)

Aim: Two Programs on Overloading and Overriding in Java


class Animal {

public void makeSound() {

System.out.println("Generic Animal Sound");

class Dog extends Animal {

public void makeSound() {

System.out.println("Bark");

class Cat extends Animal {

public void makeSound() {

System.out.println("Meow");

public class Ovlx {

CSIT Department Java Programming Lab Manual Session July-Dec2023


public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

Animal dog = new Dog();

Animal cat = new Cat();

dog.makeSound(); // Calls Dog's overridden makeSound method

cat.makeSound(); // Calls Cat's overridden makeSound method

Program No. 12(a)

Aim: Two Programs on final keyword in Java


class Bike10{

final int speedlimit;

Bike10(){

speedlimit=70;

System.out.println(speedlimit);

public static void main(String args[]){

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

new Bike10();

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 12(b)

Aim: Two Programs on final keyword in Java


public class Fex {

final int constantValue = 10;

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

Fex obj = new Fex();

// obj.constantValue = 20; // Error: Cannot assign a value to a final variable

System.out.println(obj.constantValue); // Outputs 10

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 13(a)

Aim: Two Programs on this keyword in Java


public class Student {

private String name;

private int age;

public void setDetails(String name, int age) {

this.name = name; // Using 'this' to refer to the instance variable

this.age = age;

public void displayDetails() {

System.out.println("Name: " + this.name + ", Age: " + this.age);

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEOI");

System.out.println("0827CI221061");

Student student1 = new Student();

student1.setDetails("John", 20);

student1.displayDetails();
CSIT Department Java Programming Lab Manual Session July-Dec2023
Student student2 = new Student();

student2.setDetails("Alice", 22);

student2.displayDetails();

} }

Output:

Program No. 13(b)

Aim: Two Programs on this keyword in Java


public class Rectangle {

private int length;

private int width;

public Rectangle(int length, int width) {

this.length = length;

this.width = width;

public Rectangle(int side) {

this(side, side); }

public void displayArea() {

int area = this.length * this.width;

System.out.println("Area: " + area);

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

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

Rectangle rectangle1 = new Rectangle(5, 3);

CSIT Department Java Programming Lab Manual Session July-Dec2023


rectangle1.displayArea();

Rectangle square = new Rectangle(4);

square.displayArea();

} }

Output:

Program No. 14(a)

Aim: Two Programs on super keyword in Java


class Vehicle {

void start() {

System.out.println("Vehicle started");

} class Car extends Vehicle {

void start() {

super.start(); // Calling superclass method using 'super'

System.out.println("Car started");

public class Supex {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

Car myCar = new Car();

myCar.start();

CSIT Department Java Programming Lab Manual Session July-Dec2023


}

Output:

Program No. 14(b)

Aim: Two Programs on super keyword in Java


class Animal {

String sound;

Animal(String sound) {

this.sound = sound;

void makeSound() {

System.out.println("Animal makes sound: " + sound);

class Dog extends Animal {

String breed;

Dog(String sound, String breed) {

super(sound); // Calling superclass constructor using 'super'

this.breed = breed;

void displayDetails() {

System.out.println("Breed: " + breed);

makeSound(); // Calling superclass method using 'super'

CSIT Department Java Programming Lab Manual Session July-Dec2023


}

} public class Supex {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEOI");

System.out.println("0827CI221061");

Dog myDog = new Dog("Woof", "Labrador");

myDog.displayDetails();

Program No. 15(a)

Aim: Two Programs on static members in inheritance in java


class Parent {

static void staticMethod() {

System.out.println("Static method in Parent class");

class Child extends Parent {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEOI");

System.out.println("0827CI221061");

// Calling static method from Parent class

Parent.staticMethod();

// Accessing static method directly from Child class

staticMethod();

CSIT Department Java Programming Lab Manual Session July-Dec2023


Output:

Program No. 15(b)

Aim: Two Programs on static members in inheritance in java


class Parent {

static int staticVariable = 10;

class Child extends Parent {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

// Accessing static variable from Parent class

System.out.println("Static variable from Parent: " + Parent.staticVariable);

// Accessing static variable directly from Child class

System.out.println("Static variable from Child: " + staticVariable);

// Modifying static variable in Child class

staticVariable = 20;

// Checking the updated static variable from Parent class

System.out.println("Updated static variable from Parent: " + Parent.staticVariable);

CSIT Department Java Programming Lab Manual Session July-Dec2023


}

Output:

Program No. 16(a)

Aim: Two Programs on Constructor in inheritance in Java


class Parent {

Parent() {

System.out.println("Default constructor of Parent class");

class Child extends Parent {

Child() {

System.out.println("Default constructor of Child class");

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

// Creating an object of Child class

CSIT Department Java Programming Lab Manual Session July-Dec2023


Child childObj = new Child();

Output:

Program No. 16(b)

Aim: Two Programs on Constructor in inheritance in Java


class Parent {

int parentVar;

Parent(int value) {

this.parentVar = value;

System.out.println("Parameterized constructor of Parent class with value: " + value);

class Child extends Parent {

int childVar;

Child(int parentValue, int childValue) {

super(parentValue); // Calling parameterized constructor of Parent class

this.childVar = childValue;

System.out.println("Parameterized constructor of Child class with values: " + parentValue + ", " +
childValue);

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");
CSIT Department Java Programming Lab Manual Session July-Dec2023
System.out.println("0827CI221061");

Child childObj = new Child(10, 20);

}}

Output:

Program No. 18(a)

Aim: Two Programs on Abstract class in Java


abstract class Bike{

abstract void run();

class Honda4 extends Bike{

void run(){System.out.println("running safely");}

public static void main(String args[]){

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

Bike obj = new Honda4();

obj.run();

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 18(b)

Aim: Two Programs on Abstract class in Java


abstract class Bank{

abstract int getRateOfInterest();

class SBI extends Bank{

int getRateOfInterest(){return 7;}

class PNB extends Bank{

int getRateOfInterest(){return 8;}

class TestBank{

public static void main(String args[]){

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

Bank b;

b=new SBI();

System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");

b=new PNB();

System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");

CSIT Department Java Programming Lab Manual Session July-Dec2023


}}

Output:

Program No. 19(a)

Aim: Two Programs on Interface in Java


interface printable{

void print();

class A implements printable{

public void print(){System.out.println("Hello");}

public static void main(String args[]){

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

A obj = new A();

obj.print();

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 19(b)

Aim: Two Programs on Interface in Java


// create an interface

interface Language {

void getName(String name);

// class implements interface

class ProgrammingLanguage implements Language {

// implementation of abstract method

public void getName(String name) {

System.out.println("Programming Language: " + name);

class Main {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

ProgrammingLanguage language = new ProgrammingLanguage();

language.getName("Java");

}}

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 20(a)

Aim: Two Programs on Difference between Abstract class and Interface in Java
interface A{

void a();//bydefault, public and abstract

void b();

void c();

void d();

abstract class B implements A{

public void c(){System.out.println("I am Chinmay");}

} class M extends B{

public void a(){System.out.println("I am Ajay");}

public void b(){System.out.println("I am Beena");}

public void d(){System.out.println("I am Dev");}

class Test5{

public static void main(String args[]){

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");A a=new M();

a.a();

a.b();

CSIT Department Java Programming Lab Manual Session July-Dec2023


a.c(); a.d(); }}

Output:

Program No. 20(b)

Aim: Two Programs on Difference between Abstract class and Interface in Java
//same logic using abstract class

abstract class Shape {

abstract void draw();

void display() {

System.out.println("Displaying the shape.");

}class Circle extends Shape {

void draw() {

System.out.println("Drawing a circle.");

}}

public class Main {

public static void main(String[] args) {

System.out.println("HIMANI NAMDEO\n0827CI221061");

Circle circle = new Circle();

circle.draw();

circle.display();

CSIT Department Java Programming Lab Manual Session July-Dec2023


Output:

Program No. 21(a)

Aim: Two Programs on Arrays in Java


public class Arrex {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

// Declare and initialize an array

int[] numbers = {1, 2, 3, 4, 5};

// Print the elements of the array

System.out.println("Elements of the array:");

for (int i = 0; i < numbers.length; i++) {

System.out.println(numbers[i]);

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 21(b)

Aim: Two Programs on Arrays in Java


public class ArrSum {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

// Declare and initialize an array

int[] numbers = {1, 2, 3, 4, 5};

// Calculate the sum of elements in the array

int sum = 0;

for (int i = 0; i < numbers.length; i++) {

sum += numbers[i];

// Print the sum

System.out.println("Sum of elements: " + sum);

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 22(a)

Aim: Two Programs on Two Dimensional Arrays in Java


public class TwoD {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

// Declare and initialize a 2D array

int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

// Print the elements of the 2D array

System.out.println("Elements of the 2D array:");

for (int i = 0; i < matrix.length; i++) {

for (int j = 0; j < matrix[i].length; j++) {

System.out.print(matrix[i][j] + " ");

System.out.println(); // Move to the next line after each row

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 22(b)

Aim: Two Programs on Two Dimensional Arrays in Java


public class TwoD{

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

// Declare and initialize a 2D array

int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

// Calculate the sum of elements in the 2D array

int sum = 0;

for (int i = 0; i < matrix.length; i++) {

for (int j = 0; j < matrix[i].length; j++) {

sum += matrix[i][j];

// Print the sum

System.out.println("Sum of elements in the 2D array: " + sum);

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 23(a)

Aim: Two Programs on String Class in Java


public class Strconc {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

// Concatenate two strings

String firstName = "John";

String lastName = "Doe";

String fullName = firstName + " " + lastName;

// Print the concatenated string

System.out.println("Full Name: " + fullName);

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 23(b)

Aim: Two Programs on String Class in Java


public class Strop {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

// Calculate the length of a string

String message = "Hello, World!";

int length = message.length();

// Print the length of the string

System.out.println("Length of the string: " + length);

// Extract a substring

String substring = message.substring(7, 12);

CSIT Department Java Programming Lab Manual Session July-Dec2023


// Print the extracted substring

System.out.println("Substring: " + substring);

Output:

Program No. 24(a)

Aim: Two Programs on Exception Handling in Java


class Main {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

try {

// code that generate exception

int divideByZero = 5 / 0;

System.out.println("Rest of code in try block");

catch (ArithmeticException e) {

System.out.println("ArithmeticException => " + e.getMessage());

CSIT Department Java Programming Lab Manual Session July-Dec2023


Output:

Program No. 24(b)

Aim: Two Programs on Exception Handling in Java


class Main {

public static void main(String[] args) {

System.out.println("\nAKSHAT TIWARI");

System.out.println("0827CI221015");

try {

// code that generates exception

int divideByZero = 5 / 0;

catch (ArithmeticException e) {

System.out.println("ArithmeticException => " + e.getMessage());

finally {

System.out.println("This is the finally block");

CSIT Department Java Programming Lab Manual Session July-Dec2023


}

Output:

Program No. 25(a)

Aim: Two Programs on Unchecked Exception Handling in Java


public class NullPointerExceptionExample {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

try {

// Code that may cause NullPointerException

String str = null;

int length = str.length(); // This line will throw NullPointerException

System.out.println("Length: " + length);

} catch (NullPointerException e) {

// Handle NullPointerException

System.out.println("Error: " + e.getMessage());

Output:
CSIT Department Java Programming Lab Manual Session July-Dec2023
Program No. 25(b)

Aim: Two Programs on Unchecked Exception Handling in Java


public class ArrayStoreExceptionExample {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

try {

// Code that may cause ArrayStoreException

Object[] objArray = new Integer[5];

objArray[0] = "Hello"; // This line will throw ArrayStoreException

} catch (ArrayStoreException e) {

// Handle ArrayStoreException

System.out.println("Error: " + e.getMessage());

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 26(a)

Aim: Two Programs on Use of throws in checked exception in Java


import java.io.IOException;

class Testthrows1{

void m()throws IOException{

throw new IOException("device error");

void n()throws IOException{

m();

} void p(){

try{

n();

}catch(Exception e){System.out.println("exception handled");}

public static void main(String args[]){

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

CSIT Department Java Programming Lab Manual Session July-Dec2023


Testthrows1 obj=new Testthrows1();

obj.p();

System.out.println("normal flow...");

Output:

Program No. 26(b)

Aim: Two Programs on Use of throws in checked exception in Java


import java.io.*;

class M{

void method()throws IOException{

System.out.println("device operation performed");

class Testthrows3{

public static void main(String args[])throws IOException{

System.out.println("\nHIMANI ANMDEO");

System.out.println("0827CI221061");

M m=new M();

m.method();

System.out.println("normal flow...");

CSIT Department Java Programming Lab Manual Session July-Dec2023


}

Output:

Program No. 27(a)

Aim: Two Programs on Introduction to Threads in Java


class Multi extends Thread{

public void run(){

System.out.println("thread is running...");

public static void main(String args[]){

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

Multi t1=new Multi();

t1.start();

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 27(b)

Aim: Two Programs on Introduction to Threads in Java


public class MyThread1

{ public static void main(String argvs[])

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

Thread t=new Thread("My first thread");

t.start();

String str = t.getName();

System.out.println(str);

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 28(a)

Aim: Two Programs on Threads using Runnable Interface in Java


class Multi3 implements Runnable{

public void run(){

System.out.println("thread is running..."); }

public static void main(String args[]){

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

Multi3 m1=new Multi3();

Thread t1 =new Thread(m1); t1.start(); } }

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program No. 28(b)

Aim: Two Programs on Threads using Runnable Interface in Java


public class M implements Runnable

public void run()

System.out.println("Now the thread is running ...");

public static void main(String args[]) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

Runnable r1 = new M();

Thread th1 = new Thread(r1, "My new thread");

th1.start();

CSIT Department Java Programming Lab Manual Session July-Dec2023


String str = th1.getName();

System.out.println(str);

Output:

Program No. 29(a)

Aim: Two Programs on Threads using Thread Class in Java


import java.io.*;

import java.util.*;

public class ABC extends Thread {

public void run() {

System.out.println("Thread Started Running...");

public static void main(String[] args)

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

ABC g1 = new ABC();

g1. start();

CSIT Department Java Programming Lab Manual Session July-Dec2023


}

Output:

Program No. 29(b)

Aim: Two Programs on Threads using Thread Class in Java


class TestSleepMethod1 extends Thread{

public void run(){

for(int i=1;i<5;i++){

try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}

System.out.println(i);

public static void main(String args[]){

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

TestSleepMethod1 t1=new TestSleepMethod1();

TestSleepMethod1 t2=new TestSleepMethod1();

CSIT Department Java Programming Lab Manual Session July-Dec2023


t1.start();

t2.start();

Output:

Program No. 30(a)

Aim: Two Programs on Introduction to file handling in Java


import java.io.File;

import java.io.IOException;

class CreateFile {

public static void main(String args[]) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061\n");

try {

File f0 = new File("D:FileOperationExample.txt");

if (f0.createNewFile()) {

System.out.println("File " + f0.getName() + " is created successfully.");

} else {

System.out.println("File is already exist in the directory.");


CSIT Department Java Programming Lab Manual Session July-Dec2023
}

} catch (IOException exception) {

System.out.println("An unexpected error is occurred.");

exception.printStackTrace();

Output:

Program No. 30(b)

Aim: Two Programs on Introduction to file handling in Java


import java.io.File;

class FileInfo {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

File f0 = new File("D:FileOperationExample.txt");

if (f0.exists()) {

System.out.println("The name of the file is: " + f0.getName());

System.out.println("The absolute path of the file is: " + f0.getAbsolutePath());

System.out.println("Is file writeable?: " + f0.canWrite());

CSIT Department Java Programming Lab Manual Session July-Dec2023


System.out.println("Is file readable " + f0.canRead());

System.out.println("The size of the file in bytes is: " + f0.length());

} else {

System.out.println("The file does not exist.");

Output:

Program No. 31(a)

Aim: Two Programs on File class in Java


import java.io.File;

import java.io.IOException;

public class CreateFileExample {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

// Specify the file path

String filePath = "example.txt";

try {

// Create a File object

File file = new File(filePath);

CSIT Department Java Programming Lab Manual Session July-Dec2023


if (file.createNewFile()) {

System.out.println("File created: " + file.getName());

} else {

System.out.println("File already exists.");

} catch (IOException e) {

System.out.println("An error occurred: " + e.getMessage());

}}

Output:

Program No. 31(b)

Aim: Two Programs on File class in Java


import java.io.File;

import java.io.IOException;

public class FileExample {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

String filePath = "example.txt";

File file = new File(filePath); try {

if (file.exists()) {

System.out.println("File already exists.");

CSIT Department Java Programming Lab Manual Session July-Dec2023


} else {

if (file.createNewFile()) {

System.out.println("File created successfully.");

} else {

System.out.println("Failed to create the file.") } // Display file information

System.out.println("File Name: " + file.getName());

System.out.println("Absolute Path: " + file.getAbsolutePath());

System.out.println("File Size: " + file.length() + " bytes");

} catch (IOException e) {

System.out.println("An error occurred: " + e.getMessage());

}}

Program No. 32(a)

Aim: Two Programs on Writing to file using FileOutputStream in java


import java.io.FileOutputStream;

import java.io.IOException;

public class BinaryFileWriter {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061"); // Specify the file path

String filePath = "binaryFile.bin";

try (FileOutputStream fos = new FileOutputStream(filePath)) // Binary data (in this case, an array of bytes)

byte[] binaryData = { 0x48, 0x65, 0x6C, 0x6C, 0x6F }; // Hello in ASCII

// Write binary data to the file

fos.write(binaryData)

CSIT Department Java Programming Lab Manual Session July-Dec2023


System.out.println("Binary data written to file successfully.");

} catch (IOException e) {

System.out.println("An error occurred: " + e.getMessage());

Output:

Program No. 32(b)

Aim: Two Programs on Writing to file using FileOutputStream in java


import java.io.FileOutputStream;

import java.io.IOException;

public class TextFileWriter {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

// Specify the file path

String filePath = "textFile.txt";

try (FileOutputStream fos = new FileOutputStream(filePath)) {

// Text to be written to the file

CSIT Department Java Programming Lab Manual Session July-Dec2023


String textToWrite = "Hello, this is a text file example.";

// Convert the string to bytes and write to the file

fos.write(textToWrite.getBytes());

System.out.println("Text written to file successfully. } catch (IOException e) {

System.out.println("An error occurred: " + e.getMessage());

}}

Output:

Program No. 33(a)

Aim: Two Programs on Reading from file using FileInputStream in Java


import java.io.FileInputStream;

public class Main {

public static void main(String args[]) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

try {

FileInputStream input = new FileInputStream("input.txt");

System.out.println("Data in the file: ");

int i = input.read();

while(i != -1) {

System.out.print((char)i);

CSIT Department Java Programming Lab Manual Session July-Dec2023


i = input.read();

input.close();

catch(Exception e) {

e.getStackTrace();

Output:

Program No. 33(b)

Aim: Two Programs on Reading from file using FileInputStream in Java


import java.io.File;

import java.io.FileInputStream;

public class abc {

public static void main(String[] args) {

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

File file = new File("file.txt");

try { FileInputStream input= new FileInputStream(file);

int character;

while (input.available()!=0) {

character = input.read();

CSIT Department Java Programming Lab Manual Session July-Dec2023


System.out.print((char)character);

} input.close();

catch (Exception e) {

e.printStackTrace();

}}}

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program no.34

Aim: One Programs on Garbage Collection in Java


class Employee {

private int ID;

private String name;

private int age;

private static int nextId = 1;

public Employee(String name, int age)

{ this.name = name;

this.age = age;

this.ID = nextId++;

public void show()

{ System.out.println("Id=" + ID + "\nName=" + name+ "\nAge=" + age);

public void showNextId()

{ System.out.println("Next employee id will be= "+ nextId);

class UseEmployee {

public static void main(String[] args)

System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

CSIT Department Java Programming Lab Manual Session July-Dec2023


Employee E = new Employee("GFG1", 56);

Employee F = new Employee("GFG2", 45);

Employee G = new Employee("GFG3", 25);

E.show();

F.show();

G.show();

E.showNextId();

F.showNextId();

G.showNextId();

{ Employee X = new Employee("GFG4", 23);

Employee Y = new Employee("GFG5", 21);

X.show();

Y.show();

X.showNextId();

Y.showNextId();

E.showNextId();

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program no. 35

Aim: One Programs on Finalize method in Java


import java.lang.*;

class prep {

protected void finalize() throws Throwable

{ try

{ System.out.println("inside prep finalize()");

catch (Throwable e) {

throw e;

finally {

System.out.println("Calling finalize method”+ " of the Object class");

super.finalize();

public static void main(String[] args) throws Throwable

{ prep d = new prep();

d.finalize();

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program no.36 (a)

Aim: Two Programs on Introduction to Applet in Java.

import java.applet.Applet;

import java.awt.Graphics;

public class HelloWorld extends Applet

// Overriding paint() method

@Override

public void paint(Graphics g)

g.drawString("Hello World", 20, 20);

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program no.36 (b)

Aim: Two Programs on Introduction to Applet in Java.

import java.applet.Applet;

import java.awt.Graphics;

public class HelloWorld extends Applet

@Override

public void paint(Graphics g)

{ g.drawString("Hello World", 20, 20);

Output:

CSIT Department Java Programming Lab Manual Session July-Dec2023


Program no.37

Aim: One Program on Life Cycle of an Applet in Java

Import java.util.Scanner;

Class AppletLifeCycle extends Applet

{ public void init()

{ System.out.println("\nHIMANI NAMDEO");

System.out.println("0827CI221061");

public void start()

public void paint(Graphics graphics)

public void stop()

public void destroy()

CSIT Department Java Programming Lab Manual Session July-Dec2023


CSIT Department Java Programming Lab Manual Session July-Dec2023

You might also like