Showing posts with label BCA2022. Show all posts
Showing posts with label BCA2022. Show all posts

Saturday, 21 May 2022

Write a Java program to display given extension files from a specific directory on server machine.

 DOWNLOAD     SLIP14Q2


/**
* STEPS TO RUN CODE
* Step 01 compile the code
* Step 02 run the code
* Step 03 give a file directory location to find file
* Step 04 give a file extension name without (.)
* and then u will get all file names
*/



import java.io.*;

/**
* Created by : Amar Ghugare
* created on : 21-05-2022 20:44
* Purpose of class : Write a Java program to display
* given extension files from a specific directory on
* server machine.
*/
public class Slip14Q2 {
public static void main(String[] args) throws IOException {
String userFileExtension = "";

BufferedReader bufferedInputStream = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("GIVE A FILE LOCATION TO FIND SPECIFIC TYPE OF FILE");
String userFileLocation = bufferedInputStream.readLine();
File fileLocation = new File(userFileLocation);
if (fileLocation.exists()) {
System.out.println("\nGIVE A FILE EXTENSION TO FIND IN GIVEN DIRECTORY");
userFileExtension = bufferedInputStream.readLine();
String finalUserFileExtension = userFileExtension;

String[] filenamesInDIR = fileLocation.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.toLowerCase().endsWith("." + finalUserFileExtension)) {
return true;
} else {
return false;
}
}
});
assert filenamesInDIR != null;
if(filenamesInDIR.length!=0){
for (String fileName:filenamesInDIR){
System.out.println(fileName);
}
}else {
System.out.println("THERE IS NO FILE OF THAT EXTENSION");
}
}else {
System.out.println("THIS FILE NOT EXIST IF YOU WANT TO RECHECK THEN TYPE 'Y' OR 'N'.");
String recheckOption = bufferedInputStream.readLine();
if("Y".equals(recheckOption.toUpperCase())){
main(args);
}
}
}
}

Write a program in java which will show lifecycle (creation, sleep, and dead) of a thread. Program should print randomly the name of thread and value of sleep time. The name of the thread should be hard coded through constructor. The sleep time of a thread will be a random integer in the range 0 to 4999.

 DOWNLOAD       SLIP12Q2




/**
* Created by : Amar Ghugare
* created on : 21-05-2022 20:21
* Purpose of class : Write a program in java
* which will show lifecycle (creation, sleep, and dead)
* of a thread. Program should print randomly the name
* of thread and value of sleep time. The name of the
* thread should be hard coded through constructor.
* The sleep time of a thread will be a random integer
* in the range 0 to 4999.
*/
public class Slip12Q2 extends Thread {
public Slip12Q2(String s) {
super(s);
}

public void run() {
System.out.println(getName() + " thread created.");
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("THREAD NAME : "+this.getName()+"\n"+"THREAD PRIORITY : "+this.getPriority());
int s = (int) (Math.random() * 5000);
System.out.println(getName() + "is sleeping for : " + s + " mile sec");
try {
Thread.sleep(s);
} catch (Exception ignored) {
}
}
}
}
class ThreadLifeCycle{
public static void main(String[] args) {
Slip12Q2 t1=new Slip12Q2("Groot"),t2=new Slip12Q2("Amar");
t1.start();
t2.start();
try {
t1.join();
t2.join();
}
catch(Exception ignored) {}
System.out.println(t1.getName()+"thread dead.");
System.out.println(t2.getName()+"thread dead.");
}
}

Write a java program to count the number of records in a table.

 DOWNLOAD      SLIP12Q1         SQLFORIT       SQL DRIVER



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
* Created by : Amar Ghugare
* created on : 21-05-2022 19:54
* Purpose of class :
*/
public class Slip12Q1 {
public static void main(String[] args) throws Exception {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "");
System.out.println("Connection established......");
Statement stmt = con.createStatement();
String query = "select count(*) from Cricketers_Data";
ResultSet rs = stmt.executeQuery(query);
rs.next();
int count = rs.getInt(1);
System.out.println("Number of records in the cricketers_data table: "+count);
}
}

Write a java program in multithreading using applet for Digital watch.

 DOWNLOAD    SLIP8Q2



import java.applet.Applet;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
* Created by : Amar Ghugare
* created on : 21-05-2022 19:15
* Purpose of class :
*/
public class Slip8Q2 extends Applet implements Runnable {

Thread t1 = null;
int hours = 0, minutes = 0, seconds = 0;
String time = "";
public void init() {
setBackground( Color.green);
}
public void start() {
t1 = new Thread( this );
t1.start();
}

public void run() {
try {
while (true) {
Calendar cal = Calendar.getInstance();
hours = cal.get( Calendar.HOUR_OF_DAY );
if ( hours > 12 ) hours -= 12;
minutes = cal.get( Calendar.MINUTE );
seconds = cal.get( Calendar.SECOND );
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
Date d = cal.getTime();
time = formatter.format( d );
repaint();
Thread.sleep( 1000 );
}
}
catch (Exception ignored) { }
}
public void paint( Graphics g ) {
g.setColor( Color.blue );
g.setFont(new Font("",Font.PLAIN,100));
g.drawString( time, 100, 150 );
}
}
/*<applet code= "Slip8Q2.class" height="300" width="600"></applet>*/

Write a SERVLET program which counts how many times a user has visited a web page. If user is visiting the page for the first time, display a welcome message. If the user is revisiting the page, display the number of times visited. (Use Cookie)

 DOWNLOAD  Slip6Q2



import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* Created by : Amar Ghugare
* created on : 21-05-2022 18:56
* Purpose of class : Write a SERVLET program
* which counts how many times a user has visited a web
* page. If user is visiting the page for the first time,
* display a welcome message. If the
* user is revisiting the page, display the number
* of times visited. (Use Cookie)
*/
public class Slip6Q2 extends HttpServlet{
static int i=1;
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String k=String.valueOf(i);
Cookie c=new Cookie("visit",k);
response.addCookie(c);
int j=Integer.parseInt(c.getValue());
if(j==1) {
out.println("Welcome to web page ");
}
else {
out.println("You are visited at "+i+" times");
}
i++;
}
}

Write a java program to blink image on the Frame continuously.

 DOWNLOAD  SLIP6Q1



import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
* Created by : Amar Ghugare
* created on : 21-05-2022 18:36
* Purpose of class :
*/
public class Slip6Q1 extends JComponent {
BufferedImage image;
boolean showImage;
int x = -1;
int y = -1;
Random r;

Slip6Q1() throws IOException {

// put your image reading code here..

image = new BufferedImage(32,32,BufferedImage.TYPE_INT_ARGB);
Graphics g = image.createGraphics();
g.setColor(Color.ORANGE);
g.fillOval(0,0,32,32);

r = new Random();
ActionListener listener = new ActionListener(){
public void actionPerformed(ActionEvent ae) {
if (image!=null) {
if (!showImage) {
int w = image.getWidth();
int h = image.getHeight();
int rx = getWidth()-w;
int ry = getHeight()-h;
if (rx>-1 && ry>-1) {
x = r.nextInt(rx);
y = r.nextInt(ry);
}
}
showImage = !showImage;
repaint();
}
}
};
Timer timer = new Timer(600,listener);
timer.start();
setPreferredSize(new Dimension(150,100));
JOptionPane.showMessageDialog(null, this);
timer.stop();
}

public void paintComponent(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(0,0,getWidth(),getHeight());
if (showImage && image!=null) {
g.drawImage(image,x,y,this);
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
new Slip6Q1();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}

Write a java program in multithreading using applet for Traffic signal

 

DOWNLOAD  SIGNAL



import java.applet.Applet;
import java.awt.*;
/**
* Created by : Amar Ghugare
* created on : 21-05-2022 17:32
* Purpose of class : Write a java program in multithreading
* using applet for Traffic signal
*/
public class Signal extends Applet implements Runnable {
int i,j;
Thread t;
int timeOfSignal,signalColor;

public void init() {
timeOfSignal=90;signalColor=0;
t=new Thread(this);
t.start();
}
public void run() {
try{
if (signalColor==0){
for (j=0;j<90;j++){
timeOfSignal=timeOfSignal-1;
Thread.sleep(500);
repaint();
}
signalColor=1;
timeOfSignal=15;
}
if(signalColor==1){
for (j=0;j<15;j++){
timeOfSignal=timeOfSignal-1;
Thread.sleep(500);
repaint();
}
signalColor=2;
timeOfSignal=90;
}
if(signalColor==2){
for (j=0;j<90;j++){
timeOfSignal=timeOfSignal-1;
Thread.sleep(500);
repaint();
}
signalColor=0;
timeOfSignal=90;
}
if (i==0) {
run();
}
}
catch(Exception e) {
}
}

public void paint(Graphics g) {
g.drawOval(100,100,100,100);
g.drawOval(100,225,100,100);
g.drawOval(100,350,100,100);
g.setFont(new Font("",Font.PLAIN,24));
g.drawString(String.valueOf(timeOfSignal),400,150);

if(signalColor==0&&!(j==90)){
g.setColor(Color.red);
g.fillOval(100,100,100,100);
g.drawOval(100,100,100,100);
g.drawString("stop",400,430);
}
if(signalColor==1&&!(j==15)) {
g.setColor(Color.yellow);
g.fillOval(100, 225, 100, 100);
g.drawOval(100, 225, 100, 100);
g.drawString("slow", 400, 430);
}
if (signalColor==2&&!(j==90)) {
g.setColor(Color.green);
g.fillOval(100,350,100,100);
g.drawOval(100,350,100,100);
g.drawString("go",400,430);
}
}
}
/*<applet code= "Signal.class" height="600" width="600"></applet>*/

Friday, 20 May 2022

Write a java program using applet for bouncing ball, for each bounce color of ball should change randomly.

 DOWNLOAD            Slip3Q2

/**
* Step to run the file
* 1. save this file as "Slip3Q2.java"
* 2. compile the file javac Slip3Q2.java
* 3. run the file java Slip3Q2
*/

import java.applet.Applet;
import java.awt.*;
import java.util.Random;

public class Slip3Q2 extends Applet implements Runnable {
Thread t;
int moveSide, yAxis;
boolean sideChanged=false;
private int rCode=0,gCode=0,bCode=0;

public void init(){
t=new Thread(this);
t.start();
moveSide=0; yAxis=0;
}
public void run() {
try{
if (moveSide==0){
Thread.sleep(10);
yAxis=yAxis+5;
repaint();
}
if(moveSide==1) {
Thread.sleep(10);
yAxis=yAxis-5;
repaint();
}
}catch(Exception ignored){ }
run();
}
public void paint(Graphics g) {
if(moveSide==0) {
g.setColor(new Color(rCode,gCode,bCode));
g.fillOval(150,yAxis+10,20,20);
if(yAxis==400) {
Random random = new Random();
rCode = random.nextInt(256);
gCode = random.nextInt(256);
bCode = random.nextInt(256);
moveSide=1;
}
}if(moveSide==1) {
g.setColor(new Color(rCode,gCode,bCode));
g.fillOval(150,yAxis-10,20,20);
if(yAxis==0) {
Random random = new Random();
rCode = random.nextInt(256);
gCode = random.nextInt(256);
bCode = random.nextInt(256);
moveSide=0;
}
}
}
}
/*<applet code="Slip3Q2.class" height=400 width=350></applet>*/






Write a socket program in Java to check whether given number is prime or not. Display result on client terminal

DOWNLOAD        PrimeServer         PrimeClient

 /**

 * STEPS TO RUN THE CODE
* 1. COMPILE SERVER AND CLIENT
* 2. FIRST RUN SERVER FILE
* 3. SECOND RUN CLIENT FILE
* 4. GIVE A NUMBER FROM CLIENT CMD
*/
/**
* SAVE AS PrimeServer.java
*/
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
* Created by : Amar Ghugare
* created on : 20-05-2022 18:35
* Purpose of class : server to attend client request
*/
public class PrimeServer {
public static void main(String args[])throws Exception {
ServerSocket ss=new ServerSocket(3333);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("server here..");
String str="",str2="";
str=din.readUTF();
int n=Integer.parseInt(str);
System.out.println("given no is: "+n);
int i,m=0,flag=0;
m=n/2;
if(n==0||n==1){
dout.writeUTF(n+" is not prime number");
System.out.println(n+" is not prime number");
}else{
for(i=2;i<=m;i++){
if(n%i==0){
dout.writeUTF(n+" is not prime number");
System.out.println(n+" is not prime number");
flag=1;
break;
}
}
dout.writeUTF(n+" is prime number");
if(flag==0) { System.out.println(n+" is prime number"); }
}//end of else
dout.flush();
din.close();
s.close();
ss.close();
}
}

/**
* SAVE AS PrimeClient.java
*/

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

/**
* Created by : Amar Ghugare
* created on : 20-05-2022 18:34
* Purpose of class : to give a number to server
*/
public class PrimeClient {
public static void main(String args[]) throws Exception {
Socket s = new Socket("localhost", 3333);
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter any number");
String str = "", str2 = "";
str = br.readLine();
dout.writeUTF(str);
dout.flush();
str2 = din.readUTF();
System.out.println(str2);
dout.close();
s.close();
}
}


Write a socket program in java for chatting application.(Use Swing)

 

DOWNLOAD   ServerChatForm     ClientChatForm

// Step 1 SAVE THIS FILE AS ServerChatForm.java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

/**
* Created by : Amar Ghugare
* created on : 20-05-2022 17:32
* Purpose of class : to create a server of chat
*/
public class ServerChatForm extends JFrame implements ActionListener {

static ServerSocket server;
static Socket conn;
JPanel panel;
JTextField NewMsg;
JTextArea ChatHistory;
JButton Send;
DataInputStream dis;
DataOutputStream dos;
public ServerChatForm()throws UnknownHostException, IOException{
this.setSize(500, 500);
this.setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Server");

panel = new JPanel();
panel.setLayout(null);
this.add(panel);

NewMsg = new JTextField();
NewMsg.setBounds(20, 400, 340, 30);
panel.add(NewMsg);

ChatHistory = new JTextArea();
ChatHistory.setText("Waiting for Client");
ChatHistory.setBounds(20, 20, 450, 360);
panel.add(ChatHistory);

Send = new JButton("Send");
Send.setBounds(375, 400, 95, 30);
Send.addActionListener(this);
panel.add(Send);


server = new ServerSocket(2000, 1, InetAddress.getLocalHost());
conn = server.accept();
ChatHistory.setText(ChatHistory.getText() + '\n' + "Client Found");

while (true) {
try {
DataInputStream dis = new DataInputStream(conn.getInputStream());
String string = dis.readUTF();
ChatHistory.setText(ChatHistory.getText() + '\n'+ "Client:"+string);
} catch (Exception e1) {
ChatHistory.setText(ChatHistory.getText() + '\n'+ "Message sending fail:Network Error");
try {
Thread.sleep(3000);
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if ((e.getSource() == Send) && (NewMsg.getText() != "")) {
ChatHistory.setText(ChatHistory.getText() + '\n' + "ME:"+ NewMsg.getText());
try {
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeUTF(NewMsg.getText());
} catch (Exception e1) {
try {
Thread.sleep(3000);
System.exit(0);
} catch (InterruptedException e2) {
e2.printStackTrace();
}
}
NewMsg.setText("");
}
}

public static void main(String[] args)throws UnknownHostException,IOException {
new ServerChatForm();
}
}


// STEP 2 SAVE THIS FILE AS ClientChatForm.java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
* Created by : Amar Ghugare
* created on : 20-05-2022 17:44
* Purpose of class : to make a client for chat
*/
public class ClientChatForm extends JFrame implements ActionListener {
static Socket conn;
JPanel panel;
JTextField NewMsg;
JTextArea ChatHistory;
JButton Send;

public ClientChatForm()throws UnknownHostException, IOException{
this.setSize(500, 500);
this.setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

panel = new JPanel();
panel.setLayout(null);
this.add(panel);

NewMsg = new JTextField();
NewMsg.setBounds(20, 400, 340, 30);
panel.add(NewMsg);

ChatHistory = new JTextArea();
ChatHistory.setBounds(20, 20, 450, 360);
panel.add(ChatHistory);

Send = new JButton("Send");
Send.setBounds(375, 400, 95, 30);
Send.addActionListener(this);
panel.add(Send);


conn = new Socket(InetAddress.getLocalHost(), 2000);

/** for remote pc use ip address of server with function* InetAddress.getByName("Provide ip here")* ChatHistory.setText("Connected to Server");*/
ChatHistory.setText("Connected to Server");
this.setTitle("Client");
while (true) {
try {
DataInputStream dis = new DataInputStream(conn.getInputStream());
String string = dis.readUTF();
ChatHistory.setText(ChatHistory.getText() + '\n'+ "Server:"+ string);
} catch (Exception e1) {
ChatHistory.setText(ChatHistory.getText() + '\n'+ "Message sending fail:Network Error");
try {
Thread.sleep(3000);
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if ((e.getSource() == Send) && (NewMsg.getText() != "")) {
ChatHistory.setText(ChatHistory.getText() + '\n' + "Me:"+ NewMsg.getText());
try {
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeUTF(NewMsg.getText());
} catch (Exception e1) {
ChatHistory.setText(ChatHistory.getText() + '\n' + "Message sending fail:Network Error");
try {
Thread.sleep(3000);
System.exit(0);
} catch (InterruptedException e2) {
e2.printStackTrace();
}
}
NewMsg.setText("");
}
}

public static void main(String[] args)throws UnknownHostException,IOException {
new ClientChatForm();
}
}


// STEP 3 COMPILE BOTH FILES 
// STEP 4 RUN SERVER CHAT FIRST THEN RUN CLIENT CHAT




Write a java program to scroll the text from left to right and vice versa continuously.


DOWNLOAD SLIP1Q1

 SAVE CODE AS     "Slip1Q1"

TO RUN    appletviewer Slip1Q1.java



import java.applet.Applet;
import java.awt.*;

/**
* Created by : Amar Ghugare
* created on : 20-05-2022 16:02
* Purpose of class :
* Write a java program to scroll the text from
* left to right and vice versa continuously.
*/

public class Slip1Q1 extends Applet implements Runnable{
String banner="Hello I am Groot";
Thread t = null;
int x;
public void paint(Graphics g) {
Font a = new Font("Impact",Font.BOLD,14);
g.setFont(a);
g.drawString(banner, x, 100);
}
public void start() {
t = new Thread( this );
t.start();
}
    public void init(){
        setBackground(Color.black);
    setForeground(Color.green);
    }
@Override
public void run() {
try {
for(int i =0;i<400;i++){
repaint(10);
x=i;
Thread.sleep( 100);
}
}catch (Exception ignored){}
}
}
/**
* <applet code="Slip1Q1.class" width="600" height="300">
* </applet>
*/

Write a Java program to display given extension files from a specific directory on server machine.

 DOWNLOAD     SLIP14Q2 /** * STEPS TO RUN CODE * Step 01 compile the code * Step 02 run the code * Step 03 give a file directory locatio...