Java Labbook Amol - Shinde
Java Labbook Amol - Shinde
Prepared by,
Name : Kunal Awate>
Roll No : 23123
Program Index
Page Completion Remark S
Program
No. Date
allation of jdk environment & following utilities. What is javac,
p and javadoc.
e a program to count the number of objects of a class.
e a program to assign object reference variable to another and
e the reference to object and garbage collection.
d only one parameter from command line argument and
ay the welcome message also check the length.
te a class named 'Rectangle' with two data members 'length'
breadth' and two methods to print the area and perimeter of
ectangle respectively. Its constructor having parameters for
h and breadth is used to initialize length and breadth of the
angle. Let class 'Square' inherit the 'Rectangle' class with its
tructor having a parameter for its side (suppose s) calling the
tructor of its parent class as 'super(s,s)'. Print the area and
meter of a rectangle and a square.
te a class named 'Member' having the following members:
members : Name, Age, Phone number, Address, Salary. It
has a method named 'printSalary' which prints the salary of
members. Two classes 'Employee' and 'Manager' inherits the
mber' class. The 'Employee' and 'Manager' classes have data
bers 'specialization' and 'department' respectively. Now,
gn name, age, phone number, address and salary to an
oyee and a manager by making an object of both of these
ses and print the same.
gn a java program using interface, which performs simple
metic such as addition, subtraction, multiplication and
ion.
e a program having interface A containing two methods
h1() and meth2(). interface B extends A which contain method
h3() and create one class which implements B. call all the
hod and print the output.
e a program to implement the polymorphism by creating one
rclass Beaches and three subclasses Mumbai, Kanyakumari,
gluru. Subclasses extend the superclass and override its
ion() and famousfor() methods. Call the location and
usfor methods of parent class i.e. beaches.
e a program to implement a user defined exception “NotPrime
ption”. Read number from command line and cjheck whether
number is prime or not. If it is prime then display message
mber is prime” and if it is not prime throw the exception
PrimeException”.
e a java code to create thread object and start it.
e a program that creates and run the following threads-
. To print letter ‘A’ 75 times.
. To print letter ‘B’ 100 times.
. To print integer 1 to 100.
e a program to Create multiple Threads
e a program to change name and priority of Thread
gn a program to create an *ArrayList*. Create a *Student*
s, assign rollno, name and marks to him using constructor.
e 5 objects of Student class in ArrayList. And display the
rds of the students who has more than 60 marks.
gn a program to create an *LinkedList*. Create a Employee
s, assign emp_id, emp_name and emp_salary to him using
tructor. Store 5 objects of Employee class in LinkedList.
ate the salary of the all the Employees with 10%.
Question 1: Installation of jdk environment & following utilities. What is
javac, javap and javadoc.
*********************************************************************************
1. After installing the JDK, you need to set the environment variables to make it
accessible from the command line.
o Right-click on "This PC" (or "My Computer") and select "Properties."
o Click on "Advanced system settings" on the left.
o In the "System Properties" window, click the "Environment Variables" button.
o Under "System Variables," scroll down to find "Path," select it, and click "Edit."
o Click "New" and add the path to the "bin" directory inside your JDK installation
directory (e.g., C:\Program Files\Java\jdk1.8.0_311\bin). Separate
multiple paths with semicolons.
o Click "OK" to save the changes.
2. java -version
3. This should display the version of the installed JDK.
javac: This is the Java compiler. It is used to compile Java source code files (.java)
into bytecode files (.class). For example, to compile a Java file called
MyProgram.java, you would use javac MyProgram.java.
javap: This is a disassembler for Java class files. It allows you to inspect the structure
and bytecode of compiled Java classes. For example, to view the details of a class
called MyClass, you would use javap -c MyClass.
javadoc: This tool is used to generate HTML documentation from Java source code.
It reads specially formatted comments in the code (JavaDoc comments) and creates
API documentation. To generate documentation for a Java class, use the javadoc
command followed by the name of the class.
Question 2: Write a program to count the number of objects of a class.
***************************************************************************
public ObjectCounter() {
objectCount++;
return objectCount;
}
Output:- Total number of ObjectCounter objects: 3
Roll No :23123
*************************************************************************************
// Create an object
obj1 = null;
System.gc();
if (obj1 == null) {
System.out.println("obj1 is null.");
if (obj2 != null) {
System.out.println("obj2 is still valid.");
} else {
System.out.println("obj2 is null.");
class MyClass {
this.value = value;
return value;
@Override
Question 4: Read only one parameter from command line argument and
display the welcome message also check the length.
if (args.length == 1) {
} else {
Roll No :23123
Question 5: Create a class named 'Rectangle' with two data members
'length' and 'breadth' and two methods to print the area and perimeter
of the rectangle respectively. Its constructor having parameters for
length and breadth is used to initialize length and breadth of the
rectangle. Let class 'Square' inherit the 'Rectangle' class with its
constructor having a parameter for its side (suppose s) calling the
constructor of its parent class as 'super(s,s)'. Print the area and
perimeter of a rectangle and a square.
*************************************************************************************
this.length = length;
this.breadth = breadth;
super(side, side); // Call the parent class constructor with both sides
being the same
rectangle.calculateArea();
rectangle.calculatePerimeter();
square.calculateArea();
square.calculatePerimeter();
*********************************************************************************
String name;
int age;
String phoneNumber;
String address;
double salary;
public Member(String name, int age, String phoneNumber, String address, double salary) {
this.name = name;
this.age = age;
this.phoneNumber = phoneNumber;
this.address = address;
this.salary = salary;
}
}
String specialization;
public Employee(String name, int age, String phoneNumber, String address, double salary,
String specialization) {
this.specialization = specialization;
String department;
public Manager(String name, int age, String phoneNumber, String address, double salary,
String department) {
this.department = department;
Employee employee = new Employee("John Doe", 30, "555-123-4567", "123 Main St",
50000.0, "Programming");
Manager manager = new Manager("Jane Smith", 35, "555-987-6543", "456 Oak St",
75000.0, "Operations");
// Print employee details
System.out.println("Employee Details:");
employee.printSalary();
System.out.println("\nManager Details:");
manager.printSalary();
Salary: 50000.0
Specialization: Programming
Manager Details:
Name: Jane Smith
Age: 35
Name :Kunal Awate
Roll No :23123
@Override
public double subtract(double a, double b) {
return a - b;
}
@Override
public double multiply(double a, double b) {
return a * b;
}
@Override
public double divide(double a, double b) {
if (b == 0) {
System.out.println("Division by zero is not allowed.");
return Double.NaN; // Not-a-Number
}
return a / b;
}
}
Output :-
Addition: 10.0 + 5.0 = 15.0
Subtraction: 10.0 - 5.0 = 5.0
Multiplication: 10.0 * 5.0 = 50.0
Division: 10.0 / 5.0 = 2.0
Roll No :23123
Question 8: Write a program having interface A containing two methods
meth1() and meth2(). interface B extends A which contain method
meth3() and create one class which implements B. call all the method
and print the output
*********************************************************************************
void meth2();
}
@Override
public void meth2() {
System.out.println("Method meth2() from interface A");
}
@Override
public void meth3() {
System.out.println("Method meth3() from interface B");
}
}
Output:-
Method meth1() from interface A
Method meth2() from interface A
Method meth3() from interface B
Roll No :23123
Question 9: Write a program to implement the polymorphism by
creating one superclass Beaches and three subclasses Mumbai,
Kanyakumari, Mangluru. Subclasses extend the superclass and override
its location() and famousfor() methods. Call the location and famousfor
methods of parent class i.e. beaches.
*********************************************************************************
@Override
public void famousfor() {
System.out.println("Mumbai Beach is famous for the Gateway of
India.");
}
}
@Override
public void famousfor() {
System.out.println("Kanyakumari Beach is famous for its sunrise and
sunset views.");
}
}
@Override
public void famousfor() {
System.out.println("Mangaluru Beach is famous for its pristine
shores.");
}
}
kanyakumariBeach.location();
kanyakumariBeach.famousfor();
mangaluruBeach.location();
mangaluruBeach.famousfor();
}
}
Roll No :23123
Question 10: Write a program to implement a user defined exception
“NotPrime Exception”. Read number from command line and cjheck
whether the number is prime or not. If it is prime then display message
“Number is prime” and if it is not prime throw the exception
“NotPrimeException”.
*********************************************************************************
NotPrimeException(String s) {
super(s);
try {
if (args.length == 0) {
if (isPrime(num)) {
System.out.println("Number is prime.");
} else {
} catch (NumberFormatException e) {
} catch (NotPrimeException e) {
if (num <= 1) {
return false;
if (num % i == 0) {
return false;
return true;
Output:-
java CommandLineArgsDemo 7
Number is prime.
java CommandLineArgsDemo 10
Roll No :23123
Question 11: Write a java code to create thread object and start it.
*************************************************************************************
myThread.start();
Roll No :23123
Question 12: Write a program that creates and run the following threads-
threadA.start();
threadB.start();
threadNumbers.start();
}
}
Output :-
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB1 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 32
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
96 97 98 99 100.
Roll No :23123
Question 13: Write a program to Create multiple Threads
*************************************************************************************
Ans: public class MultiThreadDemo {
public static void main(String[] args) {
int numThreads = 5;
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(threadName + " - Count: " + i);
try {
Thread.sleep(1000); // Sleep for 1 second
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Question 14: Write a program to change name and priority of Thread
*************************************************************************************
Ans:- public class ThreadNamePriorityDemo {
public static void main(String[] args) {
// Create a thread and specify its behavior
Thread customThread = new Thread(() -> {
System.out.println("Thread name: " + Thread.currentThread().getName());
System.out.println("Thread priority: " + Thread.currentThread().getPriority());
});
Roll No :23123
Question 15: Design a program to create an *ArrayList*. Create a
*Student* class, assign rollno, name and marks to him using
constructor. Store 5 objects of Student class in ArrayList. And
display the records of the students who has more than 60 marks.
******************************************************************************
class Student {
private int rollNo;
private String name;
private int marks;
@Override
public String toString() {
return "Roll No: " + rollNo + ", Name: " + name + ", Marks: " +
marks;
}
}
Output :-
Students with more than 60 marks:
Roll No: 1, Name: John, Marks: 75
Roll No: 3, Name: Bob, Marks: 80
Roll No: 4, Name: Eva, Marks: 65
Roll No :23123