0% found this document useful (0 votes)
3 views

Prog1-36 Java

The document contains multiple Java programming assignments focusing on various concepts such as multithreading, synchronization, exception handling, and inheritance. Each program includes a code snippet, its aim, and expected output. The assignments are part of a Performance Analysis of Programming Languages Lab.

Uploaded by

Deepanshi Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Prog1-36 Java

The document contains multiple Java programming assignments focusing on various concepts such as multithreading, synchronization, exception handling, and inheritance. Each program includes a code snippet, its aim, and expected output. The assignments are part of a Performance Analysis of Programming Languages Lab.

Uploaded by

Deepanshi Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

ANKUR GOEL 11232546 GROUP – A3

PROGRAM NO – 26

AIM : WAP to achieve multithreading using run(), yield(), sleep() and start()
in JAVA.

CODE :
class MyRunnable implements Runnable {
private String t1;
public MyRunnable(String name) {
this.t1 = name;
}

@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(t1 + " - Count: " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(t1 + " interrupted.");
}
Thread.yield();
}
}
}

public class MultithreadingExample {


public static void main(String[] args) {
MyRunnable runnable1 = new MyRunnable("Thread 1");
MyRunnable runnable2 = new MyRunnable("Thread 2");

Thread thread1 = new Thread(runnable1);


Thread thread2 = new Thread(runnable2);

thread1.start();
thread2.start();

try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}

System.out.println("Main thread finished.");


}
}

BCSE – 506L Performance Analysis of Programming Languages Lab 30


ANKUR GOEL 11232546 GROUP – A3

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 31


ANKUR GOEL 11232546 GROUP – A3

PROGRAM NO – 27

AIM : WAP to book movie ticket using Synchronization in JAVA.

CODE :
class BookTicket {
int total_seats = 10;
synchronized void bookTicket(int seats) {
if (total_seats >= seats) {
System.out.println("Seat booked successfully");
total_seats = total_seats - seats;
System.out.println("Seats left: " + total_seats);
}
else {
System.out.println("Not enough seats available");
}
}
}
public class MovieTicket extends Thread {
static BookTicket b;
int seats;
public MovieTicket(int seats) {
this.seats = seats;
}
public void run() {
b.bookTicket(seats);
}
public static void main(String[] args) {
b = new BookTicket();
MovieTicket t1 = new MovieTicket(7);
t1.start();
MovieTicket t2 = new MovieTicket(10);
t2.start();
}
}
OUTPUT :

BCSE – 506L Performance Analysis of Programming Languages Lab 32


ANKUR GOEL 11232546 GROUP – A3

PROGRAM NO – 28

AIM : WAP to solve Producer Consumer problem in JAVA.

CODE :
import java.util.LinkedList;
public class Threadexample {
public static void main(String[] args)
throws InterruptedException{
final PC pc = new PC();

Thread t1 = new Thread(new Runnable() {


@Override
public void run(){
try {
pc.produce();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
});

Thread t2 = new Thread(new Runnable() {


@Override
public void run(){
try {
pc.consume();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
});

t1.start();
t2.start();

t1.join();
t2.join();
}

public static class PC {


LinkedList<Integer> list = new LinkedList<>();
int capacity = 2;

public void produce() throws InterruptedException{

BCSE – 506L Performance Analysis of Programming Languages Lab 33


ANKUR GOEL 11232546 GROUP – A3

int value = 0;
while (true) {
synchronized (this){
while (list.size() == capacity)
wait();

System.out.println("Producer produced-"+ value);


list.add(value++);
notify();
Thread.sleep(1000);
}
}
}

public void consume() throws InterruptedException{


while (true) {
synchronized (this){
while (list.size() == 0)
wait();
int val = list.removeFirst();

System.out.println("Consumer consumed-"+ val);


notify();
Thread.sleep(1000);
}
}
}
}
}
OUTPUT :

BCSE – 506L Performance Analysis of Programming Languages Lab 34


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. - 23
AIM: To implement single try-catch block in JAVA.

CODE:
class Error {
public static void main(String[] args) {
int a = 10;
int b = 5;
int c = 5;
int x,y;
try{
x = a/(b-c);
}
catch(ArithmeticException e){
System.out.println("Division by zero");
}
y = a/(b+c);
System.out.println(+y);
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 27


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. - 24
AIM: To implement multiple catch blocks in JAVA.

CODE:
class Error {
public static void main(String[] args) {
int a[] = {10,5};
int b = 5;
int c;
try{
c = a[2]/a[1]-b;
int d = a[1]/a[0]-b;
}
catch(ArithmeticException e){
System.out.println("Arithmetic Exception");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Array Index Out Of Bound");
}
finally{
System.out.println("Exception Caught");
}
c = a[1]/(a[0]+b);
System.out.println(+c);
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 28


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. - 25
AIM: To implement throwing your own exception in JAVA.

CODE:
// Define the custom exception class
class MyException extends Exception {
MyException(String message) {
super(message); // Call the base class constructor with the message
}
}

// Separate class with the main method


public class TestMyException {
public static void main(String[] args) {
int x = 5, y = 1000;
try {
float z = (float) x / (float) y;
if (z < 0.01) {
throw new MyException("Number is too small");
}
System.out.println("Result: " + z); // Print result to see the output if no exception is
thrown
}
catch (MyException e) {
System.out.println("Caught MyException");
System.out.println(e.getMessage());
}
finally {
System.out.println("I am always here");
}
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 29


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. – 17
AIM: W.A.P to use String and StringBuffer Classes.

CODE:
public class StringAndStringBufferClass {
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2
sb.ensureCapacity(10);//now no change
System.out.println(sb.capacity());//now 34
sb.ensureCapacity(50);//now (34*2)+2
System.out.println(sb.capacity());//now 70
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 18


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. – 18
AIM: W.A.P. to use Wrapper Classes in JAVA.

CODE:
public class WrapperClass {
public static void main(String[] args) {
byte b=10;
short s=20;
int i=30;
long l=40;
float f=50.0F;
double d=60.0D;
char c='a';
boolean b2=true;

//Autoboxing: Converting primitives into objects


Byte byteobj=b;
Short shortobj=s;
Integer intobj=i;
Long longobj=l;
Float floatobj=f;
Double doubleobj=d;
char charobj=c;
Boolean boolobj=b2;

//Printing objects
System.out.println("---Printing object values---");
System.out.println("Byte object: "+byteobj);
System.out.println("Short object: "+shortobj);
System.out.println("Integer object: "+intobj);
System.out.println("Long object: "+longobj);
System.out.println("Float object: "+floatobj);
System.out.println("Double object: "+doubleobj);
System.out.println("Character object: "+charobj);
System.out.println("Boolean object: "+boolobj);

//Unboxing: Converting Objects to Primitives


byte bytevalue=byteobj;
short shortvalue=shortobj;
int intvalue=intobj;
long longvalue=longobj;
float floatvalue=floatobj;
double doublevalue=doubleobj;
char charvalue=charobj;
boolean boolvalue=boolobj;

//Printing primitives

BCSE – 506L Performance Analysis of Programming Languages Lab 19


DEEPANSHI GUPTA 11232602 GROUP – A3

System.out.println("---Printing primitive values---");


System.out.println("byte value: "+bytevalue);
System.out.println("short value: "+shortvalue);
System.out.println("int value: "+intvalue);
System.out.println("long value: "+longvalue);
System.out.println("float value: "+floatvalue);
System.out.println("double value: "+doublevalue);
System.out.println("char value: "+charvalue);
System.out.println("boolean value: "+boolvalue);
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 20


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. – 19
AIM: W.A.P. to perform operations on Vector Class.

CODE:
import java.util.Vector;
public class VectorOperations {
public static void main(String[] args) {
//Create an empty vector with initial capacity 4
Vector<String> vec = new Vector<String>(4);

//Adding elements to a vector


vec.add("Tiger");
vec.add("Lion");
vec.add("Dog");
vec.add("Elephant");

//Check size and capacity


System.out.println("Size is: "+vec.size());
System.out.println("Default capacity is: "+vec.capacity());

//Display Vector elements


System.out.println("Vector element is: "+vec);
vec.addElement("Rat");
vec.addElement("Cat");
vec.addElement("Deer");

//Again check size and capacity after two insertions


System.out.println("Size after addition: "+vec.size());
System.out.println("Capacity after addition is: "+vec.capacity());

//Display Vector elements again


System.out.println("Elements are: "+vec);

//Checking if Tiger is present or not in this vector


if(vec.contains("Tiger")){
System.out.println("Tiger is present at the index " +vec.indexOf("Tiger"));
}
else{
System.out.println("Tiger is not present in the list.");
}

//Get the first element


System.out.println("The first animal of the vector is = "+vec.firstElement());

//Get the last element


System.out.println("The last animal of the vector is = "+vec.lastElement());
}

BCSE – 506L Performance Analysis of Programming Languages Lab 21


DEEPANSHI GUPTA 11232602 GROUP – A3

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 22


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. – 20
AIM: JAVA program to initialize the values from one object to another
object.

CODE:
public class Student {
int id;
String name;

// Constructor to initialize integer and string


Student(int i, String n){
id = i;
name = n;
}

// Copy constructor to initialize another object


Student(Student s){
id = s.id;
name = s.name;
}

void display(){
System.out.println(id + " " + name);
}

public static void main(String[] args){


Student s1 = new Student(111, "Karan");
Student s2 = new Student(s1);

s1.display();
s2.display();
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 23


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. – 21
AIM: W.A.P. to achieve Multiple Inheritance in JAVA.

CODE:
interface Character {
void attack();
}

interface Weapon {
void use();
}

class Warrior implements Character, Weapon {


@Override
public void attack() {
System.out.println("Warrior attacks with a sword.");
}

@Override
public void use() {
System.out.println("Warrior uses a sword.");
}
}

class Mage implements Character, Weapon {


@Override
public void attack() {
System.out.println("Mage attacks with a wand.");
}

@Override
public void use() {
System.out.println("Mage uses a wand.");
}
}

public class MultipleInheritance {


public static void main(String[] args) {
Warrior warrior = new Warrior();
Mage mage = new Mage();

warrior.attack(); // Output: Warrior attacks with a sword.


warrior.use(); // Output: Warrior uses a sword.

mage.attack(); // Output: Mage attacks with a wand.


mage.use(); // Output: Mage uses a wand.
}

BCSE – 506L Performance Analysis of Programming Languages Lab 24


DEEPANSHI GUPTA 11232602 GROUP – A3

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 25


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. – 22
AIM: W.A.P. to achieve Multi-level Inheritance.

CODE:
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
public class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 26


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. - 11
AIM: To calculate income tax in JAVA.

CODE:
import java.util.Scanner;
public class TaxCalculation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your income: ");
int n = sc.nextInt();
int tax;
if(n<=180000){
System.out.println("No Tax");
}
else if(n>180000 && n<=500000){
tax = (int) ((n - 180000)*0.10);
System.out.print("Tax = "+tax);
}
else if(n>500000 && n<=800000){
tax = (int) ((n - 500000)*0.20);
System.out.print("Tax = "+tax);
}
else{
tax = (int) ((n - 800000)*0.30 + (n - 500000)*0.20 + (n - 180000)*0.10);
System.out.print("Tax = "+tax);
}
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 12


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. – 13
AIM: W.A.P to calculate area of different shapes using method overloading.

CODE:
public class Area {
float l,b,a;
static int area(int l,int b){
return (l*b);
}
static int area(int a){
return(a*a);
}
double area(float l, float b){
return(0.5*l*b);
}
double area(float a){
return(0.433*a*a);
}
public static void main(String[] args) {
Area obj = new Area();
System.out.println("area = "+Area.area(2,3));
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 14


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. – 14
AIM: W.A.P. to sort a list of numbers.

CODE:
public class Sorting {
public static void main(String[] args)
{

int arr[] = { 10,54,6,97,24,11 };


for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
int temp = 0;
if (arr[j] < arr[i]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
System.out.print(arr[i] + " ");
}
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 15


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. – 15
AIM: W.A.P. to multiply 3×3 matrix.

CODE:
public class MatrixMultiplication {
public static void main(String args[]){
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int b[][]={{9,8,7},{6,5,4},{3,2,1}};

int c[][]=new int[3][3];

for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++){
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 16


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. – 16
AIM: W.A.P. to check whether a given string is palindrome or not.

CODE:
import java.util.Scanner;
public class Palindrome {
public static void main(String args[]){
int r,sum=0,temp;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a no.: ");
int n = sc.nextInt();

temp=n;
while(n>0){
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("Palindrome number ");
else
System.out.println("Not palindrome");
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 17


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. - 9
AIM: To print Fibonacci series in JAVA.

CODE:
import java.util.Scanner;
public class Fibonacci {

public static void main(String[] args) {

int a = 0;
int b = 1;
int i = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter no. of digits: ");
int n = sc.nextInt();
System.out.println(a);
System.out.println(b);
while(i<=n){
int nextTerm = a+b;
System.out.println(nextTerm);
a = b;
b = nextTerm;
i++;
}
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 10


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. - 10
AIM: To display multiplication table of a number in JAVA.

CODE:
import java.util.Scanner;
public class Table {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.print("Enter a no.: ");
int n = sc.nextInt();
int i = 1;
do{
int a = n*i;
System.out.print(n);
System.out.print("*");
System.out.print(i);
System.out.print(" = ");
System.out.println(a);
i++;
}
while(i<=10);
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 11


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. - 11
AIM: To calculate income tax in JAVA.

CODE:
import java.util.Scanner;
public class TaxCalculation {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.print("Enter your income: ");
int n = sc.nextInt();
if(n<=180000){
System.out.println("No Tax");
}
else if(n>180000 && n<=500000){
System.out.println("10% Tax");
}
else if(n>500000 && n<=800000){
System.out.println("20% Tax");
}
else{
System.out.println("30% Tax");
}
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 12


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. - 12
AIM: To convert a given number of eggs into the number of gross plus the
number of dozens plus the number of left over eggs in JAVA.

CODE:
import java.util.Scanner;
public class GrossAndDozens {

public static void main(String[] args) {

int eggs;
int gross;
int aboveGross;

int dozens;
int extras;

System.out.println("How many eggs do you have? ");


Scanner sc= new Scanner(System.in);
eggs = sc.nextInt();

gross = eggs / 144;


aboveGross = eggs % 144;

dozens = aboveGross / 12;


extras = aboveGross % 12;

System.out.print("Your number of eggs is ");


System.out.print(""+gross);
System.out.print(" gross, ");
System.out.print(""+dozens);
System.out.print(" dozen, and ");
System.out.print(""+extras);
System.out.println();
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 13


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. - 1
AIM: To print “hello” message in JAVA.

CODE:
class Hello{
public static void main(String[] args){
System.out.println("Hello Java");
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 1


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. - 2
AIM: To swap value of two variables without using third variable in JAVA.

CODE:
public class Swap {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("Before swapping");
System.out.println("a = " + a);
System.out.println("b = " + b);
a = a + b;
b = a - b;
a = a - b;
System.out.println("\nAfter swapping");
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 2


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. – 3
AIM: To swap value of two variables by using third variable in JAVA.

CODE:
public class Swap2 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c;
System.out.println("Before swapping");
System.out.println("a = " + a);
System.out.println("b = " + b);
c = a;
a = b;
b = c;
System.out.println("\nAfter swapping");
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 3


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. - 4
AIM: To check whether a number is even or odd in JAVA.

CODE:
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a no.: ");
int a = sc.nextInt();
if (a%2==0)
System.out.println("Even");
else
System.out.println("Odd");

}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 4


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. - 5
AIM: To check whether a year is leap year or not in JAVA.

CODE:
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a year: ");
int a = sc.nextInt();
if (a%4==0)
System.out.println("Leap Year");
else
System.out.println("Non-Leap Year");
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 5


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. - 6
AIM: To give grading to students according to the percentage in JAVA.

CODE:
public class Grading {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter percentage: ");
int a = sc.nextInt();
if (a>=60)
System.out.println("1st Division");
else if (a>=50)
System.out.println("2nd Division");
else if (a>=40)
System.out.println("3rd Division");
else
System.out.println("Fail");

}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 6


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. - 7
AIM: To implement switch statement in JAVA.

CODE:
import java.util.Scanner;
public class DaysOfWeek {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a no.: ");
int d = sc.nextInt();
switch(d){
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:

BCSE – 506L Performance Analysis of Programming Languages Lab 7


DEEPANSHI GUPTA 11232602 GROUP – A3

System.out.println("Saturday");
break;
default:
System.out.println("Invalid Day");

}
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 8


DEEPANSHI GUPTA 11232602 GROUP – A3

PROGRAM NO. - 8
AIM: To find factorial of a number in JAVA.

CODE:
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a no.: ");
float a = sc.nextInt();
float fact = 1;
for (int i = 1; i <= a; i++) {
fact = fact*i;
}
System.out.println(fact);
}
}

OUTPUT:

BCSE – 506L Performance Analysis of Programming Languages Lab 9

You might also like