Tuesday, September 26, 2023
BCA and B.Sc Programs - Programming in Java Lab - Bharathiar University -Program 10 - Write a Java Program to create a frame which responds to the mouse clicks. For each events with mouse such as mouse up, mouse down, etc, the corresponding message to be displayed
BCA and B.Sc Programs - Programming in Java Lab - Bharathiar University - Practical Program 8 - Write a Java Program to create a frame with three text fields for name, age and qualification and a text field for multiple line for address
Bharathiar University
Programming Java LabJava Program 8Write a Java Program to create a frame with three text fields for name, age and qualification and a text field for multiple line for address
Source Codeimport javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;
public class UserForm { public static void main(String[] args) { // Create the main frame JFrame frame = new JFrame("User Profile Form"); frame.setSize(320, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new FlowLayout(FlowLayout.LEFT,20,25));
// Create labels and text fields JLabel nameLabel = new JLabel("Name:"); JTextField name = new JTextField(20);
JLabel ageLabel = new JLabel("Age:"); JTextField age = new JTextField(20);
JLabel qualificationLabel = new JLabel("Qualification:"); JTextField qualification = new JTextField(20);
JLabel addressLabel = new JLabel("Address:"); JTextArea address = new JTextArea(5, 20);
// Create a button to trigger the action JButton submit = new JButton("Submit");
// Add components to the frame frame.add(nameLabel); frame.add(name); frame.add(ageLabel); frame.add(age); frame.add(qualificationLabel); frame.add(qualification); frame.add(addressLabel); frame.add(new JScrollPane(address)); frame.add(submit);
// Create ActionListener for the button submit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // Display all values in a dialog box String message = "Name: " + name.getText() + "\nAge: " + age.getText() + "\nQualification: " + qualification.getText() + "\nAddress:\n" + address.getText(); JOptionPane.showMessageDialog(frame, message); } });
// Set frame visibility frame.setVisible(true); }}
OUTPUT
Dialog box
BCA and B.Sc Programs - Programming in Java Lab - Bharathiar University - Practical Program 7 - Write a Java Program to demonstrate the Multiple Selection List-box
Bharathiar University
Programming Java LabJava Program 7Write a Java Program to demonstrate the Multiple Selection List-box
Source Code
import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;
public class MultiSelectionListbox extends JFrame { public JLabel label; public JButton submit; public JList list1,list2; public MultiSelectionListbox() { setTitle("Multiple Selection ListBox"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500, 500); setLayout(new FlowLayout(FlowLayout.LEFT)); String values[]={"C","C++","Java","Python","R","HTML","XML","CSS","PHP"}; label = new JLabel("Which Languages do you know?"); list1 = new JList(values); list2 = new JList(); list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); submit = new JButton("Submit"); submit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { list2.setListData(list1.getSelectedValues()); } }); add(label); add(new JScrollPane(list1)); add(list1); add(submit); add(new JScrollPane(list2)); add(list2); setVisible(true); } public static void main(String[] args) { new MultiSelectionListbox();}}
OUTPUT
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 LabJava Program 6Write 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 LabJava Program 4Write 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()); } }}
OUTPUTsample.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 LabJava Program 4Write 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;
}
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...
-
Bharathiar University Programming in C++ Lab C++ Program 1 Write a C++ Program to create a class to implement the data structure STACK. Wr...
-
Bharathiar University Programming Java Lab Java Program 2 Write a Java Program to implement the concept of multiple inheritance using Inte...
-
Bharathiar University Programming Java Lab Java Program 1 Write a Java Applications to extract a portion of a character string and print t...