Tanay Java MCA Sem 1
Tanay Java MCA Sem 1
Enrollment No : A010145023039
Programme : MCA-1(B)
Table of Contents:-
WAP of ExceptionExample1(Handle an
7 13-09-2023
exception).
WAP of to handle
11 18-09-2023
Exceptions using Multiple Catch.
WAP to do Demo
12 20-09-2023
Join(Thread Runnable).
WAP To Handle Exception Using Nested
13 Try. 20-09-2023
WAP of create multiple thread for
multiple tasks.
14 20-09-2023
WAP of use Multiple thread for Single
task.
15 20-09-2023
WAP to use single thread for single
16 task. 20-09-2023
Class DecToBin_1 {
public static void main(String[] args) {
int num = 12, rem, rev = 1, bin = 0;
while (num != 0) {
rem = num % 2;
bin = bin + rem * rev;
}
Output: -
Q3. Write a Program to convert Binary to Decimal.
class BinToDec_3 {
public static void main(String[] args) {
int bin = 11000, rem, dec = 0, a = 1;
while (bin != 0) {
rem = bin % 10;
dec = dec + rem * a;
a = a * 2;
bin = bin / 10;
}
System.out.println(dec);
}
}
Output: -
Q4. Write a Program to sort an array using Bubble sort.
class BubbleSort_4 {
public static void main(String[] args) {
int[] arr = { 10, 15, 12, 18, 94, 78, 32 };
int temp;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 1; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
Output: -
Q5. Write a Program to sort an array using Heap Sort.
import java.util.Scanner;
public class HeapSort_5 {
void print(int array[], int size) { int index = 0; while (index <
size) { System.out.print(" " + array[index]); index++; } }
void heapify(int arr[], int size, int index) { int maximum =
index;
int leftChild = 2 * index + 1;
int rightChild = 2 * index + 2;
int swapper;
if (leftChild < size && arr[leftChild] > arr[maximum]) {
maximum = leftChild;
}
if (rightChild < size && arr[rightChild] > arr[maximum]) {
maximum = rightChild;
}
if (maximum != index) {
swapper = arr[index];
arr[index] = arr[maximum];
arr[maximum] = swapper;
heapify(arr, size, maximum);
}
}
void sort(int array[]) {
int size = array.length;
int swapper;
int index = (size / 2) - 1;
while (index >= 0) {
heapify(array, size, index);
index--;
}
for (index = size - 1; index > 0; index--) {
swapper = array[0];
array[0] = array[index];
array[index] = swapper;
heapify(array, index, 0);
}
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int array[] = new int[10];
for (int i = 0; i < 10; i++) {
array[i] = in.nextInt();
}
int size = array.length;
HeapSort_5 object = new HeapSort_5();
object.sort(array);
System.out.println("After Heap Sort: ");
object.print(array, size);
}
}
Output: -
Q6. Write a Program to show Inheritance.
class Faculty {
String designation = "Professor";
String collegeName = "AIIT";
void does() {
System.out.println("Teaching");
}
}
void hacking() {
System.out.println("Hacking is my Passion");
}
class ExceptionEx2_8 {
public static void main(String[] args) throws InterruptedException {
Thread.sleep(1000);
System.out.println(10 / 2);
}
}
Output: -
Q9. Write a Program of Exception Handling Example 3.
class ExceptionEx4_10 {
public static void main(String [] args) {
try {
System.out.println(9 / 0);
} catch (Exception e) {
System.out.println(e);
System.out.println(e.getMessage());
e.printStackTrace();
}
System.out.println("Will this line be printed?");
}
}
Output: -
Q11. Write a Program of Exception Handling using Multiple Catches.
NewThread(String threadname) {
name = threadname; t = new
Thread(this, name);
System.out.println("New thread: " + t);
t.start();
}
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");
}
System.out.println(name + " Exiting.");
}
}
ob2.t.join(); ob3.t.join();
} catch (InterruptedException e) {
System.out.println("Main Thread Interrupted");
}
System.out.println("Thread one is Alive: " + ob1.t.isAlive());
System.out.println("Thread two is Alive: " + ob2.t.isAlive());
System.out.println("Thread three is Alive: " + ob3.t.isAlive());
System.out.println("Main Thread Exiting");
}
}
Output: -
Q13. Write a Program to Handle Exception using Nested try.
class NestedTry_13 {
public static void main(String[] args) {
try {
int a = args.length;
int b = 80 / a;
System.out.println("a = " + a);
try { if (a
== 1) { a=a/
(a - a);
}
if (a == 2) {
int c[] = { 1 };
c[20] = 45;
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out of bounds: " + e);
}
} catch (ArithmeticException e) {
System.out.println("Divide by 0: " + e);
}
}
}
Output: -
Q14. Write a Program to create Multiple thread for Multiple Task.
class MultiThreadMultiTask_14 {
public static void main(String[] args) {
class SingleThreadSingleTask_16 {
public static void main(String args[]) {
SingleThread1 t1 = new SingleThread1();
t1.start();
}
}
Output: -
Q17. Write a Program to create a Thread and use the setName, getName, activeCount,
setPriority & getPriority method.
}
}
Output: -
Q18. Write a Program to make 5 Different Colored Boxes and Display them Diagonally.
import java.applet.*; import
java.awt.*;
public class DiagonalBoxes_18 extends Applet {
public void paint(Graphics g) {
g.drawRect(10, 10, 100, 50);
g.setColor(Color.red);
g.fillRect(10, 10, 100, 50);
g.drawRect(110, 60, 100, 50);
g.setColor(Color.blue);
g.fillRect(110, 60, 100, 50);
g.drawRect(210, 110, 100, 50);
g.setColor(Color.yellow);
g.fillRect(210, 110, 100, 50);
g.drawRect(310, 160, 100, 50);
g.setColor(Color.green);
g.fillRect(310, 160, 100, 50);
g.drawRect(410, 210, 100, 50);
g.setColor(Color.orange);
g.fillRect(410, 210, 100, 50);
Font f = new Font("Arial", 2, 50);
g.setFont(f);
g.setColor(Color.black);
g.drawString("Krishnasheesh", 500, 500);
}
}
/*
* <applet code = "DiagonalBoxes_18" width=600 height=600>
* </applet> */
Output: -
Q20. Write a Program to Display an Octagon (Polygon).
*/
Output: -
Q21. Write a Program to display a Cylinder, Cube, Square in Circle, Circle in Square and
Polygon.
import java.awt.*; import
java.applet.*; public class Shapes_21
extends Applet
{
String msg="";
int x, y, width, height;
setForeground(Color.BLACK);
}
public void paint(Graphics g)
{
//Points Of Cylinder
g.drawLine(50,50, 50,150);
g.setColor(Color.BLUE);
g.fillOval(50,25,100,50);
g.drawLine(150,50, 150,150);
g.fillOval(50,125,100,50);
//Points Of Envelope
g.drawLine(200,50, 200,150);
g.drawLine(200,50, 300,50);
g.drawLine(300,50, 300,150);
g.drawLine(200,50, 300,150);
g.drawLine(200,150, 300,50);
g.drawLine(200,150, 300,150);
//Square Inside Circle
g.setColor(Color.BLUE);
g.fillOval(375,30,150,140);
g.setColor(Color.RED);
g.fillRect(400,50,100,100);
//Cube
g.drawRect(50,300,100,100);
g.drawLine(50,300,75,325);
g.drawRect(75,325,100,100);
g.drawLine(150,400,175,425);
g.drawLine(150,300,175,325);
g.drawLine(50,400,75,425);
//Circle Inside Square
g.setColor(Color.RED);
g.fillRect(250,300,120,120);
g.setColor(Color.BLUE);
g.fillOval(250,300,120,120);
//Hexagone
int x[] = {450,500,530,500,450,420};
g.setColor(Color.BLACK);
g.fillPolygon(x,y,6);
}
}
/*
<applet code="Shapes_21" width=600 height=600>
</applet>
*/
Output: -
Q22. Write a Program to use My Mouse Events.
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g) {
setBackground(currentColor);
g.setFont(f);
}
public void mouseClicked(MouseEvent e) {
currentColor = Color.RED;
msg = "mouseClicked";
repaint();
}
public void mousePressed(MouseEvent e) {
currentColor = Color.GREEN;
msg = "mousePressed";
repaint();
}
public void mouseReleased(MouseEvent e) {
currentColor = Color.BLUE;
msg = "mouseReleased";
repaint();
}
public void mouseEntered(MouseEvent e) {
currentColor = Color.YELLOW;
msg = "mouseEntered";
repaint();
}
public void mouseExited(MouseEvent e) {
currentColor = Color.WHITE;
msg = "mouseExited";
repaint();
}
public void mouseDragged(MouseEvent e) {
currentColor = Color.CYAN;
msg = "mouseDragged";
repaint();
}
public void mouseMoved(MouseEvent e) {
msg = "mouseMoved";
repaint();
}
}
/* <applet code="MouseEventNew_22" width=300 height=300>
</applet> */
Output: -
Q23. Write a Program to use KeyEvents.
addKeyListener(this);
}
msg = "keyPressed";
repaint();
msg = "keyReleased";
repaint();
}
}
/*
<applet code="KeyEvent_23" width=400 height=400>
</applet>
*/
Output: -
Q24. Write a Program to use Button Demo.
import java.applet.*;
import java.awt.*; import
java.awt.event.*;
g.setFont(f1);
g.drawString("Best Car", 200, 100);
}
public void actionPerformed(ActionEvent ae) {
String str = ae.getActionCommand();
if (str.equals("Ferrari")) {
setBackground(Color.red); } else if
(str.equals("Lamborghini")) {
setBackground(Color.yellow);
} else {
setBackground(Color.blue);
}
}
}
/*
<applet code =" ButtonDemo_24 " width=500 height=500>
</applet>
*/
Output: -
Q26. Write a Program to make Spiral Matrix.
public class SpiralMatrix_26 { public static
void printSpiral(int[][] matrix) { int top =
0;
int bottom = matrix.length - 1;
int left = 0;
int right = matrix[0].length - 1;
};
case 1:
c[1] = new Ladies();
c[1].notice();
break;
case 2:
c[2] = new General();
c[2].notice();
break;
case 3:
c[3] = new Luggage();
c[3].notice();
break;
default:
System.out.println("Invalid");
}
}
}
Output: -