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

Tanay Java MCA Sem 1

Uploaded by

Technical Wala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Tanay Java MCA Sem 1

Uploaded by

Technical Wala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

SUBMITTED BY: SUBMITTED TO:

Name : Tanay Mishra MR. RAJESH KUMAR

Enrollment No : A010145023039

Programme : MCA-1(B)
Table of Contents:-

S.No Name of the Program Date Signature Remarks

1 WAP to convert Decimal to Binary 22-08-2023

WAP to check whether a number is


2 23-08-2023
Armstrong or not

3 WAP to convert Binary to Decimal 24-08-2023

4 WAP to sort an array using Bubble Sort 28-08-2023

5 WAP to sort an array using Heap sort 28-08-2023

WAP to show Inheritance.


6 05-09-2023

WAP of ExceptionExample1(Handle an
7 13-09-2023
exception).

WAP of Exception Example


8 13-09-2023
2(Interruption).
WAP of Exception Example
9 3(Arithmetic). 18-09-2023

WAP to Handle an Exception and use


10 18-09-2023
its methods. (Example 4)

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

WAP to create a Thread and use the


setName , getName , activeCount ,
17 setPriority & getPriority method. 20-09-2023

WAP to make 5 different coloured


18 26-09-2023
boxes & display them diagonally.

WAP to print multiple hello world


19 26-09-2023
diagonally in a java applet.

20 WAP to display a Octagon (Polygon). 26-09-2023

WAP to display a Cylinder,


Cube , Square in circle , Circle
21 26-09-2023
in Square, Polygon.

22 WAP to use MouseEvents. 27-09-2023

23 WAP to use KeyEvent. 27-09-2023

24 WAP to use ButtonDemo. 28-09-2023

25 WAP to use ButtonDemoText. 03-10-2023

26 WAP to make SpiralMatrix. 03-10-2023

27 WAP to make SmileyFace. 04-10-2023

28 WAP to make BorderLayout. 04-10-2023

29 WAP to make GridLayout. 04-10-2023

30 WAP to make FlowLayout. 04-10-2023


31 WAP to run a Calculator. 04-10-2023

WAP to create connection to database.


32 5-10-2023

33 WAP to create table in JDBC. 6-10-2023

34 WAP to insert in table in JDBC. 6-10-2023

35 WAP to use Prepared statement. 9-10-2023

36 WAP to retrieve data from database. 10-10-2023

37 WAP to insert more than 1 record. 11-10-2023

WAP To Call Stored Procedure To Add


38 12-10-2023
Two Numbers.

39 WAP to call Scrollable Result Set. 16-10-2023

40 WAP to update Result set. 17-10-2023


Q.1 Write a Program to convert Decimal to Binary.

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;

rev = rev * 10;


num = num / 2;
}
System.out.println(bin);
}
}
Output: -
Q2. Write a Program to check whether the number is Armstrong or not.
import java.util.Scanner;
class Armstrong_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(), temp = a, temp1 = a;
double b, d = 0, c, num = 0;
while (temp1 != 0) {
temp1 = temp1 / 10;
d++;
}
while (a != 0) {
b = a % 10;
c = Math.pow(b, d);
num = num + c;
a = a / 10;
}
if (temp == num) {
System.out.println("The Number is Armstrong.");
} else {
System.out.println("The Number is not Armstrong.");
}
}

}
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");
}
}

public class Faculty_6 extends Faculty {


String mainSubject = "Java";

void hacking() {
System.out.println("Hacking is my Passion");
}

public static void main(String[] args) {


Faculty_6 obj = new Faculty_6();
System.out.println(obj.collegeName);
System.out.println(obj.designation);
System.out.println(obj.mainSubject);
obj.does(); obj.hacking();
}
}
Output: -
Q7. Write a Program of Exception Handling Example 1.

class ExceptionEx1_7 { public static


void main(String[] args) {
try {
System.out.println(9 / 0);
} catch (ArithmeticException e) {
System.out.println(e);
}
}
}
Output: -
Q8. Write a Program of Exception Handling Example 2.

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.

import java.util.Random; class


ExceptionEx3_9 {
public static void main(String[] args) {
int a = 0, b = 0, c = 0;
Random r = new Random();

for (int i = 0; i < 100; i++) {


try { b=
r.nextInt(); c=
r.nextInt(); a=
12345 / (b / c);
} catch (ArithmeticException e) {
System.out.println("Division by Zero");
a = 0;
}
}
}
}
Output: -
Q10. Write a Program to handle an Exception and use its methods Example 4.

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.

class MultipleCatch_11 { public static


void main(String[] args) {
try {
int a = args.length;
System.out.println("a = " + a);
int b = 54 / a; int c[] = { 1 };
c[21] = 84;
} catch (ArithmeticException e) {
System.out.println("Divide by 0 : " + e);
} catch (ArrayIndexOutOfBoundsException a) {
System.out.println("Array Index : " + a);
}
System.out.println("Will this line be printed?");
}
}
Output: -
Q12. Write a Program to do Demo Join(Thread Runnable).

class NewThread implements Runnable {


String name;
Thread t;

NewThread(String threadname) {
name = threadname; t = new
Thread(this, name);
System.out.println("New thread: " + t);

t.start();
}

public void run() {


try {
for (int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);

}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");
}
System.out.println(name + " Exiting.");
}
}

class DemoJoin_12 { public static


void main(String[] args) {
NewThread ob1 = new NewThread("Ford");
NewThread ob2 = new NewThread("Ferrari");
NewThread ob3 = new NewThread("Lamborghini");
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());


try {
System.out.println("Waiting for threads to finish");
ob1.t.join();

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 Thread1 extends Thread {


public void run() {
System.out.println("India");
}
}

class Thread2 extends Thread {


public void run() {
System.out.println("America");
}
}

class MultiThreadMultiTask_14 {
public static void main(String[] args) {

Thread1 th1 = new Thread1();


Thread2 th2 = new Thread2();
th1.start();
th2.start();
}
}
Output: -
Q15. Write a Program to use Multiple thread for single task.

class MultiThreadSingleTask_15 extends Thread {


public void run() {
System.out.println("Virat_Kholi,MSD");
}

public static void main(String[] args) {


MultiThreadSingleTask t1 = new MultiThreadSingleTask();
MultiThreadSingleTask t2 = new MultiThreadSingleTask();
MultiThreadSingleTask t3 = new MultiThreadSingleTask();
t1.start();
t2.start(); t3.start();
}
}
Output: -
Q16. Write a Program to use Single Thread for Single Task.

class SingleThread1 extends Thread {


public void run() {
System.out.println("task one");
}
}

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.

class MultithreadingMethods_17 extends Thread {


@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println(e);
}
System.out.println(i);
}
}

public static void main(String[] args) {


MultithreadingMethods_17 a = new MultithreadingMethods_17();
a.start();
System.out.println(a.getName());
System.out.println(Thread.activeCount());
a.setPriority(4);
System.out.println(a.getPriority());
a.setName("KRISHNASHEESH");
System.out.println(a.getName());

}
}
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).

import java.applet.*; import


java.awt.*;

public class Octagon_20 extends Applet {


int xcoords[] = { 69, 131, 175, 175, 131, 69, 25, 25 };
int ycoords[] = { 25, 25, 69, 131, 175, 175, 131, 69 };

public void paint(Graphics g) {


g.drawPolygon(xcoords, ycoords, 8);
g.setColor(Color.red);
g.fillPolygon(xcoords, ycoords, 8);

Font f = new Font("Arial", 2, 50);


g.setFont(f);
g.setColor(Color.black);
g.drawString("Rautish", 350, 500);
}
}
/*
* <applet code = "Octagon_22" width=600 height=600>
* </applet>

*/
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;

public void init()


{
setBackground(Color.WHITE);

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};

int y[] = {300,300,350,400,400,350};

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.

import java.awt.*; import


java.applet.*; import
java.awt.event.*;
public class MouseEventNew_22 extends Applet implements MouseListener,
MouseMotionListener {
Color currentColor = Color.WHITE;
Font f = new Font("Arial",3,30);
String msg = ""; public void
init() {

addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g) {

setBackground(currentColor);

g.setFont(f);

g.drawString(msg, 10, 50);

}
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.

import java.awt.*; import


java.applet.*; import
java.awt.event.*;

public class KeyEvent_23 extends Applet implements KeyListener {


String keyPressed = "";
String msg ="";
Color currentColor;
Font f = new Font("Arial",3,30);

public void init() {


keyPressed = "";

addKeyListener(this);
}

public void paint(Graphics g) {


setBackground(currentColor);
g.setFont(f);
g.drawString(msg, 10, 50);
g.drawString(keyPressed, 150, 150);
}

public void keyTyped(KeyEvent e) {


msg = "keyTyped";
keyPressed = Character.toString(e.getKeyChar());
repaint();
}

public void keyPressed(KeyEvent e) {


currentColor = Color.GREEN;

msg = "keyPressed";
repaint();

public void keyReleased(KeyEvent e) {


currentColor = Color.RED;

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.*;

public class ButtonDemo_24 extends Applet implements ActionListener {


public void init() {
Button b1 = new Button("Ferrari");
Button b2 = new Button("Lamborghini");
Button b3 = new Button("Ford");
add(b1); add(b2); add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
Font f2 = new Font("Sans Serif", 1, 20);
b1.setFont(f2); b2.setFont(f2);
b3.setFont(f2);

public void paint(Graphics g) {


Font f1 = new Font("Arial", 1, 50);

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;

while (left <= right && top <= bottom) {


// Print left column from top to bottom

for (int i = top; i <= bottom; i++) {


System.out.print(matrix[i][left] + " ");
}
left++;

// Print bottom row from left to right


for (int i = left; i <= right; i++) {
System.out.print(matrix[bottom][i] + " ");
}
bottom--;

// Check if there's more to print


if (left <= right) {
// Print right column from bottom to top
for (int i = bottom; i >= top; i--) {

System.out.print(matrix[i][right] + " ");


}
right--;
}
// Check if there's more to print
if (top <= bottom) {

// Print top row from right to left


for (int i = right; i >= left; i--) {
System.out.print(matrix[top][i] + " ");
}
top++;
}
}
}

public static void main(String[] args) {


int[][] matrix = {
{ 1, 12, 11, 10 },
{ 2, 13, 16, 9 },
{ 3, 14, 15, 8 },
{ 4, 5, 6, 7 }

};

System.out.println("Matrix in spiral order:");


printSpiral(matrix);
}
}
Output:
Q32. Write a program in java to use an Abstraction class in a railway coach.
abstract class Compartment {

abstract void notice();


}
class FirstClass extends Compartment {
void notice() {

System.out.println("It is first class");


}
}
class General extends Compartment {
void notice() {
System.out.println("It is General Compartment");
}
}

class Ladies extends Compartment {


void notice() {
System.out.println("It is Ladies Compartment");
}
}
class Luggage extends Compartment {
void notice() {
System.out.println("It is luggage");
}
}
public class RailwayCoach {
public static void main(String[] args) {
Compartment c[] = new Compartment[10];
double i = Math.random() * 5;
int x = (int) i;
System.out.println(x);
switch (x) {
case 0:
c[0] = new FirstClass();
c[0].notice();
break;

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: -

You might also like