[go: up one dir, main page]

0% found this document useful (0 votes)
39 views26 pages

Java LabMANUAL 2023-24 Without Op

The document is a laboratory manual for the Object Oriented Programming Using Java course at Brindavan College of Engineering for the academic year 2024-2025. It includes guidelines for laboratory conduct, a preface explaining the manual's purpose, software installation instructions, and various Java programming exercises. The document is prepared by Kavya K S and aims to provide comprehensive guidance for students learning Java programming.

Uploaded by

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

Java LabMANUAL 2023-24 Without Op

The document is a laboratory manual for the Object Oriented Programming Using Java course at Brindavan College of Engineering for the academic year 2024-2025. It includes guidelines for laboratory conduct, a preface explaining the manual's purpose, software installation instructions, and various Java programming exercises. The document is prepared by Kavya K S and aims to provide comprehensive guidance for students learning Java programming.

Uploaded by

shashank bhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Brindavan College Of Engineering Department of MCA

Brindavan College of
Engineering
Dwarakanagar, Bagalur Main Road, Yelahanka, Bengaluru – 560063
Affiliated to VTU Belagavi, Approved by AICTE, New Delhi, India
Accredited ‘B++’ level by NAAC www.brindavancollege.com

II Semester

Object Oriented Programming Using Java Laboratory


MMCL206

ACADEMIC YEAR
2024 – 20245

LABORATORY MANUAL

PREPARED BY
Kavya K S

1|Page Java Lab


Brindavan College Of Engineering Department of MCA

Brindavan College of Engineering


Department of Computer Applications

II Semester

Object Oriented Programming Using Java Laboratory


MMCL206
ACADEMIC YEAR
2024 – 2025

NAM E OF THE STUDENT :

UNIVERSITY SEAT NO. :

BATCH :

PREPARED BY
Kavya K S

2|Page
Brindavan College Of Engineering Department of MCA

Brindavan College of Engineering


Department of Computer Applications

LABORATORY CERTIFICATE
This is to certify that Mr. /Ms.__________________________________________ bearing
USN_________________ of __________semester and ________section has satisfactorily
completed the course of experiments in Object Oriented Programming Using Java
Laboratory, code MMCL206 prescribed by the Visvesvaraya Technological University,
Belagavi of this Institute for the academic year 2024 – 2025 .

MARKS

Maximum Marks Marks Obtained

Signature of Faculty-In-Charge Head of the Department

Date:

3|Page
Brindavan College Of Engineering Department of MCA

Brindavan College of Engineering


Dwarakanagar, Bagalur Main Road, Yelahanka, Bengaluru – 560063
Affiliated to VTU Belagavi, Approved by AICTE, New Delhi, India
Accredited B++ level by NAAC

Department of Computer Applications

DEPARTMENT VISION

To inculcate the engineering professionalism along with Universal Human


Values among the students to meet the needs of the global scenario.

DEPARTMENT MISSION

• To achieve academic excellence in Computer Applications through the state


of the art learning environment.

• Nurturing the spirit of Innovation, Research and Development among Faculty


and Students by creating opportunities to interact with personnel in various
academic and industry.

• Motivating students with value based skills and ethics to make them
responsible citizens in extending their technical skills and knowledge to meet
the needs of the society.

4|Page
Brindavan College Of Engineering Department of MCA

Brindavan College
of Engineering
Dwarakanagar, Bagalur Main Road, Yelahanka, Bengaluru – 560063
Affiliated to VTU Belagavi, Approved by AICTE, New Delhi, India
Accredited B++ level by NAAC

Department of Computer Applications

DOs & DON’Ts IN LABORATORY

DOs
 Be on time and students should carry observation and completed records in all
aspects.
 Dress code & wearing ID card is compulsory.
 Electronic gadgets are not allowed inside the lab.
 Students should be at their concerned desktop.
 After execution the students should get it verified by the concerned faculty.
 The executed results should be noted in their observations and get it verified by the
concerned faculty.
 Students must ensure that all the switches are in the OFF position; desktop is
shutdown properly after completion of the assignments.

DON’Ts

 Do not come late to lab.


 Do not touch server computer.
 Do not leave the lab without the permission of the faculty in-charge.
 Do not wear footwear and enter the lab(except power electronics lab).
 Do not insert pen drive/memory card to any computer in the lab.
 Do not upload, delete or alter any software files.
 Don’t bring any eatable things to the lab.

5|Page
Brindavan College Of Engineering Department of MCA

PREFACE

The indispensable intention for writing the manual for Object Oriented Programming using
JAVA is to impart inclusive guidance for the students to learn object oriented programming
technique for developing software using JAVA. The inspiration at the rear is to spotlight on the
practical phase of programming. To implement this objective it has been included couple of
examples regarding each topic.

The manual provides complete programming guide for the students from beginning to some
advanced level. The language is simple and stress-free to understand.

This manual is basically intended for the students but it can also be used by any person who
wants to get the knowledge of Object Oriented Programming

Kavya K S

6|Page
Brindavan College Of Engineering Department of MCA

Software requirement and Installation Procedure

Here We are Using Software java development kit 1.8 version(JDK 1.8 version)

Install JDK on Microsoft Windows

Step 1: Download and Install Java Development Kit (JDK)

The very first step is to download the Oracle Java Development Kit (JDK) from the Official
Oracle Website. For that, Head over to the Official Website.

Step 1 - Downloading JDK for Windows

You need to identify your system specifications to choose the Product/file description. The
website will contain the latest version for your corresponding system. For Windows, we’ll be
downloading the latest x64 Installer of Java SE Development Kit 18. After the download is
complete, proceed to install the JDK by following the bootstrapped steps.

Step 2: Configure Environment Variables

7|Page
Brindavan College Of Engineering Department of MCA

After the installation is complete, we have to configure environment variables to notify the
system about the directory in which JDK files are located. Proceed to C:\Program
Files\Java\jdk-{YOUR_JDK_VERSION}\bin (replace {-} with your JDK version)

Step 2 - Locating JDK File

To set the Environment Variables, you need to search Environment Variables in the Task Bar
and click on “Edit the system environment variables”.

Step 2 - Configuring Environment Variables

Under the Advanced section, Click on “Environment Variables”.

8|Page
Brindavan College Of Engineering Department of MCA

Step 2 - Setting Environment Variable

Under System variables, select the “Path” variable and click on “Edit”. Click on “New” then
paste the Path Address i.e. C:\Program Files\Java\jdk-{YOUR_JDK_VERSION}\bin. Click
on “OK”.

Now, in the Environment Variables dialogue, under System variables, click on “New” and
then under Variable name: JAVA_HOME and Variable value: paste address i.e. C:\Program
Files\Java\jdk-{YOUR_JDK_VERSION}. Click on OK => OK => OK.

9|Page
Brindavan College Of Engineering Department of MCA

Step 3: Check the Java Version

Open Command Prompt and enter the following commands

java -version javac

-version

Execution Procedure :

• Write the Program in any text editor and save the with filename.java
• Open command prompt And change the directory to folder where you save the
program
• For compilation-type javac filename.java
• For Run-java mainclass_name

10 | P a g e
Brindavan College Of Engineering Department of MCA

JAVA PROGRAMMING LAB


1. Write a Java program to print the following triangle of a numbers
1
12
123
1234
12345

2. Write a Java program to list the factorial of the numbers 1 to 10. To calculate the factorial
value, use while loop.(Hint Fact of 4 = 4*3*2*1)

3. Write a Java program


a) To find the area and circumference of the circle by accepting the radius from the user.
b) To accept a number and find whether the number is Prime or not.

4. Write a Java program to demonstrate a division by zero exception

5. Write a Java program to implement Inner class and demonstrate its Access protection.

6. Write a Java program to demonstrate Constructor Overloading and Method Overloading.

7. Write a JAVA program to demonstrate Inheritance. Simple Program on Java for the
implementation of Multiple inheritance using interfaces to calculate the area of a rectangle
and triangle.

8. Write a Java applet program, which handles keyboard event.

Demonstration Experiments ( For CIE ) if any

9. Write a Java Program to create a window we press


M or m the window displays Good Morning
A or a the window displays Good After Noon
E or e the window displays Good Evening
N or n the window displays Good Night
10. Write a Java program to implement a Queue using user defined Exception Handling (also
make use of throw, throws). a. Complete the following: b. Create a package named shape.
c. Create some classes in the package representing some common shapes like Square,
Triangle, and Circle. d. Import and compile these classes in other program.

11 | P a g e
Brindavan College Of Engineering Department of MCA

1. Write a Java program to print the following triangle of a numbers 1


12
123
1234
12345

import java.util.*; class


pattern {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows to be printed");
int rows = sc.nextInt();

// loop to iterate for the given number of rows


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

// loop to print the number of spaces before the star


for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.print("\n");

}
}
}
Execution Procedure:
• Open command prompt And change the directory to bin
folder
• For compilation type javac Pattern.java
• java Pattern.java

12 | P a g e
Brindavan College Of Engineering Department of MCA

2. Write a Java program to list the factorial of the numbers 1 to 10. To calculate the
factorial value, use while loop.(Hint Fact of 4 = 4*3*2*1)

import java.util.*;
import java.io.*;

class Factorial
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the number of factorial");
int num=sc.nextInt();
int i=1; int
fact=1; while(i<=num)
{
fact=fact*i;
System.out.println("the factorial of"+i+"="+fact);
i++;
}

}
}

Execution Procedure:

• Open command prompt And change the directory to bin folder


• type javac Factorial.java
• java Factorial.java

13 | P a g e
Brindavan College Of Engineering Department of MCA

3.Write a Java program


a. To find the area and circumference of the circle by accepting the radius
from the user.
b. To accept a number and find whether the number is Prime or not.

a.To find the area and circumference of the circle by accepting the radius
from the user. import java.util.*; import java.io.*;

class Circle
{
public static void main(String args[])
{
double radius,area,circumference;
Scanner sc=new Scanner(System.in);
System.out.println("enter the radius of circle");
radius=sc.nextDouble();
area=3.14*radius*radius;
circumference=+3.14*2*radius;
System.out.println("the area of circle is"+area);
System.out.println("the circumference of circle is"+circumference);

}
}

Execution Procedure:

• Open command prompt And change the directory to bin folder


• type javac Circle.java
• java Circle.java

14 | P a g e
Brindavan College Of Engineering Department of MCA

b.To accept a number and find whether the number is Prime or not.

import java.util.*;
import java.io.*;

class Prime
{
public static void main(String args[])
{ int
num;
int count=0;
Scanner sc=new Scanner(System.in);
System.out.println("enter the number");
num=sc.nextInt();
for(int i=0;i<=num;i++)
{
if(i%num==0)
{
count++;
}
}
if(count==2)
{
System.out.println("the given number "+num+" is prime number");
}
else{
System.out.println("the given number "+num+" is not prime number");
}

}
}
Execution Procedure:

• Open command prompt And change the directory to bin folder


• type javac Prime.java
• java Prime.java

15 | P a g e
Brindavan College Of Engineering Department of MCA

4. Write a Java program to demonstrate a division by zero exception

import java.io.*;
import java.util.*;

class ExceptionExample {
public static void main(String[] args)
{
System.out.println("enter the any number");
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
try {
int result= num/ 0;
}
catch (ArithmeticException e) {
System.out.println(
"Zero cannot divide any number");
}
}
}
Execution procedure:
• Open command prompt And change the directory to bin folder
• type javac ExceptionExample.java
• java ExceptionExample.java

16 | P a g e
Brindavan College Of Engineering Department of MCA

5. Write a Java program to implement Inner class and demonstrate its Access
protection.

import java.io.*;
class outer
{ int outdata = 10; void display()
{
inner inobj = new inner();
System.out.println("Accessing from outer class");
System.out.println("The value of outdata is " +outdata);
System.out.println("The value of indata is " +inobj.indata);
} class inner
{ int indata = 20; void inmethod()
{
System.out.println("Accessing from inner class");
System.out.println("The sum of indata & outdata is " +(outdata + indata));
}
}
}
class AccessMain
{
public static void main(String args[])
{
outer outobj = new outer(); outobj.display();
outer.inner inobj1 = outobj.new inner(); inobj1.inmethod();
}
}
Execution Procedure:
• Open command prompt And change the directory to bin folder
• type javac AccessMain.java
• java AccessMain.java

17 | P a g e
Brindavan College Of Engineering Department of MCA

6. Write a Java program to demonstrate Constructor Overloading and Method


Overloading.

public class Student { int


id;
String name;

Student(){
System.out.println("this a default constructor");
}

Student(int i, String n){


id = i;
name = n;

}
void area(int radius)
{
System.out.println("\n method overloading the area of circle"+3.14*radius*radius);
}
void area(int len,int width)
{
System.out.println("\n method overloading the area of rectangle"+len*width); }

public static void main(String[] args) {


System.out.println("\n****** Constructor overloading******** \n");
Student s = new Student();

System.out.println("\nDefault Constructor values: \n");


System.out.println("Student Id : "+s.id + "\nStudent Name : "+s.name);

System.out.println("\nParameterized Constructor values: \n");


Student student = new Student(10, "David");
System.out.println("Student Id : "+student.id + "\nStudent Name : "+student.name);
System.out.println("\n******method overloading******** \n"); s.area(3);
s.area(5,4);
}
}
Execution Procedure:
• Open command prompt And change the directory to bin folder
• type javac Student.java
• java Student.java

18 | P a g e
Brindavan College Of Engineering Department of MCA

7. Write a JAVA program to demonstrate Inheritance. Simple Program on Java for the
implementation of Multiple inheritance using interfaces to calculate the area of a
rectangle and triangle.

import java.io.*;
interface area
{
float compute(float x, float y);
}

class rectangle
{
public float compute(float x, float y)
{
return (x*y);
}
}

class triangle
{
public float compute(float x, float y)
{
return (x*y/2);
}
}

class result extends rectangle implements area


{
public float compute(float x, float y)
{
return (x*y);
}
}

class result1 extends triangle implements area


{ public float compute(float x,
float y)
{
return (x*y/2);
}
}

class Inheritance1
{
public static void main(String args[])
{
result rect = new result();
result1 tri = new result1();
area a;
a = rect;
System.out.println("\nArea of rectangle = " + a.compute(10,20));
a = tri;
System.out.println("\nArea of triangle = " +a.compute(10,2));
19 | P a g e
Brindavan College Of Engineering Department of MCA

}
}

Execution Procedure:
• Open command prompt And change the directory to bin folder
• type javac Inheritance1.java
• java Inheritance1.java

20 | P a g e
Brindavan College Of Engineering Department of MCA

8. Write a Java applet program, which handles keyboard event.

import java.applet.Applet;
import java.awt.*; import
java.awt.event.*;
/* <APPLET CODE ="AppletKeyboard.class" WIDTH=300 HEIGHT=200> </APPLET>
*/
public class AppletKeyboard extends Applet implements KeyListener
{
TextField t,tt,tp,tr;
public void init()
{
t=new TextField(20);
t.addKeyListener(this);
tt=new TextField(70); tp=new
TextField(70);
tr=new TextField(70);
add(t);
add(tt);
add(tr);
add(tp);
}
public void keyTyped(KeyEvent e)
{
tt.setText("key Released"+e.getKeyChar());
}
public void keyReleased(KeyEvent e)
{
tr.setText("key Released"+e.getKeyChar());
}
public void keyPressed(KeyEvent e)
{
int kc;
String s;
kc=e.getKeyCode();
s=e.getKeyText(kc);
tp.setText("Key Pressed"+s);
}
}

Execution Procedure:
• Open command prompt And change the directory to bin folder
• type javac AppletKeyboard.java
• appletviewer AppletKeyboard.java
Note: Applets only supported by older jdk version (below & jdk 1.8)

21 | P a g e
Brindavan College Of Engineering Department of MCA

9. Write a Java Program to create a window we press


M or m the window displays Good Morning A or a the
window displays Good After Noon
E or e the window displays Good Evening
N or n the window displays Good Night

public class Program9 extends Frame implements KeyListener {


Label lbl; Program9() { addKeyListener(this);
requestFocus(); lbl = new Label();
lbl.setBounds(100, 100, 200, 40);
lbl.setFont(new Font("Calibri", Font.BOLD, 16));
add(lbl); setSize(400, 300); setLayout(null);
setVisible(true); }
public void keyPressed(KeyEvent e)
{ if (e.getKeyChar() == 'M' || e.getKeyChar() == 'm') lbl.setText("Good
morning");
else if (e.getKeyChar() == 'A' || e.getKeyChar() == 'a') lbl.setText("Good
afternoon");
else if (e.getKeyChar() == 'E' || e.getKeyChar() == 'e') lbl.setText("Good
evening");
else if (e.getKeyChar() == 'N' || e.getKeyChar() == 'n')
lbl.setText("Good night"); } public void
keyReleased(KeyEvent e) { } public void
keyTyped(KeyEvent e) {
} public static void main(String[] args)
{ new Program9();
}}

Execution Procedure:
• Open command prompt And change the directory to bin folder
• type javac Program9.java
• java Program9.java

22 | P a g e
Brindavan College Of Engineering Department of MCA

10.Write a Java program to implement a Queue using user defined Exception Handling
(also make use of throw, throws). a. Complete the following: b. Create a package named
shape. c. Create some classes in the package representing some common shapes like
Square, Triangle, and Circle. d. Import and compile these classes in other program.
import java.util.Scanner;
class oe extends Exception {
int a; oe(int a) { this.a = a; }
public String toString() { return
"Overflow inserting " + a;
}
}
class ue extends Exception {
int a; ue(int a)
{ this.a
= a;
} public String toString()
{ if (a == 1) return
"Underflow";
else return "Queue Empty";
}
} class queue
{ int q[], r, f;
queue(int s) {
q = new int[s];
r = 0; f = 0; } void insert(int a)
throws oe { if (r == q.length)
throw new oe(a);
else { q[r++] = a;
System.out.println(a + " pushed into queue");
System.out.println("Remaining queue Capacity : " + (q.length - r));
}
}
int delete() throws ue { if
(f == r) throw new ue(1);
else {
System.out.println("Queue contains " + (r - f) + " elements before Deletion"); return
q[f++];
}
}
void display() throws ue { int i;
if (f == r) throw new ue(2); else
for (i = f; i < r; i++) System.out.print(q[i]
+ "\t");
}

} public class Student {


public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i, s, a, ch;
queue q;
System.out.println("Enter Queue Size :");
s = in.nextInt(); q = new queue(s); do {

23 | P a g e
Brindavan College Of Engineering Department of MCA

System.out.println("\n1. Insert");
System.out.println("2. Delete");
System.out.println("3. Display");
System.out.println("4. Exit"); ch
= in.nextInt(); try { switch (ch)
{
case 1: System.out.println("Enter element to insert");
a = in.nextInt(); q.insert(a); break;
case 2: System.out.println(q.delete() + " deleted ");
break; case 3: q.display(); break;
} }
catch (oe e) { System.out.println(e);
} catch (ue e) {
System.out.println(e);
}
} while (ch != 4);
}
}
Execution Procedure
• Open command prompt And change the directory to bin folder
• type javac Program9.java
• java Program9.java

24 | P a g e
Brindavan College Of Engineering Department of MCA

Program(Reactangle1.java) package Shape;


public class Rectangle1 { private double
length, breadth; public void
setRectangle(double len, double br)
{ length = len; breadth = br; }
public void area() { double
area = length * breadth;
System.out.println("Area of Rectangle =" + area);
}
}

Program(Square.java)

package Shape; public class


Square { private double side;
public void setSquare(double val)
{ side = val; }
public void area() {
System.out.println("Area of Square=" + (side * side));
}
}
Program(Circle1.java) package
Shape; public class Circle1 { private
double rad; public void
setCircle(double radius) { rad =
radius; } public void area() { double
area = 3.14 * rad * rad;
System.out.println("Area of Rectangle =" + area);
}
}
Program(Program10.java)

import Shape.Rectangle1; import


Shape.Square; import Shape.Circle1;
public class Program10 { public static
void main(String args[]) { Rectangle1
rect = new Rectangle1();
rect.setRectangle(5.6, 6.4);
rect.area();
Square sq = new Square();
sq.setSquare(10.5); sq.area();
Circle1 round = new Circle1();

round.setCircle(5.6); round.area();
}}

Execution Process:
 First create shape folder inside bin Folder
 Then open notepad and create Rectangle1.java file inside the Shape Folder
 Then open notepad and create Square.java file inside the Shape Folder
 Then open notepad and create Circlele1.java file inside the Shape Folder
 Next open notepad and create Program10.java Program inside Bin Folder
25 | P a g e
Brindavan College Of Engineering Department of MCA

 Open Command Prompt type Javac program10.java


 Then Run java Program10.java

26 | P a g e

You might also like