DhruvShah JavaExperiments
DhruvShah JavaExperiments
EXPERIMENT NO. 1
AIM / OBJECTIVE:
PROGRAM/CODE:
Problem Statement 1:
Code:
import java.util.Scanner;
class Main {
public static void main(String args[]){
Scanner scanner= new Scanner(System.in);
System.out.println("Enter a number:");
int n = scanner.nextInt();
if(n%2!=0)
System.out.println("Weird");
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
else if(n>=2 && n<=5)
System.out.println("Not Weird");
else if(n>=20 && n<= 6)
System.out.println("Weird");
else if(n>20)
System.out.println("Not Weird");
}
}
OUTPUT:
Problem Statement 2:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
WAP to find largest of 3 numbers using nested if else and nested ternary
operator.
Code:
(a)
import java.util.Scanner;
class Main {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
System.out.println("Enter three numbers:");
int a = s.nextInt();
int b = s.nextInt();
int c = s.nextInt();
if (a > b) {
if (a > c)
System.out.println("The greatest is " + a);
else
System.out.println("The greatest is " + c);
} else {
if (b > c)
System.out.println("The greatest is " + b);
else
System.out.println("The greatest is " + c);
}
}
}
(b)
import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner s = new Scanner(System.in);
System.out.println("Enter three numbers:");
int a = s.nextInt();
int b = s.nextInt();
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
int c = s.nextInt();
Output:
(a)
(b)
Problem Statement 3:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Write a Java program that reads a positive integer from command line and count
the number of digits the number (less than ten billion) has.
Code:
class Main{
public static void main(String args[]){
long n = Long.parseLong(args[0]);
int count = 0;
while(n!=0){
count++;
n/=10;
}
Output:
Problem Statement 4:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Write a menu driven program using switch case to perform mathematical
operations.
Code:
import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner s = new Scanner(System.in);
System.out.println("Enter two numbers:");
int a = s.nextInt();
int b = s.nextInt();
System.out.println("Enter the operation:");
System.out.println("1 for Addition");
System.out.println("2 for Subtraction");
System.out.println("3 for Multiplication");
System.out.println("4 for Division");
char choice = s.next().charAt(0);
String result = "The result of the operation is ";
switch(choice){
case '1':
result+= (a+b);
break;
case '2':
result += (a-b);
break;
case '3':
result += (a*b);
break;
case '4':
result += ((float)a/b);
break;
default:
result = "Invalid Input";
}
System.out.println(result);
}
Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Problem Statement 5:
WAP to find grade of student from input marks using if else ladder and switch
case.
Code:
(a)
import java.util.Scanner;
class Main {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter marks:");
int marks = scanner.nextInt();
String output = "Your grade is ";
if(marks>100)
output = "Invalid marks";
else if(marks>=90)
output+= "A";
else if(marks>=80)
output += "B";
else if(marks>=70)
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
output += "C";
else if(marks>=60)
output+= "D";
else if(marks>=50)
output+= "E";
else
output+= "F";
System.out.println(output);
}
}
(b)
import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter marks:");
int marks = scanner.nextInt();
String output = "Your grade is ";
switch(marks/10){
case 10:
case 9:
output += "A";
break;
case 8:
output += "B";
break;
case 7:
output += "C";
break;
case 6:
output += "D";
break;
case 5:
output += "E";
break;
case 4:
case 3:
case 2:
case 1:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
case 0:
output += "F";
break;
default:
output = "Invalid marks";
}
System.out.println(output);
}
}
Output :
(a)
(b)
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Problem Statement 6:
Code:
import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter n:");
int n = scanner.nextInt();
double sum = 0;
for(int i = 1;i<=n;i++){
sum += 1.0/(i*i);
}
System.out.println("The value of the series is " + sum);
}
}
Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Problem Statement 7:
Code:
(a)
class Main{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
int i,j;
for(i = 1;i<=n;i++){
if(i%2==0)
for(j = i;j>=1;j--)
System.out.print(j + " ");
else
for(j = 1;j<=i;j++)
System.out.print(j + " ");
System.out.println();
}
}
}
(b)
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
class Main{
c=(char)((i+1)*(i+2)/2+64);
System.out.println();
}
}
}
Output:
(a)
(b)
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
CONCLUSION:
We have successfully studied and implemented java control statement, loops and command
line arguments
EXPERIMENT NO. 2
Aim/ Objective :
To implement Arrays
Program/ Code:
Problem Statement 1:
You have been given an array of positive integers A1, A2,...,An with length N and
you have to print an array of same length (N) where the values in the new array are
the sum of every number in the array, except the number at that index.
i/p 1 2 3 4
For the 0th index, the result will be 2+3+4= 9, similarly for the second, third and
fourth index the corresponding results will be 8, 7 and 6 respectively.
i/p 4 5 6
o/p 11 10 9
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Code:
import java.util.Scanner;
class Main{
int sum = 0;
for(int i = 0;i<nums.length;i++){
nums[i] = s.nextInt();
sum += nums[i];
for(int i = 0;i<nums.length;i++)
sumarray[i] = sum-nums[i];
for(int n : sumarray)
System.out.println();
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Output:
Problem Statement 2:
Code:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
import java.util.Scanner;
class Main{
int size = 5;
int highest = 0;
for(int i = 0;i<size;i++){
rollno[i]=s.nextInt();
sub1[i]=s.nextInt();
sub2[i]=s.nextInt();
sub3[i]=s.nextInt();
for(int i = 0;i<size;i++){
total[i] = sub1[i]+sub2[i]+sub3[i];
if(total[i]>total[highest])
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
highest = i;
System.out.println("\nData entered");
System.out.println("Rollno\tSub1\tSub2\tSub3\tTotal");
for(int i = 0;i<size;i++)
}}
Output:
Problem Statement 3:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
WAP to display following pattern using irregular arrays (jagged arrays).
1
12
123…
Code:
import java.util.Scanner;
class Main{
for(int i = 1;i<=row;i++){
for(int j = 1;j<=i;j++){
arr[i-1][j-1] = j;
for(int i = 0;i<row;i++){
for(int j = 0;j<=i;j++){
System.out.print(arr[i][j]+" ");
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
System.out.println();
}}
Output:
CONCLUSION:
We have successfully studied and implemented concept of java arrays and jagged arrays.
EXPERIMENT NO. 3
AIM / OBJECTIVE:
To implement Strings
PROGRAM/CODE:
Problem Statement 1:
WAP to find out number of uppercase & lowercase characters, blank spaces and
digits from the string
Code:
import java.util.Scanner;
class Main{
System.out.print("Enter a string:");
String s = scanner.nextLine();
char c = s.charAt(i);
upper++;
lower++;
digits++;
blank++;
Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Problem Statement 2:
Code:
import java.util.Scanner;
class Main{
System.out.print("Enter a string:");
String s = scanner.nextLine();
char c = scanner.next().charAt(0);
int count = 0, i = 0;
while(s.indexOf(c,i) != -1){
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
count++;
i = s.indexOf(c,i)+1;
Output:
Problem Statement 3:
Code:
import java.util.Scanner;
class Main{
System.out.print("Enter a string:");
sb.reverse();
if(word.equalsIgnoreCase(sb.toString()))
else
Output:
CONCLUSION:
We have successfully studied and implemented concept of java Strings and its
inbuilt functions
EXPERIMENT NO. 4
AIM / OBJECTIVE:
PROGRAM/CODE:
Problem Statement 1:
WAP to accept students name from command line and store them in vector.
Code:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
import java.util.*;
class Main{
names.addElement(name);
System.out.println("Entered names:");
Enumeration en = names.elements();
while(en.hasMoreElements()){
System.out.println(en.nextElement());
Output:
Problem Statement 2:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
WAP to add n strings in a vector array. Input new string and check if it is present
in the vector. If present delete it else add to the vector
Code:
import java.util.*;
class Main{
scanner.nextLine();
System.out.println("Enter strings:");
for(int i = 0;i<length;i++){
strings.addElement(scanner.nextLine());
String s = scanner.nextLine();
if(strings.contains(s)){
strings.removeElementAt(firstindex);
while(strings.contains(s)){
strings.removeElementAt(strings.indexOf(s));
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}
else{
strings.addElement(s);
System.out.println("Updated set:");
Enumeration en = strings.elements();
while(en.hasMoreElements()){
System.out.println(en.nextElement());
Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
CONCLUSION:
EXPERIMENT NO. 5
AIM / OBJECTIVE:
To implement class with members and methods (static, non-static, recursive and
overloaded methods)
PROGRAM/CODE:
Problem Statement 1:
Create a class employee with data member’s empid, empname, designation and
salary. Write methods getemployee() to take user input, showgrade() to display
grade of employee based on salary, showemployee() to display details of
employee.
Code:
import java.util.Scanner;
class Main{
em.getEmployee();
em.showEmployee();
em.showGrade();
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}
class Employee{
empname = s.nextLine();
empid = s.nextLine();
designation = s.nextLine();
salary = s.nextFloat();
if(salary>500000)
System.out.println("Grade: A");
else if(salary>100000)
System.out.println("Grade: B");
else
System.out.println("Grade: C");
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}
System.out.println("Employee details:");
Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Problem Statement 2:
WAP to display area of square and rectangle using the concept of overloaded
functions
Code:
import java.util.Scanner;
class Main{
displayArea(len);
displayArea(rlen,rbre);
}}
Output:
Problem Statement 3:
Code:
import java.util.Scanner;
class Main{
float x = s.nextFloat();
int n = s.nextInt();
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
static float pow(float x,int y){
if(y==0)
return 1;
return x*pow(x,y-1);
Output:
Problem Statement 4:
WAP to count the number of objects made of a particular class using static
variable and static method and display the same.
Code:
class Main{
for(int i = 0;i<args.length;i++){
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
strings[i] = new StringWrapper(args[i]);
strings[i].count();
StringWrapper.showCount();
System.out.println("Value of StringWrappers:");
for(StringWrapper s : strings){
System.out.println(s);
class StringWrapper{
StringWrapper(String s){
string = s;
return string;
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
public static void count(){
count++;
Output:
CONCLUSION:
EXPERIMENT NO. 6
AIM / OBJECTIVE:
PROGRAM/CODE:
Problem Statement 1:
WOOP to arrange the names of students in descending order of their total marks,
input data consists of students details such as names, ID.no, marks of maths,
physics, chemistry. (Use array of objects)
Code:
import java.util.Scanner;
class Main {
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
public static void main(String args[]) {
for(int i = 0;i<students.length;i++){
students[i].input();
for(int i =0;i<students.length-1;i++){
for(int j = 0;j<students.length-1;j++){
if(students[j].getTotal()<students[j+1].getTotal()){
students[j] = students[j+1];
students[j+1] = temp;
System.out.println("Data Set:");
Student.printFormat();
System.out.println(student);
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}
class Student {
return total;
System.out.print("Enter name:");
name = scanner.nextLine();
System.out.print("Enter ID no:");
idno = scanner.nextInt();
phy = scanner.nextInt();
maths = scanner.nextInt();
chem = scanner.nextInt();
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
total = phy + chem + maths;
scanner.nextLine();
System.out.println();
System.out.println("Name\tID\tPhy\tChem\tMaths\tTotal");
String s = name + "\t" + idno + "\t" + phy + "\t" + chem + "\t" + maths + "\t" + total;
return s;
Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Problem Statement 2:
Code:
import java.util.Scanner;
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
public class Main {
switch (scanner.next().charAt(0)) {
case '1':
System.out.print("Addition: ");
System.out.println(a.add(b));
break;
case '2':
System.out.print("Subraction: ");
System.out.println(a.sub(b));
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
break;
case '3':
System.out.print("Multiplition: ");
System.out.println(a.multiply(b));
break;
case '4':
System.out.print("Division: ");
System.out.println(a.divide(b));
break;
default:
System.out.println("Invalid Input");
break;
scanner.close();
class Complex {
private float x;
private float y;
this.y = img;
return this.x;
return this.y;
String temp;
if (this.y < 0) {
} else {
return temp;
Output:
CONCLUSION:
EXPERIMENT NO. 7
AIM / OBJECTIVE:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
To implement Constructors and constructor overloading
PROGRAM/CODE:
Problem Statement 1:
Code:
import java.util.Scanner;
class Main{
class Count{
Count(){
count++;
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Output:
Problem Statement 2:
WAP to display area of square and rectangle using the concept of overloaded
constructor (use parameterized, non-parameterized and copy constructor)
Code:
import java.util.Scanner;
class Main{
System.out.println("Enter length:");
System.out.println(square);
System.out.println(rect);
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
System.out.println("Square copy:");
System.out.println(copysquare);
class Shape{
Shape(){}
Shape(Shape copy){
this.length = copy.length;
this.breadth = copy.breadth;
this.area = copy.area;
Shape(double length){
this.length = length;
this.length = length;
this.breadth = breadth;
Output:
CONCLUSION:
We have successfully studied and implemented the concept of constructor and constructor
overloading
EXPERIMENT NO. 8
AIM / OBJECTIVE:
PROGRAM/CODE:
Problem Statement 1:
WAP to demonstrate the role of Constructors in inheritance in the following class diagram.
Code:
class test{
C c = new C();
class A{
int a = 0;
A(){
System.out.println("From A");
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}
class B extends A {
int a = super.a + 1;
int b = 0;
B(){
System.out.println("From B");
class C extends B {
int a = super.a + 1;
int b = super.b + 1;
int c = 0;
C(){
System.out.println("From C");
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
OUTPUT:
Problem Statement 2:
WAP to create a super class having a variable. Let the variable be initialized to some value within a
constructor. This class should have a method display () to display the initial value of the variable.
Derive a sub class that accesses the constructor, variable and method of the super class using super
keyword.
Code:
class test{
s.display();
class SuperClass{
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
int x;
SuperClass(){
x = 234567;
SubClass(){
super();
super.display();
Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Problem Statement 3:
Display data of the specialized classes given in the following class diagram.
Code:
import java.util.Scanner;
class test {
System.out.println("----------------------------------------------");
t.read();
System.out.println("----------------------------------------------");
r.read();
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
System.out.println("----------------------------------------------");
c.read();
System.out.println("----------------------------------------------");
o.read();
System.out.println("----------------------------------------------");
System.out.println();
System.out.println("Details");
System.out.println();
System.out.println("----------------------------------------------");
t.display();
System.out.println("----------------------------------------------");
r.display();
System.out.println("----------------------------------------------");
c.display();
System.out.println("----------------------------------------------");
o.display();
System.out.println("----------------------------------------------");
class Staff {
name = scanner.nextLine();
code = scanner.nextInt();
subject = scanner.next();
exp = scanner.nextFloat();
scanner.nextLine();
System.out.println("Teacher Details:");
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}
name = scanner.nextLine();
code = scanner.nextInt();
sal = scanner.nextFloat();
speed = scanner.nextFloat();
exp = scanner.nextFloat();
scanner.nextLine();
name = scanner.nextLine();
code = scanner.nextInt();
daily_wages = scanner.nextFloat();
speed = scanner.nextFloat();
exp = scanner.nextFloat();
scanner.nextLine();
name = scanner.nextLine();
code = scanner.nextInt();
scanner.nextLine();
dept = scanner.nextLine();
grade = scanner.nextLine();
System.out.println("Officer Details:");
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
System.out.println("Name: " + name);
Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
CONCLUSION:
We have successfully studied and implemented concept of inheritance and super keyword.
EXPERIMENT NO. 9
AIM / OBJECTIVE:
PROGRAM/CODE:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Problem Statement 1:
Design an interface with a method reversal. This method takes a string as input and returns the
reversed string. Create a class that implements the above interface.
Code:
interface StringManipulation {
@Override
reversedString.append(input.charAt(i));
return reversedString.toString();
}}
Output:
Problem Statement 2:
WAP to implement three classes namely Student, Test and Result. Student class has member as
rollno, and read(). Test class has members as sem1_marks and sem2_marks and read(). Result class
has member as total. Create an interface named sports that has a member score (). Derive Test class
from Student and Result class has multiple inheritances from Test and Sports. Total is formula based
on sem1_marks, sem2_mark and score. Use super keyword.
Code:
import java.util.*;
class Student {
public Student() {
this.Name = "";
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
public Student(String name) {
this.Name = name;
public Exam() {
super();
this.m1 = 0;
this.m2 = 0;
this.m3 = 0;
super(name);
this.m1 = m1;
this.m2 = m2;
this.m3 = m3;
this.m1 = sc.nextInt();
this.m2 = sc.nextInt();
this.m3 = sc.nextInt();
interface Sports {
int m = 10;
void IP();
public Result() {
super();
this.total = 0;
this.percent = 0.0;
this.total = 0;
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
this.percent = 0.0;
int ip;
ip = sc.nextInt();
total = m1 + m2 + m3;
class test {
r.accept();
r.calc();
r.IP();
r.display();
Output:
Conclusion:
EXPERIMENT NO. 10
AIM / OBJECTIVE:
PROGRAM/CODE:
Problem Statement 1:
Demonstrate using a suitable example that a base class reference variable can point to a child class
object or a base class object using the concept of dynamic method dispatch (dynamic
polymorphism).
Code:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
class Animal {
}}
System.out.println("Dog barks");
}}
System.out.println("Cat meows");
}}
animal1.sound();
animal2.sound();
}}
Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Problem Statement 2:
Adwita , 8th Grade student wants to write a functions to calculate simple interest, compound
interest. She wants to keep same (final) rate of interest for every input of principal and time. She
wants to ensure that the declared functions are not overridden in any subclasses and the class is not
inherited by any other class. Help her to declare the variables methods and classes and write the
code for the same using final keyword.
Code:
this.rate = rate;
double time = 2;
Output:
Problem Statement 3:
WAP to create an object of a class, delete the same object by calling System. gc () and display a
message that the “object has been deleted”.
Code:
class MyClass {
public MyClass() {
System.out.println("Object created");
super.finalize();
Output:
Conclusion:
EXPERIMENT NO. 11
AIM / OBJECTIVE:
PROGRAM/CODE:
Problem Statement 1:
Code:
double radius;
Circle(double radius) {
this.radius = radius;
double calculateArea() {
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}
this.length = length;
this.width = width;
double calculateArea() {
this.base = base;
this.height = height;
double calculateArea() {
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
public class test {
Output:
Problem Statement 2:
WAP to create a package called vol having Cylinder class and volume (). WAP that imports this
package to calculate volume of a Cylinder.
Code:
package vol;
import vol.Cylinder;
Output:
Conclusion:
From above examples we learnt to implement exceptions in Java (read input using DataInputStream/
BufferedReader classes).
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
EXPERIMENT NO. 12
AIM / OBJECTIVE:
PROGRAM/CODE:
Problem Statement 1:
Code:
import java.io.IOException;
try {
Class.forName("NonExistentClass");
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
} catch (ClassNotFoundException e) {
try {
} catch (IOException e) {
try {
} catch (NumberFormatException e) {
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
try {
} catch (ArrayIndexOutOfBoundsException e) {
try {
} catch (NullPointerException e) {
checkedExceptionDemo();
checkedIOExceptionDemo();
uncheckedNumberFormatExceptionDemo();
uncheckedArithmeticExceptionDemo();
uncheckedArrayIndexOutOfBoundsExceptionDemo();
uncheckedNullPointerExceptionDemo();
Output:
Problem Statement 2:
Write a Java Program to Create a User Defined Exception class MarksOutOfBoundsException, If
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Entered marks of any subject is greater than 100 or less than 0, and then program should create a
user defined Exception of type MarksOutOfBoundsException and must have a provision to handle it.
Code:
super(message);
try {
validateMarks(marks);
} catch (MarksOutOfBoundsException e) {
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}
Output:
Conclusion:
We have successfully studied and implemented program to throw and handle exceptions in
java.
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
EXPERIMENT NO. 13
AIM / OBJECTIVE:
To implement Multithreading.
PROGRAM/CODE:
Problem Statement 1:
Write a multithreaded program a java program to print Table of Five, Seven and Thirteen using
Multithreading (Use Thread class for the implementation).
Code:
import java.util.*;
for(int i=1;i<=10;i++){
try{
System.out.println(5*i);
Thread.sleep(1000);}
catch(Exception e){
System.out.print(e);}
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}}
for(int i=1;i<=10;i++){
try{
System.out.println(7*i);
Thread.sleep(1000);}
catch(Exception e){
System.out.print(e);}
}}
for(int i=1;i<=10;i++){
try{
System.out.println(13*i);
Thread.sleep(1000);}
catch(Exception e){
System.out.println(e);}
}}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
class test{
t1.start();
t2.start();
t3.start();
}}
Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Problem Statement 2:
Code:
import java.util.*;
if(Thread.currentThread().getName().equals("slash")){
for(int i=1;i<=8;i++){
try{
System.out.print("/");
Thread.sleep(1000);}
catch(Exception e){
System.out.println(e);}
}}
else if(Thread.currentThread().getName().equals("star")){
for(int i=0;i<8;i++){
try{
System.out.print("*");
Thread.sleep(1000);}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
catch(Exception e){
System.out.print(e);}
}}
}}
class test{
t1.setName("slash");
t2.setName("star");
t1.start();
t2.start();
}}
Output:
Problem Statement 3:
Write a program to demonstrate thread methods: wait notify suspend resume join setpriority
getpriority setname getname
Code:
super(name);
synchronized(this) {
try {
Thread.sleep(2000);
wait();
} catch (InterruptedException e) {
e.printStackTrace();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
t1.setPriority(Thread.MAX_PRIORITY);
t1.suspend();
System.out.println("Thread 1 is suspended.");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
t1.resume();
System.out.println("Thread 1 is resumed.");
Output:
Problem Statement 4:
Write a multithreaded program that generates the Fibonacci sequence. This program should work as
follows: create a class Input that reads the number of Fibonacci numbers that the program is to
generate. The class will then create a separate thread that will generate the Fibonacci numbers,
placing the sequence in an array. When the thread finishes execution, the parent thread (Input class)
will output the sequence generated by the child thread. Because the parent thread cannot begin
outputting the Fibonacci sequence until the child thread finishes, the parent thread will have to wait
for the child thread to finish.
Code:
import java.util.*;
FibThread(int n){
no=n;
synchronized(this){
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
fib=new int[no];
fib[0]=fib1;
fib[1]=fib2;
for(int i=2;i<no;i++)
try{
fib3=fib1+fib2;
fib[i]=fib3;
fib1=fib2;
fib2=fib3;
System.out.println("...");
Thread.sleep(1000);
catch(Exception e){
System.out.print(e);
notify();
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
public void display(int n){
for(int i=0;i<n;i++)
System.out.println(fib[i]);
class test{
int n;
n=sc.nextInt();
f.start();
synchronized(f){
try{
f.wait();
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}
catch(Exception e){
System.out.println(e.getMessage());
f.display(n);
}}
Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Problem Statement 5:
WAP to prevent concurrent booking of a ticket using the concept of thread synchronization.
Code:
import java.util.*;
class TicketBooking{
int fare;
try{
fare=sc.nextInt();
for(int i=0;i<8;i++){
System.out.print("...\n");
Thread.sleep(500);
catch(Exception e){
System.out.println(e);
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}
}}
TicketBooking ticket;
Passenger(TicketBooking ticket){
this.ticket= ticket;
ticket.bookTicket();
}}
class test{
p1.start();
p2.start();
}}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Output:
Conclusion:
We have successfully studied and implemented program of Multithreading.
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
EXPERIMENT NO. 14
AIM / OBJECTIVE:
PROGRAM/CODE:
Problem Statement 1:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public test() {
// Text labels
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
loginLabel = new JLabel("Login:");
// Buttons
panel.add(loginLabel);
panel.add(loginField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(outputLabel);
panel.add(outputField);
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
panel.add(OKButton);
panel.add(resetButton);
add(panel);
OKButton.addActionListener(this);
resetButton.addActionListener(this);
if (e.getSource() == OKButton) {
outputLabel.setVisible(true);
outputField.setVisible(true);
loginField.setText("");
passwordField.setText("");
outputField.setText("");
outputLabel.setVisible(false);
outputField.setVisible(false);
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}
frame.setTitle("Registration Form");
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Problem Statement 2:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
new calculator();
JTextField t1;
JPanel panel;
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
char operator;
calculator(){
super("Calculator");
this.setSize(420,550);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setLayout(null);
this.setResizable(false);
// this.getContentPane().setBackground(Color.black);
t1 = new JTextField();
t1.setBounds(50,25,300,50);
t1.setFont(myFont);
t1.setEditable(false);
functionButtons[0] = add;
functionButtons[1] = sub;
functionButtons[2] = mul;
functionButtons[3] = div;
functionButtons[4] = dec;
functionButtons[5] = solve;
functionButtons[6] = del;
functionButtons[7] = clr;
functionButtons[8] = neg;
functionButtons[i].setFont(myFont);
functionButtons[i].setFocusable(false);
functionButtons[i].addActionListener(this);
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}
numberButtons[i].setFocusable(false);
numberButtons[i].setFont(myFont);
numberButtons[i].addActionListener(this);
neg.setBounds(50,430,100,50);
del.setBounds(150,430,100,50);
clr.setBounds(250,430,100,50);
panel.setBounds(50,100,300,300);
panel.setLayout(new GridLayout(4,4,10,10));
panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(add);
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(sub);
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(mul);
panel.add(dec);
panel.add(numberButtons[0]);
panel.add(solve);
panel.add(div);
this.add(panel);
this.add(neg);
this.add(del);
this.add(clr);
this.add(t1);
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
this.setVisible(true);
if (e.getSource() == numberButtons[i]) {
t1.setText(t1.getText().concat(String.valueOf(i)));
if(e.getSource() == dec){
t1.setText(t1.getText().concat(String.valueOf(".")));
if(e.getSource() == neg){
temp *= -1;
t1.setText(String.valueOf(temp));
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
if(e.getSource() == add){
num1 = Double.parseDouble(t1.getText());
operator = '+';t1.setText("");
if(e.getSource() == sub){
num1 = Double.parseDouble(t1.getText());
operator = '-';
t1.setText("");
if(e.getSource() == mul){
num1 = Double.parseDouble(t1.getText());
operator = '*';
t1.setText("");
if(e.getSource() == div){
num1 = Double.parseDouble(t1.getText());
operator = '/';
t1.setText("");
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}
if(e.getSource() == solve){
num2 = Double.parseDouble(t1.getText());
switch(operator){
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}
t1.setText(String.valueOf(result));
if(e.getSource() == clr){
t1.setText("");
if(e.getSource() == del){
t1.setText("");
t1.setText(t1.getText() + str.charAt(i));
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Output:
Problem Statement 3:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
{
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
JLabel name,gen,interest,place,details;
JTextField tname;
JComboBox cplace;
JButton submit,exit;
JTextArea tadd;
JCheckBox music,swim;
ButtonGroup gengp;
public Form()
setTitle("Form");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
setLayout(null);
name.setSize(100, 20);
name.setLocation(100, 100);
add(name);
tname.setSize(190, 20);
tname.setLocation(200, 100);
add(tname);
gen.setSize(100, 20);
gen.setLocation(100, 150);
add(gen);
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
male.setSelected(true);
male.setSize(80, 20);
male.setLocation(200, 150);
add(male);
female.setSize(80, 20);
female.setLocation(350, 150);
add(female);
gengp.add(male);
gengp.add(female);
interest.setLocation(100, 200);
add(interest);
music.setSize(100, 20);
music.setLocation(200, 200);
add(music);
swim.setSize(100, 20);
swim.setLocation(350, 200);
add(swim);
place.setSize(100, 20);
place.setLocation(100, 250);
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
add(place);
cplace.setSize(150, 20);
cplace.setLocation(200, 250);
add( cplace);
cplace.addItem("India");
cplace.addItem("USA");
cplace.addItem("Bhutan");
cplace.addItem("Srilanka");
cplace.addItem("Maldives");
details.setSize(100, 20);
details.setLocation(100, 300);
add(details);
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
tadd.setSize(200, 200);
tadd.setLocation(200, 300);
tadd.setLineWrap(true);
tadd.setEditable(false);
add(tadd);
submit.setSize(100, 20);
submit.setLocation(150, 550);
submit.addActionListener(this);
add(submit);
exit.setSize(100, 20);
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
exit.setLocation(270, 550);
exit.addActionListener(this);
add(exit);
setVisible(true);
if(e.getSource() == submit)
(swim.isSelected()==true?"Swimming":"") +"\n" +
tadd.setText(data);
//tadd.setEditable(false);
tadd.setText("");
class test {
Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Conclusion:
We have successfully studied and implemented the programs to run GUI applications using
Java Swing.