Java Lab Manual
Java Lab Manual
[Link]
I/II Semester
(common to all branches)
LAB MANUAL
}
Prabodh C P
Asst Professor
Dept of CSE, SIT
Volunteer, FSMK
Visit : [Link]
The versioned repository of all the programs can be found here as a GitLab Repository
[Link]
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
To veiw a copy of this license, visit [Link]
Contents
1 Quadratic Equation 1
2 Array Multiplication 3
3 Shift Operation 5
4 Sorting 7
5 Student Details 9
7 Inheritance 14
8 Dynamic Dispatch 18
10 Exception Handling 23
i
Listings
1.1 [Link] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.1 [Link] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.1 [Link] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.1 [Link] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.1 [Link] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
6.1 [Link] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
6.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
7.1 [Link] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
7.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
8.1 [Link] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
8.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
9.1 [Link] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
9.2 [Link] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
9.3 [Link] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
9.4 [Link] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
9.5 [Link] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
9.6 [Link] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
9.7 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
10.1 [Link] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
10.2 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
ii
Preface
Use and spread the word of Free Software. Free Software leads to a Free Society!
Prabodh C P
iii
Chapter 1
Quadratic Equation
Question
Write a JAVA program that prints all real solutions to the quadratic equation ax2+bx+c=0. Read
in a, b, c and use the quadratic formula.
Java Code
1 import [Link];
2
7 [Link]("Enter a: ");
8 double a = [Link]();
9
10 [Link]("Enter b: ");
11 double b = [Link]();
12
13 [Link]("Enter c: ");
14 double c = [Link]();
15
16 double discriminant = b * b - 4 * a * c;
17 if (discriminant > 0) {
18 [Link]("The equation has real and distinct roots.");
19 double root1 = (-b + [Link](discriminant)) / (2 * a);
20 double root2 = (-b - [Link](discriminant)) / (2 * a);
21 [Link]("The roots are " + root1 + " and " + root2);
22 } else if (discriminant == 0) {
23 [Link]("The equation has real and equal roots.");
24 double root = -b / (2 * a);
25 [Link]("The root is " + root);
26 } else {
27 [Link]("The equation has no real roots.");
28 double realp = -b / (2 * a);
29 double imagp = ([Link](-discriminant)) / (2 * a);
30 [Link]("The roots are (%.2f + i%.4f) and (%.2f - i%.4f)\n",
realp, imagp, realp, imagp);
31 }
32 }
33 }
Listing 1.1: [Link]
1
CHAPTER 1. QUADRATIC EQUATION
Output
=================================
1 putta:˜/.../Program1$ javac [Link]
2 putta:˜/.../Program1$ java QuadraticEquationDemo
3 Enter a: 1
4 Enter b: 4
5 Enter c: 4
6 The equation has real and equal roots.
7 The root is -2.0
8
Array Multiplication
Question
Write a JAVA program for multiplication of two arrays.
Java Code
1 import [Link];
2 import [Link];
3
23 if (n != m) {
24 [Link]("Error: Arrays have different length, not possible to
multiply them.");
25 return;
26 }
27
3
CHAPTER 2. ARRAY MULTIPLICATION
Output
=================================
1 putta:˜/.../Program2$ javac [Link]
2 putta:˜/.../Program2$ java MultiplyArraysDemo
3 Enter the number of elements in the first array: 4
4 Enter element 1 of the first array: 1
5 Enter element 2 of the first array: 2
6 Enter element 3 of the first array: 3
7 Enter element 4 of the first array: 4
8 Enter the number of elements in the second array: 4
9 Enter element 1 of the second array: 4
10 Enter element 2 of the second array: 3
11 Enter element 3 of the second array: 2
12 Enter element 4 of the second array: 1
13 Result of multiplying arrays: [4, 6, 6, 4]
14
Shift Operation
Question
Demonstrate the following operations and sign extension with Java programs
i) ≪ ii) ≫ iii) ⋙
Java Code
1 import [Link];
2
3 class OperatorDemoSignExtension {
4 public static void main(String[] args) {
5 Scanner sc = new Scanner([Link]);
6
5
CHAPTER 3. SHIFT OPERATION
36 }
Listing 3.1: [Link]
Output
=================================
1 putta:˜/.../Program10$ javac [Link]
2 putta:˜/.../Program10$ java OperatorDemoSignExtension
3
10 4 << 3 = 32
11
37 4 << 2 = 16
38
45 57 >> 2 = 14
46
53 57 >>> 2 = 14
Listing 3.2: OUTPUT
Sorting
Question
Write a JAVA program to sort list of elements in ascending and descending order.
Java Code
1 import [Link];
2
7
CHAPTER 4. SORTING
45 }
46 }
Listing 4.1: [Link]
Output
=================================
1 putta:˜/.../Program4$ javac [Link]
2 putta:˜/.../Program4$ java ExchangeSortDemo
3 Enter the number of elements in the array:
4 6
5 Enter the elements of the array:
6 6 1 5 2 4 3
7
Student Details
Question
Create a JAVA class called Student with the following details as variables within it.
USN
NAME
BRANCH
PHONE
PERCENTAGE
Write a JAVA program to create n Student objects and print the USN, Name, Branch, Phone, and
percentage of these objects with suitable headings.
Java Code
1 import [Link];
2 import [Link];
3 import [Link];
4
5 class StudentType {
6 private String USN;
7 private String NAME;
8 private String BRANCH;
9 private String PHONE;
10 private double PERCENTAGE;
11
12 public StudentType(String USN, String NAME, String BRANCH, String PHONE, double
PERCENTAGE) {
13 [Link] = USN;
14 [Link] = NAME;
15 [Link] = BRANCH;
16 [Link] = PHONE;
17 [Link] = PERCENTAGE;
18 }
19
9
CHAPTER 5. STUDENT DETAILS
33 return PHONE;
34 }
35
41
Output
=================================
1 putta:˜/.../Program5$ javac [Link]
2 putta:˜/.../Program5$ java StudentDemo
3 Enter the number of students:
4 2
5 Enter the details of student 1:
6 USN: 1SI22CS036
7 NAME: Rajesh
8 BRANCH: CSE
9 PHONE: 7187238165
10 PERCENTAGE: 89.7
11
16 PHONE: 8238887233
17 PERCENTAGE: 84.5
18
19 STUDENT DETAILS
20 =====================
21 USN NAME BRANCH PHONE PERCENTAGE
22 1SI22CS036 Rajesh CSE 7187238165 89.7
23 1PE22EC088 Sheela ECE 8238887233 84.5
Listing 5.2: OUTPUT
Question
Write a JAVA program demonstrating Method overloading and Constructor overloading.
Java Code
1 class PointType {
2 private double x;
3 private double y;
4
5 // Constructor overloading
6 public PointType() {
7 this.x = 0;
8 this.y = 0;
9 }
10
33 class PointDemo{
34 public static void main(String[] args) {
35 PointType point1 = new PointType(1, 1);
36 PointType point2 = new PointType(7, 9);
37 [Link]("\npoint1 coordinates :"); [Link]();
38 [Link]("point2 coordinates :"); [Link]();
12
CHAPTER 6. POLYMORPHISM - METHOD OVERLOADING
Output
=================================
1 putta:˜/.../Program6$ javac [Link]
2 putta:˜/.../Program6$ java PointDemo
3
Inheritance
Question
Design a super class called Staff with details as StaffId, Name, Phone, Salary. Extend this class
by writing three subclasses namely Teaching (domain, publications), Technical (skills), and Con-
tract (period). Write a JAVA program to read and display at least 3 staff objects of all three
categories.
Java Code
1 class Staff {
2 private int staffId;
3 private String name;
4 private String phone;
5 private double salary;
6
14
CHAPTER 7. INHERITANCE
42 public Teaching(int staffId, String name, String phone, double salary, String
domain, int publications) {
43 super(staffId, name, phone, salary);
44 [Link] = domain;
45 [Link] = publications;
46 }
47
66 public Technical(int staffId, String name, String phone, double salary, String
skills) {
67 super(staffId, name, phone, salary);
68 [Link] = skills;
69 }
70
83 public Contract(int staffId, String name, String phone, double salary, int
period) {
84 super(staffId, name, phone, salary);
85 [Link] = period;
86 }
87
97
98
Output
=================================
1 putta:˜/.../Program7$ javac [Link]
2 putta:˜/.../Program7$ java StaffTypeDemo
3 Teaching Staff 1:
4 Name: Rajesh Nayak
5 Staff ID: 1
6 Phone: 9822546534
7 Salary: 75000.0
8 Domain: Computer Science
9 Publications: 15
10
11 Teaching Staff 2:
19 Teaching Staff 3:
20 Name: John Peter
21 Staff ID: 3
22 Phone: 8528734373
23 Salary: 85000.0
24 Domain: Physics
25 Publications: 25
26
27 Technical Staff 1:
28 Name: Ramesha
29 Staff ID: 4
30 Phone: 9473673642
31 Salary: 90000.0
32 Skills: Java,Python,C++
33
34 Technical Staff 2:
35 Name: Suresha
36 Staff ID: 5
37 Phone: 8917612332
38 Salary: 95000.0
39 Skills: JavaScript,React,[Link]
40
41 Technical Staff 3:
42 Name: Dinesha
43 Staff ID: 6
44 Phone: 9944222323
45 Salary: 100000.0
46 Skills: Python,TensorFlow,Keras
47
48 Contract Staff 1:
49 Name: Abida Begum
50 Staff ID: 7
51 Phone: 9323786211
52 Salary: 75000.0
53 Period: 6 months
54
55 Contract Staff 2:
56 Name: Lily Thomas
57 Staff ID: 8
58 Phone: 8776551219
59 Salary: 80000.0
60 Period: 12 months
61
62 Contract Staff 3:
63 Name: Seema Jain
64 Staff ID: 9
65 Phone: 9922324343
66 Salary: 85000.0
67 Period: 18 months
Listing 7.2: OUTPUT
Dynamic Dispatch
Question
Demonstrate dynamic dispatch using abstract class in JAVA.
Java Code
1 abstract class Shape {
2 abstract void draw();
3 }
4
17 class DynamicDispatchDemo {
18 public static void main(String[] args) {
19 Shape s;
20 s = new Circle();
21 [Link]();
22 s = new Square();
23 [Link]();
24 }
25 }
Listing 8.1: [Link]
Output
=================================
1 putta:˜/.../Program8$ javac [Link]
2 putta:˜/.../Program8$ java DynamicDispatchDemo
3 Drawing Circle
4 Drawing Square
Listing 8.2: OUTPUT
18
Chapter 9
Question
Create two packages P1 and P2. In package P1, create class A, class B inherited from A, class C
. In package P2, create class D inherited from class A in package P1 and class E. Demonstrate
working of access modifiers (private, public, protected, default) in all these classes using JAVA.
Java Code
1 // Package P1
2 package com.P1;
3
1 // Package P1
2 package com.P1;
3
19
CHAPTER 9. PACKAGES AND ACESS MODIFIERS
12
1 // Package P1
2 package com.P1;
3
1 // Package P2
2 package com.P2;
3
1 // Package P2
2 package com.P2;
3
2 import com.P1.A;
3 import com.P1.B;
4 import com.P1.C;
5 import com.P2.D;
6 import com.P2.E;
7
The above files should be stored in the folder structure as shown in the below diagram.
First create a folder com in the same folder where you saved [Link]. Then within com directory
create two directories P1 and P2. Within P1 directory store the files [Link], [Link] and [Link]. Within P2
directory store the files [Link] and [Link].
Output
=================================
1 putta:˜/.../Program9$ javac [Link]
2 putta:˜/.../Program9$ java PackageDemo
3 Value of x: 10
4 Value of y: 20
5 Value of x: 30
6 Value of y: 40
7 Value of z: 50
8 Value of w: 60
9 Value of x: 70
10 Value of y: 80
11 Value of v: 90
12 Value of u: 100
Listing 9.7: OUTPUT
Exception Handling
Question
Write a JAVA program to read two integers a and b. Compute a/b and print, when b is not zero.
Raise an exception when b is equal to zero. Also demonstrate working of ArrayIndexOutOfBound-
Exception.
Java Code
1 import [Link];
2
23
CHAPTER 10. EXCEPTION HANDLING
Output
=================================
1 putta:˜/.../Program10$ javac [Link]
2 putta:˜/.../Program10$ java ExceptionDemo
3