Saturday, September 23, 2023

BCA and B.Sc Programs - Programming in Java Lab - Bharathiar University - Practical Program 6- Write a Java Program to create a frame with four text fields name, street, city and pin code with suitable tables. Also add a button called my details. When the button is clicked its corresponding values are to be appeared in the text fields.


Bharathiar University

Programming Java Lab
Java Program 6
Write a Java Program to create a frame with four text fields name, street, city and pin code with suitable tables. Also add a button called my details. When the button is clicked its corresponding values are to be appeared in the text fields.



Source Code

 import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyDetails extends JFrame {
    private JTextField name;
    private JTextField street;
    private JTextField city;
    private JTextField pincode;
    private JButton myDetailsButton;
    private JTable detailsTable;
    public MyDetails() {
        setTitle("My Details");
        setSize(580, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        // Create components
        name = new JTextField(20);
        street = new JTextField(20);
        city = new JTextField(20);
        pincode = new JTextField(20);
        myDetailsButton = new JButton("My Details");
        // Create table with sample data
        String[] columnNames = {"Name", "Street", "City", "Pincode"};
        String[][] data = {
            {"xxx", "1 Main St", "Chennai", "641801"},
            {"yyy", "4 Raja St", "Coimbatore", "641007"},
            {"zzz", "63 Ram Nagar", "Erode", "638507"}, 
            
            // Add more rows as needed
        };
        
        detailsTable = new JTable(data, columnNames);
        // Add action listener to the button
        myDetailsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedRow = detailsTable.getSelectedRow();
                if (selectedRow >= 0) {
                    name.setText((String) detailsTable.getValueAt(selectedRow, 0));
                    street.setText((String) detailsTable.getValueAt(selectedRow, 1));
                    city.setText((String) detailsTable.getValueAt(selectedRow, 2));
                    pincode.setText((String) detailsTable.getValueAt(selectedRow, 3));
                }
            }
        });
add(new JLabel("Name:"));
add(name);
add(new JLabel("Street:"));
add(street);
add(new JLabel("City:"));
add(city);
add(new JLabel("Pincode:"));
add(pincode);
add(myDetailsButton);
add(detailsTable);
add(new JScrollPane(detailsTable));
        setVisible(true);
    }
    public static void main(String[] args) {
        
                new MyDetails();
         
    }
}

OUTPUT





Thursday, September 14, 2023

BCA and B.Sc Programs - Programming in Java Lab - Bharathiar University - Practical Program 4- Write a Java Program which open an existing file and append text to that file.

 

Bharathiar University

Programming Java Lab
Java Program 4
Write a Java Program which open an existing file and append text to that file.



Source Code

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

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

        String filePath;

        // The text you want to append
        String text;
        
        try {
            Scanner in =new Scanner(System.in);
             // Specify the file path you want to append to
            System.out.println("Enter File Path (use double slash):");
            filePath=in.nextLine();
            // Create a FileWriter in append mode
            FileWriter fw = new FileWriter(filePath, true);

            // Create a BufferedWriter for efficient writing
            BufferedWriter bw = new BufferedWriter(fw);
             // The text you want to append
            System.out.println("Enter text to append the File:");
            text=in.nextLine();
            bw.newLine(); 
            // Append the text to the file
            bw.write(text);
            bw.newLine(); // Add a newline for clarity

            // Close the BufferedWriter to flush and release resources
            bw.close();

            System.out.println("Text appended to the file successfully.");
        } catch (IOException e) {
            System.err.println("An error occurred: " + e.getMessage());
        }
    }
}


OUTPUT
sample.txt file before appending text




sample.txt file after appending text





Tuesday, September 12, 2023

BCA and B.Sc Programs - Programming in Java Lab - Bharathiar University - Practical Program 4- Write a Java Program to implement the concept of multithreading with the use of any three multiplication tables and assign three different priorities to them.


Bharathiar University

Programming Java Lab
Java Program 4
Write a Java Program to implement the concept of multithreading with the use of any three multiplication tables and assign three different priorities to them.

Source Code


 import java.util.*;

public class MultiThread extends Thread {

    private int tableNumber;

    private int max;


    public MultiThread(int tableNumber, int max) {

        this.tableNumber = tableNumber;

        this.max = max;

    }


   // @Override

    public void run() {

        for (int i = 1; i <= max; i++) {

            System.out.println(tableNumber + " x " + i + " = " + (tableNumber * i));

            try {

                Thread.sleep(1000); // Add a slight delay to better visualize the output

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

    }


    public static void main(String[] args) {

        int limit = 10;


Scanner in =new Scanner(System.in);

System.out.println("Enter three numbers for multiplication tables: ");

int x=in.nextInt();

int y=in.nextInt();

int z=in.nextInt();

        // Create three threads with different priorities

        MultiThread t1 = new MultiThread(x, limit);

        MultiThread t2 = new MultiThread(y, limit);

        MultiThread t3 = new MultiThread(z, limit);


        // Set different priorities to the threads (MIN_PRIORITY = 1, MAX_PRIORITY = 10)

        t1.setPriority(Thread.MIN_PRIORITY);

        t2.setPriority(Thread.NORM_PRIORITY);

        t3.setPriority(Thread.MAX_PRIORITY);


        // Start the threads

        t1.start();

        t2.start();

        t3.start(); 

    }

}


OUTPUT:




Tuesday, September 5, 2023

BCA and B.Sc Programs - Programming in C Lab - Bharathiar University - Practical Program- C Program to Write a C Program to convert decimal number to binary number

 

BHARATHIAR UNIVERSITY

BCA and B.Sc Programs -  Programming in C Lab - Bharathiar University - Practical Program- 

C Program to Write a C Program to convert decimal number to binary number


Source Code

#include <stdio.h> 

#include <conio.h>

void decimalToBinary(int decimal) 

int binary[32]; 

int index = 0,i;

while (decimal > 0) 

binary[index++] = decimal % 2; 

decimal /= 2; 

printf("Binary representation: "); 

for (i = index - 1; i >= 0; i--)

printf("%d", binary[i]); 

} printf("\n"); 

int main() 

int decimal;

clrscr();

printf("Enter a decimal number: ");

scanf("%d", &decimal);

decimalToBinary(decimal);

getch();

return 0; 

}


OUTPUT:



Thursday, August 31, 2023

BCA and B.Sc Programs - Programming in Java Lab - Bharathiar University - Practical Program 3- Write a Java Program to create an Exception called PayOut of Bounds and throw the exception.

 

Bharathiar University

Programming Java Lab
Java Program 3
Write a Java Program to create an Exception called PayOut of Bounds and throw the exception.

Source Code
 
import java.util.*;
//user defined exception class for pay out of bounds Exception
class PayOutOfBoundsException extends Exception
 {
    //Constructor 
    public PayOutOfBoundsException(String s)
    {
       super(s); //calls Exception class Constructor
    }
}
//Main class for User defined Exception
public class Salary
{
    public static void main(String args[])
    {
    
    Scanner in =new Scanner(System.in);
    System.out.println("Enter Employee Salary: ");
    //Get Employee salary from User
    int esal=in.nextInt();
    try{
        //Check if employee salary not between 3000 and 10000 
        if(esal<3000 || esal>10000)
        {
    // throw pay out of bounds exception with a message
            throw new PayOutOfBoundsException("Salary must be 3000 to 10000");
        }
         //print employee salary if given salary is within bounds
        System.out.println("The given salary of Employee is "+esal);
    }
   //catch block for payoutof bounds exception
    catch(PayOutOfBoundsException e)
    {
//catch the exception and prints the error message
        System.out.println(e);
    }
}
}

Output:


Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...