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>*/

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...