Program
function[]=gf(a,b,d,f)
c=(a*f(b)-b*f(a))/(f(b)-f(a))
while(abs(f(c))>d)
c=(a*f(b)-b*f(a))/(f(b)-f(a))
if(f(a)*f(c)<0)then b=c
elseif(f(c)*f(b)<0)then a=c
end
end
printf('root by regular falsi method=%f',c)
endfunction
Output:
-->deff('y=f(x)','y=x*tan(x)+1')
-->gf(2.5,3,50,f)
root by regular falsi method=2.801252
18 Comments
/* ********* Slip 16 ********** */
ReplyDelete/* 1. Write a java program to create a TreeSet, add some colors (String) and print out the
content of TreeSet in ascending order. */
import java.io.*;
import java.util.*;
public class Q1 {
public static void main(String args[]) throws IOException
{
Set ts = new TreeSet();
ts.add("Red");
ts.add("Blue");
ts.add("Yellow");
ts.add("Pink");
ts.add("Baby Pink");
System.out.println("TreeSet in ascending order: " + ts);
}
}
/* 2. Write a Java program to accept the details of Teacher (TNo, TName, Subject). Insert at
least 5 Records into Teacher Table and display the details of Teacher who is teaching
“JAVA” Subject. (Use PreparedStatement Interface) */
import java.sql.*;
public class Q2 {
public static void main(String[] args) {
try {
// Load PostgreSQL Driver (optional for newer JDBC versions)
Class.forName("org.postgresql.Driver");
// Establish Connection
Connection con = DriverManager.getConnection(
"jdbc:postgresql:amresh", "postgres", "8624807723");
// Insert multiple rows into the Teacher table
String insertQuery = "INSERT INTO Teacher (Tno, Tname, Subject) VALUES (?, ?, ?)";
PreparedStatement insertStmt = con.prepareStatement(insertQuery);
// Data entries
Object[][] teachers = {
{1, "Missba Shaikh", "JAVA"},
{2, "Afreen Sayyed", "Python"},
{3, "Shabnam Pathan", "C++"},
{4, "Aslam Saudagar", "JAVA"},
{5, "Imran Shaikh", "PHP"}
};
// Loop through each entry and execute insert
for (Object[] teacher : teachers) {
insertStmt.setInt(1, (int) teacher[0]);
insertStmt.setString(2, (String) teacher[1]);
insertStmt.setString(3, (String) teacher[2]);
insertStmt.executeUpdate();
}
System.out.println("Data inserted successfully!");
// Select statement to fetch teachers who teach "JAVA"
String selectQuery = "SELECT * FROM Teacher WHERE Subject = ?";
PreparedStatement selectStmt = con.prepareStatement(selectQuery);
selectStmt.setString(1, "JAVA");
ResultSet rs = selectStmt.executeQuery();
System.out.println("\nTeachers who teach JAVA:");
while (rs.next()) {
int tno = rs.getInt("Tno");
String tname = rs.getString("Tname");
String subject = rs.getString("Subject");
System.out.println("Teacher Number: " + tno + ", Name: " + tname + ", Subject: " + subject);
}
// Close resources
rs.close();
selectStmt.close();
insertStmt.close();
con.close();
System.out.println("\nConnection closed successfully!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
/* ******** Slip 17 ******* */
ReplyDelete/* 1. Write a java program to accept ‘N’ integers from a user. Store and display integers in
sorted order having proper collection class. The collection should not accept duplicate
elements */
import java.util.*;
public class Q1
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of integers: ");
int n = scanner.nextInt();
Set set = new TreeSet<>();
for (int i = 1; i <= n; i++) {
System.out.print("Enter integer #" + i + ": ");
int num = scanner.nextInt();
set.add(num);
}
System.out.println("The integers in sorted order are: ");
for (int num : set) {
System.out.print(num + " ");
}
}
}
/* 2. Write a Multithreading program in java to display the number’s between 1 to 100
continuously in a TextField by clicking on button. (Use Runnable Interface). */
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Q2 implements Runnable
{
private JTextField textField;
public Q2(JTextField textField) {
this.textField = textField;
}
public void run() {
for (int i = 1; i <= 100; i++) {
textField.setText(Integer.toString(i));
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
JFrame frame = new JFrame("Number Display");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField textField = new JTextField(10);
JButton button = new JButton("Start");
JPanel panel = new JPanel();
panel.add(textField);
panel.add(button);
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Q2 numberDisplay = new Q2(textField);
Thread thread = new Thread(numberDisplay);
thread.start();
}
});
}
}
/* ********** SLip 18 *********** */
ReplyDelete/* 1. Write a java program to display name and priority of a Thread. */
public class Q1
{
public static void main(String[] args)
{
Thread thread = Thread.currentThread();
System.out.println("Thread Name: " + thread.getName());
System.out.println("Thread Priority: " + thread.getPriority());
}
}
/* ********************* Slip 19 *************** */
ReplyDelete/* 1. Write a java program to accept ‘N’ Integers from a user store them into LinkedList
Collection and display only negative integers. */
import java.util.LinkedList;
import java.util.Scanner;
public class Q1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LinkedList list = new LinkedList();
System.out.print("Enter the number of integers: ");
int n = sc.nextInt();
System.out.println("Enter " + n + " integers:");
for (int i = 0; i < n; i++) {
int num = sc.nextInt();
list.add(num);
}
System.out.println("Negative Integers:");
for (int i : list) {
if (i < 0) {
System.out.println(i);
}
}
}
}
ReplyDelete//2. Write a java program to blink image on the JFrame continuously.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Q2 extends JPanel implements ActionListener
{
private static final long serialVersionUID = 1L;
private Image image;
private boolean blinkOn = true;
public Q2(Image image) {
this.image = image;
Timer timer = new Timer(500, this);
timer.start();
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (blinkOn) {
g.drawImage(image, 0, 0, this);
}
}
public void actionPerformed(ActionEvent e) {
blinkOn = !blinkOn;
repaint();
}
public static void main(String[] args) {
try {
Image image = ImageIO.read(new File("path/to/image.png"));
JPanel contentPane = new Q2(image);
contentPane.setPreferredSize(new Dimension(image.getWidth(null),
image.getHeight(null)));
JFrame frame = new JFrame("Blinking Image");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(contentPane, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/* *********** SLip 21 ********** */
ReplyDelete/* 1. Write a java program to accept ‘N’ Subject Names from a user store them into
LinkedList Collection and Display them by using Iterator interface. */
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
public class Q1
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of subjects: ");
int n = sc.nextInt();
LinkedList subjects = new LinkedList();
System.out.println("Enter the subject names:");
for (int i = 0; i < n; i++) {
String subject = sc.next();
subjects.add(subject);
}
System.out.println("Subject names:");
Iterator iterator = subjects.iterator();
while (iterator.hasNext()) {
String subject = iterator.next();
System.out.println(subject);
}
}
}
/* 2. Write a java program to solve producer consumer problem in which a producer
produces a value and consumer consume the value before producer generate the next
value. (Hint: use thread synchronization) */
import java.util.LinkedList;
public class Q2 {
public static void main(String[] args) {
LinkedList buffer = new LinkedList<>();
int bufferSize = 5;
Thread producerThread = new Thread(new Producer(buffer, bufferSize), "Producer");
Thread consumerThread = new Thread(new Consumer(buffer), "Consumer");
producerThread.start();
consumerThread.start();
}
}
class Producer implements Runnable {
private final LinkedList buffer;
private final int bufferSize;
private int value = 0;
public Producer(LinkedList buffer, int bufferSize) {
this.buffer = buffer;
this.bufferSize = bufferSize;
}
@Override
public void run() {
while (true) {
synchronized (buffer) {
if (buffer.size() < bufferSize) {
buffer.add(value);
System.out.println(Thread.currentThread().getName() + " produced "
+ value);
value++;
buffer.notifyAll();
} else {
try
{
buffer.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
class Consumer implements Runnable {
private final LinkedList buffer;
public Consumer(LinkedList buffer) {
this.buffer = buffer;
}
@Override
public void run() {
while (true) {
synchronized (buffer) {
if (buffer.isEmpty()) {
try {
buffer.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
} else {
int value = buffer.removeFirst();
System.out.println(Thread.currentThread().getName() + " consumed " + value);
buffer.notifyAll();
}
}
}
}
}
// ****** SLip 22 **********
ReplyDelete/* 1. Write a Menu Driven program in Java for the following: Assume Employee table with
attributes (ENo, EName, Salary) is already created. 1. Insert 2. Update 3. Display 4.
Exit. */
import java.sql.*;
public class Q1 {
static final String JDBC_DRIVER = "com.postgresql.jdbc.Driver";
static final String DB_URL = "jdbc:postgresql:amresh";
static final String USER = "postgres";
static final String PASSWORD = "8624807723";
public static void main(String[] args)
{
Connection con = null;
Statement st = null;
try
{
Class.forName(JDBC_DRIVER);
con = DriverManager.getConnection(DB_URL, USER, PASSWORD);
st = con.createStatement();
int choice;
do
{
System.out.println("Menu:");
System.out.println("1. Insert");
System.out.println("2. Update");
System.out.println("3. Display");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = Integer.parseInt(System.console().readLine());
switch (choice) {
case 1:
System.out.print("Enter employee number: ");
int eno = Integer.parseInt(System.console().readLine());
System.out.print("Enter employee name: ");
String ename = System.console().readLine();
System.out.print("Enter employee salary: ");
double salary = Double.parseDouble(System.console().readLine());
String sql = "INSERT INTO Employee (ENo, EName, Salary) VALUES (" + eno + ", '" + ename + "', " + salary + ")";
st.executeUpdate(sql);
System.out.println("Record inserted successfully.");
break;
case 2:
System.out.print("Enter employee number: ");
eno = Integer.parseInt(System.console().readLine());
System.out.print("Enter new employee name: ");
ename = System.console().readLine();
System.out.print("Enter new employee salary: ");
salary = Double.parseDouble(System.console().readLine());
sql = "UPDATE Employee SET EName='" + ename + "', Salary=" +
salary + " WHERE ENo=" + eno;
int rowsAffected = st.executeUpdate(sql);
if (rowsAffected == 0) {
System.out.println("No records found with employee number "
+ eno);
} else {
System.out.println(rowsAffected + " record(s) updated successfully.");
}
break;
case 3:
sql = "SELECT * FROM Employee";
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
eno = rs.getInt("ENo");
ename = rs.getString("EName");
salary = rs.getDouble("Salary");
System.out.println("Employee number: " + eno + ", Employee name: " + ename + ", Employee salary: " + salary);
}
rs.close();
break;
case 4:
System.out.println("Exiting program.");
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
} while (choice != 4);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
/* ************ Slip 23 ************** */
ReplyDelete/* 1. Write a java program to accept a String from a user and display each vowel from a
String after every 3 seconds. */
import java.util.Scanner;
public class Q1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get user input
System.out.print("Enter a string: ");
String inputString = scanner.nextLine().toLowerCase();
// Loop through each character
for (int i = 0; i < inputString.length(); i++) {
char c = inputString.charAt(i);
// Check if the character is a vowel
if (isVowel(c)) {
System.out.print(c + " ");
// Pause for 3 seconds
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
scanner.close();
}
// Method to check if a character is a vowel
public static boolean isVowel(char c) {
return "aeiou".indexOf(c) != -1;
}
}
/* 2. Write a java program to accept ‘N’ student names through command line, store them
into the appropriate Collection and display them by using Iterator and ListIterator
interface. */
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class Q2 {
public static void main(String[] args) {
ArrayList studentNames = new ArrayList<>();
for (int i = 0; i < args.length; i++) {
studentNames.add(args[i]);
}
System.out.println("Student names using Iterator:");
Iterator iterator = studentNames.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
System.out.println("\nStudent names using ListIterator:");
ListIterator listIterator = studentNames.listIterator();
while (listIterator.hasNext()) {
System.out.println(listIterator.next());
}
}
}
/* ********* Slip 24 ********** */
ReplyDelete/* 1. Write a java program to scroll the text from left to right continuously. */
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Q1 extends JPanel implements Runnable {
private int x, y;
private String message;
private Thread thread;
private boolean running;
public Q1(String message) {
this.message = message;
x = getWidth();
y = getHeight() / 2;
setFont(new Font("Arial", Font.BOLD, 20));
setForeground(Color.RED);
setBackground(Color.WHITE);
thread = new Thread(this);
running = true;
thread.start();
}
public void run() {
while (running) {
try {
Thread.sleep(20); // Adjust the speed of scrolling here
}
catch (InterruptedException e)
{
e.printStackTrace();
}
x--;
if (x < -getFontMetrics(getFont()).stringWidth(message)) {
x = getWidth();
}
repaint();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString(message, x, y);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Scrolling Text");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 100);
Q1 scrollingText = new Q1("This is scrolling text");
frame.add(scrollingText);
frame.setVisible(true);
}
}
ReplyDelete/* 2. Write a Java Program for the following: Assume database is already created */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.swing.*;
public class Q2 extends JFrame implements ActionListener {
JTextField queryField;
JButton createButton, alterButton, dropButton;
Connection conn;
public Q2() {
// Set up the GUI layout
setTitle("DDL Query Executor");
setSize(400, 200);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Input field for query
queryField = new JTextField(30);
add(new JLabel("Type Your DDL Query Here"));
add(queryField);
// Buttons
createButton = new JButton("Create Table");
alterButton = new JButton("Alter Table");
dropButton = new JButton("Drop Table");
add(createButton);
add(alterButton);
add(dropButton);
// Add action listeners
createButton.addActionListener(this);
alterButton.addActionListener(this);
dropButton.addActionListener(this);
// Database connection setup
try {
conn = DriverManager.getConnection("jdbc:postgresql:amresh", "postgres", "8624807723");
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Database Connection Failed");
}
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String query = queryField.getText().trim();
if (query.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter a query!");
return;
}
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate(query);
JOptionPane.showMessageDialog(this, "Query executed successfully!");
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
}
}
public static void main(String[] args) {
new Q2();
}
}
/* *********** SLIP 26 ********* */
Delete/* 1. Write a Java program to delete the details of given employee (ENo EName Salary).
Accept employee ID through command line. (Use PreparedStatement Interface) */
import java.sql.*;
public class Q1 {
public static void main(String[] args) {
try
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:postgresql:amresh";
String username = "postgres";
String password = "8624807723";
Connection con = DriverManager.getConnection(url, username, password);
PreparedStatement pstmt = con.prepareStatement("DELETE FROM Employee WHERE ENo=?");
int employeeID = Integer.parseInt(args[0]);
pstmt.setInt(1, employeeID);
int rowsAffected = pstmt.executeUpdate();
System.out.println(rowsAffected + " row(s) deleted.");
pstmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
s25
Delete/* *********** SLIP 26 ********* */
ReplyDelete/* 1. Write a Java program to delete the details of given employee (ENo EName Salary).
Accept employee ID through command line. (Use PreparedStatement Interface) */
import java.sql.*;
public class Q1 {
public static void main(String[] args) {
try
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:postgresql:amresh";
String username = "postgres";
String password = "8624807723";
Connection con = DriverManager.getConnection(url, username, password);
PreparedStatement pstmt = con.prepareStatement("DELETE FROM Employee WHERE ENo=?");
int employeeID = Integer.parseInt(args[0]);
pstmt.setInt(1, employeeID);
int rowsAffected = pstmt.executeUpdate();
System.out.println(rowsAffected + " row(s) deleted.");
pstmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/* ******** SLIP 27 ********** */
ReplyDelete/* 1. Write a Java Program to display the details of College (CID, CName, address, Year)
on JTable. */
import java.sql.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class Q1 extends JFrame
{
static final String DB_URL = "jdbc:postgresql://localhost:5432/amresh";
static final String DB_USER = "postgres";
static final String DB_PASSWORD = "8624807723";
public Q1() {
super("College Details");
JTable table = new JTable();
String[] columnNames = {"CID", "CName", "Address", "Year"};
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
try {
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM college");
while (rs.next()) {
String cid = rs.getString("CID");
String cname = rs.getString("CName");
String address = rs.getString("Address");
int year = rs.getInt("Year");
model.addRow(new Object[] { cid, cname, address, year });
}
rs.close();
st.close();
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
table.setModel(model);
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
pack();
setVisible(true);
}
public static void main(String[] args) {
new Q1();
}
}
ReplyDelete/* 2. Write a java program to display name of currently executing Thread in multithreading. */
public class Q2 {
public static void main(String[] args) {
Thread t = Thread.currentThread();
System.out.println("Currently executing thread: " + t.getName());
}
}
s28
Delete/* ************** SLIP 29 **************** */
ReplyDelete/* 1. Write a Java program to display information about all columns in the DONAR table
using ResultSetMetaData. */
import java.sql.*;
public class Q1 {
public static void main(String[] args) {
String url = "jdbc:pstgresql://localhost:5432/amresh";
String username = "postgres";
String password = "8624807723";
try (Connection con = DriverManager.getConnection(url, username, password)) \
{
String query = "SELECT * FROM DONAR";
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
System.out.println("Column Name\t\tData Type");
System.out.println("------------------------------------------");
for (int i = 1; i <= columnCount; i++) {
String columnName = rsmd.getColumnName(i);
String dataType = rsmd.getColumnTypeName(i);
System.out.println(columnName + "\t\t\t" + dataType);
}
} catch (SQLException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
/* 2. Write a Java program to create LinkedList of integer objects and perform the following:
i. Add element at first position
ii. Delete last element
iii. Display the size of link list */
import java.util.LinkedList;
public class Q2 {
public static void main(String[] args) {
LinkedList list = new LinkedList<>();
list.addFirst(10);
list.addFirst(20);
list.addFirst(30);
System.out.println("LinkedList after adding elements at first position: "+list);
list.removeLast();
System.out.println("LinkedList after deleting last element: " + list);
System.out.println("Size of LinkedList: " + list.size());
}
}
/* ******** SLIP 30 ******* */
ReplyDelete/* 1. Write a java program for the implementation of synchronization. */
class Counter {
private int count = 0;
public synchronized void increment() {
count++;
}
public int getCount() {
return count;
}
}
class MyThread extends Thread {
private Counter counter;
public MyThread(Counter counter) {
this.counter = counter;
}
public void run() {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
}
}
public class Q1 {
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();
MyThread thread1 = new MyThread(counter);
MyThread thread2 = new MyThread(counter);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println("Count: " + counter.getCount());
}
}
/* 2. Write a Java Program for the implementation of scrollable ResultSet. Assume Teacher
table with attributes (TID, TName, Salary) is already created. */
import java.sql.*;
public class Q2 {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con =DriverManager.getConnection("jdbc:postgresql:amresh", "postgres", "8624807723");
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = st.executeQuery("SELECT * FROM Teacher");
rs.last();
int rowCount = rs.getRow();
rs.beforeFirst();
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
System.out.print(rsmd.getColumnName(i) + "\t");
}
System.out.println();
while (rs.next()) {
System.out.print(rs.getInt("TID") + "\t");
System.out.print(rs.getString("TName") + "\t");
System.out.print(rs.getInt("Salary") + "\t");
System.out.println();
}
rs.close();
st.close();
con.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}