Nafis Java - Manual
Nafis Java - Manual
INFORMATION TECHNOLOGY
(AIIT)
1
AMITY UNIVERSITY MAHARASHTRA
CERTIFICATE
This is to certify that_MR. Nafis Parwez Enrolment No. A710145023050 of class MCA,
Semester 1 has satisfactorily completed the Java Lab Report Practical lab
Manual prescribed by Amity University Maharashtra during the academic year 2023-2024.
Name: Name:
_____________________ _____________________
Index
Sr. No Practical
2
1 1. WAP to print Hello World
1. WAP in java to print even and odd number between 100 to 150 using for loop.
2. WAP in java to print Fibonacci series upto 100 using while loop.
2. Java Array Program to Copy All the Elements of One Array to Another Array
3. Java Array Program to Check Whether Two Matrices Are Equal or Not
5. Java Array Program to Search an Element in an Array 6. Java Array Program for
Bubble Sort
3
1. Java Program to Count Number of Objects Created for Class Passing and
Returning Objects in Java
3. Java Program to Create a Method without Parameters and with Return Type.
Createmethod to calculate the volume of a cuboid which takes the dimensions length,
breadth and height as input and return the volume as output back to the main
method.
4. Java Program to Find Area of Square, Rectangle and Circle using Method
Overloading
5 1. Create a abstract class employee, having its properties & abstract function for
calculating net salary and displaying the information. Drive manager & clerk class
from this abstract class & implement the abstract method net salary and override
the display method.
3. Write a Java program to create a vehicle class hierarchy. The base class should be
Vehicle, with subclasses Truck, Car and Motorcycle. Each subclass should have
properties such as make, model, year, and fuel type. Implement methods for
calculating fuel efficiency, distance traveled, and maximum speed.
4
6
1. Write a Java program that throws an exception and catch it using a try-catch block.
2.Write a Java program to create a method that takes an integer as a parameter and
throws an exception if the number is odd.
3. Write a Java program to create a method that takes a string as input and throws an
exception if the string does not contain vowels.
7
1. Write a Java program to create a basic Java thread that prints "Hello, World!" when
executed.
2. Write a Java program that creates two threads to find and print even and odd number
s
from 1 to 20.
3. Write a Java program that creates a bank account with concurrent deposits and
withdrawals using threads.
4. Write a Java program to create and start multiple threads that increment a
shared counter variable concurrently.
10
5
Practical No. 1
6
7
8
Practical No. 2
9
1. WAP in java to print even and odd number between 100 to 150 using for
loop.
Evenodd.java public class EvenOdd { public
Output:
10
11
12
}
13
}
Output:
main(String[] args) {
14
(
15
public class Armstrong { public static void main(String[]
args) {
16
int num = input.nextInt(); int originalNum,
10; result +=
} if (result == num)
}
}
Practical No. 3
[] {25, 11, 7,
17
arr.length; i++) { if(arr[i] > max) max =
arr[i];
Output
Q.2) Java Array Program to Copy All the Elements of One Array to
Another Array.
Solution – Code
{ arr2[i] = arr1[i];
18
}
System.out.println();
Output
Q.3) Java Array Program to Check Whether Two Matrices Are Equal or
Not.
Solution –
Code
{1, 2, 5},
19
{4, 3, 7},
{8, 4, 6}
};
int b[][]
={
{1, 2, 5},
{4, 3, 7},
{8, 4, 6}
}; row1
a.length; col1 =
a[0].length;
row2 = b.length;
col2 = b[0].length;
break;
} if(flag)
20
}
Output
Code
{8, 9, 7} };
display(matrix);
matrix[i][j];
} display(transpose);
21
System.out.println("The matrix is: "); for(int[] row
System.out.println();
Output
return i;
22
return -1;
= 30;
Output
Solution –
Code
return i;
return -1;
23
public static void main(String a[]){ int[]
= 30;
Output
Practical No. 4
1. Java Program to Count Number of Objects Created for Class Passing and
0;
ObjectCounter() {
objectCount++;
ObjectCounter createNewObject() {
24
ObjectCounter obj1 = new ObjectCounter();
Output :
main(String[] args) {
= input.nextInt();
= input.nextInt();
25
System.out.println("Addition: " + result1); int result2 = obj.subtract(num1, num2);
= obj.divide(num1, num2);
return a + b; } int
subtract(int a, int b) {
return a - b; } int
multiply(int a, int b) {
return a * b; } int
a / b;
}
}
Output:
26
dimensions length, breadth and height as input and return the volume as
output back to the main method.
= input.nextDouble();
= input.nextDouble();
breadth, height);
27
}
Output:
4. Java Program to Find Area of Square, Rectangle and Circle using Method
Overloading
Prac44.java public class
AreaCalculator {
* side;
* radius;
} else {
28
System.out.println("Invalid shape specified for circle area calculation."); return
-1;
//
rectangleWidth);
"circle");
}
}
Output:
29
Practical No. 5
1. Create a abstract class employee, having its properties & abstract function
for calculating net salary and displaying the information. Drive manager &
clerk class from this abstract class & implement the abstract method net
salary and override the display method.
abstract class Employee {
double basicSalary;
employeeId, double
basicSalary) { this.name =
name; this.employeeId =
employeeId;
this.basicSalary = basicSalary;
display() {
30
System.out.println("Basic Salary: " + basicSalary);
bonus;
{ return basicSalary
+ bonus;
display() {
super.display();
31
class Clerk extends Employee { double
overtime;
+ overtime;
display() {
super.display();
System.out.println();
32
Clerk clerk = new Clerk("Alice", 102, 40000, 5000); clerk.display();
}
}
Output :
double balance;
+= amount;
33
public void withdraw(double amount) { if
= amount;
} else {
{ balance -= amount;
} else {
34
SavingsAccount savingsAccount = new SavingsAccount();
savingsAccount.deposit(200); savingsAccount.withdraw(50);
savingsAccount.withdraw(150);
}
}
Output :
3. Write a Java program to create a vehicle class hierarchy. The base class
should be Vehicle, with subclasses Truck, Car and Motorcycle. Each subclass
should have properties such as make, model, year, and fuel type. Implement
methods for calculating fuel efficiency, distance traveled, and maximum
speed.
class Vehicle { protected
fuelType;
= fuelType;
35
// Method to calculate fuel efficiency (dummy implementation)
calculateFuelEfficiency();
getMaxSpeed() {
36
// Displaying information and using methods
37
}
String type;
} void eat()
38
{
System.out.println("Animal is eating.");
Animal {
String breed;
// Using super to call the constructor of the immediate parent class Animal)
@Override
void eat() {
// Using super to call the eat method of the immediate parent class Animal)
super.eat();
System.out.println("Dog is eating.");
} void bark()
System.out. println("Dog
is barking.");
main(String[] args) {
39
Dog myDog = new Dog("Mammal", "Labrador");
myDog.eat();
// Calls overridden eat method in Dog class, which alsocalls the eat method in Animal class
// Accessing fields from the Animal class through the Dog class
}
}
Output :
class ParentClass {
int x;
40
}
display() {
Output :
41
double calculatePerimeter(); // Abstract method to calculate the perimeter }
{ return
{ return 2 *
Math.PI * radius;
42
Rectangle class public Rectangle(double length,
= width;
{ return length
* width;
calculatePerimeter() { return 2 *
main(String[] args) {
43
}
Output :
Practical No. 6
44
1. Write a Java program that throws an exception and catch it using a try-catch block.
import java.util.Scanner;
try {
userInput = scanner.nextInt();
} catch (Exception e) {
45
}
Output:
46
2.Write a Java program to create a method that takes an integer as a parameter and
throws an exception if the number is odd.
try {
checkIfEven(userInput);
} catch (Exception e) {
47
// Method to check if a number is even public static void checkIfEven(int
Exception("Number is odd!");
} else {
System.out.println("Number is even.");
Output :
48
3. Write a Java program to create a method that takes a string as input and throws an
exception if the string does not contain vowels. Vowel.java import java.util.Scanner;
args) {
try {
49
50
1. Write a Java program to create a basic Java thread that prints "Hello, World!" when
System.out.println("Hello, World!");
thread.start();
Output:
51
2. Write a Java program that creates two threads to find and print even and odd
main(String[] args) {
52
Thread oddThread = new Thread(() -> printNumbers(false));
evenThread.start(); oddThread.start();
Output:
53
54
55
3. Write a Java program that creates a bank account with concurrent deposits and
amount;
} else {
56
BankAccount account = new BankAccount();
});
});
depositThread.start(); withdrawThread.start();
Output:
57
4. Write a Java program to create and start multiple threads that increment a shared counter
0;
58
Thread thread = new Thread(() -> { for
} });
thread.start();
Output:
5. Write a Java program to create a producer-consumer scenario using the wait() and
notify() methods for thread synchronization. Prac75.java import java.util.LinkedList; import
java.util.Queue;
59
60
61
class SharedResource { private Queue<Integer> buffer = new
notify();
(buffer.isEmpty()) { wait();
62
63
64
65
notify();
sharedResource.produce();
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
});
66
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
});
producerThread.start(); consumerThread.start();
}
}
Output:
67
Practical No. 8
Output:
68
2. WAP to write text in text file.
Prac82.java import
java.io.BufferedWriter; import
java.io.FileWriter; import
java.io.IOException;
} catch (IOException e) {
e.printStackTrace();
69
}
Output:
Practical 9
1. Write a java program for calculator operation using AWT controls.
Code
70
import java.awt.*;
import java.awt.event.*;
TextFieldExample2() {
tf1 = new TextField();
tf1.setBounds(50, 50, 150, 20);
tf2 = new TextField();
tf2.setBounds(50, 100, 150, 20);
tf3 = new TextField();
tf3.setBounds(50, 150, 150, 20);
tf3.setEditable(false); b1
= new Button("+");
b1.setBounds(50, 200, 50, 50);
b2 = new Button("-");
b2.setBounds(120,200,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
add(tf1);
add(tf2);
add(tf3);
add(b1);
add(b2);
setSize(300,300);
setLayout(null); setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s1 = tf1.getText();
String s2 = tf2.getText();
int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);
int c = 0; if
(e.getSource() == b1){
c = a + b;
71
} else if
(e.getSource() == b2){ c
= a - b;
}
String result = String.valueOf(c); tf3.setText(result);
} public static void main(String[] args)
{ new
TextFieldExample2();
}
}
Output
72
Practical 10
1. Write a java program for student registration using swing.
Code
73
import javax.swing.*; import
java.awt.*; import java.awt.event.*;
class MyFrame extends JFrame
implements ActionListener {
private Container c; private
JLabel title; private JLabel name;
private JTextField tname; private
JLabel mno; private
JTextField tmno; private
JLabel gender; private
JRadioButton male; private
JRadioButton female;
private ButtonGroup gengp;
private JLabel dob; private
JComboBox date; private
JComboBox month; private
JComboBox year; private
JLabel add; private
JTextArea tadd; private
JCheckBox term; private
JButton sub; private
JButton reset; private
JTextArea tout; private
JLabel res; private
JTextArea resadd;
private String dates[]
= { "1", "2", "3", "4", "5",
"6", "7", "8", "9", "10",
"11", "12", "13", "14", "15",
"16", "17", "18", "19", "20",
"21", "22", "23", "24", "25",
"26", "27", "28", "29", "30",
"31" };
private String months[]
= { "Jan", "feb", "Mar", "Apr",
"May", "Jun", "July", "Aug",
"Sup", "Oct", "Nov", "Dec" }; private
String years[]
= { "1995", "1996", "1997", "1998",
74
Adarsh Harpude
A710145023067
75
Adarsh Harpude
A710145023067
76
Adarsh Harpude
A710145023067
77
Adarsh Harpude
A710145023067
78
Adarsh Harpude
A710145023067
gender.setSize(100, 20);
gender.setLocation(100, 200);
c.add(gender); male = new
JRadioButton("Male"); male.setFont(new
Font("Arial", Font.PLAIN, 15));
male.setSelected(true); male.setSize(75, 20);
male.setLocation(200, 200);
c.add(male); female = new
JRadioButton("Female"); female.setFont(new
Font("Arial", Font.PLAIN, 15));
female.setSelected(false);
female.setSize(80, 20);
female.setLocation(275, 200); c.add(female);
gengp = new ButtonGroup(); gengp.add(male);
gengp.add(female); dob = new
JLabel("DOB"); dob.setFont(new Font("Arial",
Font.PLAIN, 20)); dob.setSize(100, 20);
dob.setLocation(100,
250);
c.add(dob); date = new
JComboBox(dates); date.setFont(new
Font("Arial", Font.PLAIN, 15));
date.setSize(50, 20); date.setLocation(200,
250);
c.add(date); month = new
JComboBox(months); month.setFont(new
Font("Arial", Font.PLAIN, 15));
month.setSize(60, 20); month.setLocation(250,
250);
c.add(month); year = new
JComboBox(years); year.setFont(new
Font("Arial", Font.PLAIN, 15));
year.setSize(60, 20);
year.setLocation(320,
250);
c.add(year);
79
Adarsh Harpude
A710145023067
80
Adarsh Harpude
A710145023067
81
Adarsh Harpude
A710145023067
82
Adarsh Harpude
A710145023067
83
Adarsh Harpude
A710145023067
84
Adarsh Harpude
A710145023067
85
Adarsh Harpude
A710145023067
86
Adarsh Harpude
A710145023067
tmno.setText(def);
res.setText(def);
tout.setText(def);
term.setSelected(false);
date.setSelectedIndex(0);
month.setSelectedIndex(0);
year.setSelectedIndex(0);
resadd.setText(def);
}
}
} class
Registration {
public static void main(String[] args) throws
Exception
{
MyFrame f = new MyFrame();
}
}
Output
87
Adarsh Harpude
A710145023067
88