Posts

Week4 java

  Write a program that creates a user interface to perform integer divisions. The user enters two numbers in the text fields, Num1 and Num2.The division of Num1 and Num2 is displayed in the Result field when the Divide button is clicked. If Num1 or Num2 were not an integer, the program would throw a NumberFormatException. If Num2 were Zero, the program would throw an Arithmetic Exception Display the exception in a message dialog box. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DivisionCalculator extends JFrame implements ActionListener { JLabel inputLabel1, inputLabel2, resultLabel; JTextField input1, input2, result; JButtondivideButton;     public DivisionCalculator() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout());         inputLabel1 = new JLabel("Welcome"); setSize(800, 400);         inputLabel1 = new JLabel("Enter Number1");         add(inputLa...

Week13

 import java.io.File;   public class DisplayFileExample   {   public void printFileNames(File[] a, int i, int lvl)   {   // base case of the recursion   // i == a.length means the directory has    // no more files. Hence, the recursion has to stop   if(i == a.length)   {   return;   }   // checking if the encountered object is a file or not   if(a[i].isFile())   {   System.out.println(a[i].getName());   }   // recursively printing files from the directory   // i + 1 means look for the next file   printFileNames...

Insertion deletion ds lab

 Insert at front #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; struct Node* prev; } Node; Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; newNode->prev = NULL; return newNode; } void printList(Node* head) { Node* temp = head; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("n"); } void insertOne(Node** head, int data, int index) { Node* newNode = createNode(data); if (index == 0) { newNode->next = *head; if (*head != NULL) { (*head)->prev = newNode; } *head = newNode; return; } Node* temp = *head; int currentIndex = 0;   while (temp != NULL && currentIndex < index - 1) { temp = temp->next; currentIndex++; }   if (temp == NULL) { free(newNode); printf("Index out of bounds. Inserting at the end instead.n"); return; } newNode->next = temp->next; newNode->prev = t...

Week4

 #include<stdio.h> #include<conio.h> #define size 10 void push(int); void pop(); void display(); int stack[size], top = -1; void main() { int ele, choice; clrscr(); while(1){ printf("\n\n***** MENU *****\n"); printf("1. Push\n2. Pop\n3. Display\n4. Exit"); printf("\nEnter your choice: "); scanf("%d",&choice); switch(choice){ case 1: printf("Enter the value to be insert: "); scanf("%d",&ele); push(ele); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); default: printf("\nWrong selection!!! Try again!!!"); } } } void push(int ele) { if(top == size-1) printf("\nStack is Full!!! Insertion is not possible!!!"); else { top++; stack[top] = ele; printf("\nElement Inserted!!!"); } } void pop() { if(top == -1) printf("\nStack is Empty!!! Deletion is not possible!!!"); else { printf("\nDeleted : %d", stack[top]); top--; } } void display() { if(top...

Create and Insert data in circular linked list

  #include <stdio.h> #include <stdlib.h> // Create a node struct Node {   int data;   struct Node* next; }; // Insert at the beginning void insertAtBeginning(struct Node** head_ref, int new_data) {   // Allocate memory to a node   struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));   // insert the data   new_node->data = new_data;   new_node->next = (*head_ref);   // Move head to new node   (*head_ref) = new_node; } // Insert a node after a node void insertAfter(struct Node* prev_node, int new_data) {   if (prev_node == NULL) {   printf("the given previous node cannot be NULL");   return;   }   struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));   new_node->data = new_data;   new_node->next = prev_node->next;   prev_node->next = new_node; } // Insert the the end void insertAtEnd(struct Node** head_ref, int new_data) {   struct Node* n...

Create RandomGenerator.java

  Create RandomGenerator.java import java.util.*; class RandomGenerator extends Thread {  public String tname;  public Random r; public Thread t1, t2;  public RandomGenerator(String s) {  tname = s; } public void run() {  int num = 0;  r = new Random(); try { for (int i = 0; i< 50; i++) { num = r.nextInt(100); System.out.println("Main Thread and Generated Number is " + num);  if (num % 2 == 0) {  t1 = new Thread(new Even(num));   t1.start();  } else {  t2 = new Thread(new Odd(num));  t2.start(); } Thread.sleep(1000);  System.out.println ("--------------------------------------------"); } } catch (Exception ex) {  System.out.println(ex.getMessage()); } } } public class Mthread { public static void main(String[] args) {  RandomGenerator a = new RandomGenerator("One");  a.start(); } } import java.util.*; class Even implements Runnable { public int x; public Even(int x) { this.x = x; } public void r...

Week2

 abstract class Shape { public int x, y; public abstract void printArea(); } class Rectangle extends Shape { public void printArea() { System.out.println("Area of Rectangle is " + x * y); } } class Triangle extends Shape { public void printArea() { System.out.println("Area of Triangle is " + (x * y) / 2); } } class Circle extends Shape { public void printArea() { System.out.println("Area of Circle is " + (22 * x * x) / 7); } } public class Abstex { public static void main(String[] args) { Rectangle r = new Rectangle (); r.x = 10; r.y = 20; r.printArea(); System.out.println(" "); Triangle t = new Triangle(); t.x =30; t.y =35; t.printArea(); System.out.println(" "); Circle c = new Circle(); c.x = 2; c.printArea(); System.out.println(" "); } }