Blogging for beginners




CHAPTER CONTENT
1. Introduction

The course starts off with the introduction of blogging in the late 1990s, details about the development of content and Panda updates, which significantly affected blogging, as well as the most relevant blogging statistics. All of this illustrates the importance of blogging and its influence on growing a business and reaching online users.

2. What Is Blogging?

In this chapter, you will learn how it all started, how blogging became mainstream with the introduction of WordPress, what a blog and blogosphere are. The chapter also includes a section on basic differences between blogs and websites, as well as types of blogs. The goal here is to provide a full definition of blogging which is later needed for understanding the rest of the course.

3. Blogging Platforms

The first step when creating a blog is choosing the platform. Here you can learn about commonly used platforms for creating a blog with main features highlighted for each of them. The final section of this chapter is focused on the process of choosing a platform, and things you need to consider in this process.

4. Blogging Essentials

The selection of blogging platform is only the first step, after which there is a series of essentials each blogger should take care of before it is time to publish content. The essentials help create an organized strategy to explore the full benefits of blogging.

5. Creating a Blogging Strategy

To ensure you succeed with blogging, regardless if that is your hobby or a full-time job, you will need a roadmap with clear goals, resources, and metrics used for evaluation. You need to create a blogging strategy that starts with the current situation and includes where you want your blogging activity to take you next.

6. Integrating Blogging into a Business Strategy

The statistics show that blogging can significantly boost your business, which is why you will discover ideas regarding integration of blogging into your business strategy. Starting from what business blogging is, how to create an online presence and how to blog as your business, this chapter will guide you through this process and show you how a blog can complement your website and your business promotion.

7. The Benefits of Blogging

This chapter focuses on the benefits of blogging, but it also shows why blogging is so popular as well as how to overcome obstacles most bloggers face.

8. Blogger Outreach and Guest Blogging

Some of the most used strategies for increasing blog visibility include blogger outreach and guest blogging. In this chapter, you will learn what those tactics are, what they include and how your blog can benefit from them.

9. What Is Vlogging?

The rise of video in online marketing has contributed to the increasing popularity of vlogging, a form of blogging which uses video as a content format. This section of the course will show you what a vlog is, commonly used types of vlogs, and how you can use vlogging in business.

10. Making a Living Through Blogging

Blogging reached massive popularity in the 2000s, after which numerous opportunities for earning money through a blog appeared. From affiliate marketing to banners and sponsored posts, this chapter shows the most commonly used methods to earn money from blogging. A special focus is on turning your blog into a business with ideas on how to do so.

11. Tips to Help You Run a Successful Blog

This chapter includes multiple strategies to help you increase the success of your blog, starting from responsive design, blog optimization, usage of social networks to share blog content, content optimization, etc.

12. Mistakes to Avoid When Blogging

Things that might negatively affect your blog performance are covered in this chapter. The most common mistakes such as not thinking about target group or neglecting online promotion and content distribution can reduce your blog visibility and success, so it is better to be aware of these mistakes. Each mistake comes with a practical solution on how to avoid or fix it.

13. The Most Popular Tools for Bloggers

Arranged into five groups, these tools are intended to help you optimize your workflow, improve your content and content promotion, and successfully monitor the performance of your content. You should choose the ones you find most useful in your daily work routine.

14. Blogging as a Part of Online Marketing

This chapter explores how blogging is integrated into online marketing and how together these represent a unified strategy. Content marketing, SEO, social media marketing, and lead generation are all sections which are analyzed in relation to blogging.

15. Blogging Glossary

The glossary chapter includes definitions for the terminology related to online marketing which is used in this course.

16. Questionnaire

This questionnaire is going to help you revise what you have learned throughout the course. It consists of 50 multiple-answer questions.

17. Conclusion

The summary of blogging is shown inside the conclusion, where you can see the key differences between three types of blogging depending on the type of business you run. Also, there is a list of things to learn from popular blogs, that actually summarizes the entire blogging strategy in the main takeaways.


Write a Socket program in java in which client accept a number, send it to the server, server calculates its factorial and sends result to the client.

factclient.java

        import java.io.*;
        import java.net.*;
class factclient{
    public static void main(String argv[]) throws Exception
    {
        String n;
        DatagramSocket clientSocket = new DatagramSocket();
        byte []send=new byte[102];
        byte []resive=new byte[102];
        BufferedReader inFromUser = new BufferedReader(new                InputStreamReader(System.in));
        System.out.println("\nEnter Number : ");
        n=inFromUser.readLine();
        InetAddress ipadd= InetAddress.getByName("localhost");
        send=n.getBytes();
        DatagramPacketsendPck=new DatagramPacket(send,send.length,ipadd,6870);
        clientSocket.send(sendPck);
        DatagramPacket resPck=new DatagramPacket(resive,resive.length);
        clientSocket.receive(resPck);
        String fact=new String(resPck.getData());
        System.out.println("FROM SERVER: " +n+"! = " +fact);
        clientSocket.close();
    }
}

       
 factserver.java

        import java.io.*;
        import java.net.*;
class factserver{
    public static void main(String argv[]) throws Exception
    {
        String num,res;
        int i,no;
        long fact;
        System.out.println("Server is ready");
        DatagramSocket serverSocket = new DatagramSocket(6870);
        byte []send=new byte[102];
        byte []resive=new byte[102];
        DatagramPacket resPck=new DatagramPacket(resive,resive.length);
        serverSocket.receive(resPck);
        num=new String(resPck.getData());
        num=num.trim();
        no=Integer.parseInt(num);
        fact=1;
        for(i=1;i<=no;i++)
            fact=fact*i;
        res=Long.toString(fact);
        send=res.getBytes();
        InetAddress ipadd= resPck.getAddress();
        int port=resPck.getPort();
        DatagramPacketsendPck=new DatagramPacket(send,send.length,ipadd,port);
        serverSocket.send(sendPck);
    }
}

Write a Socket program in java for chatting application. (Use AWT)

ClientChatForm.java

        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;
        import java.awt.print.PrinterException;
        import java.io.DataInputStream;
        import java.io.DataOutputStream;
        import java.io.IOException;
        import java.net.InetAddress;
        import java.net.Socket;
        import java.net.UnknownHostException;
        import java.sql.Time;
        import javax.swing.JButton;
        import javax.swing.JFrame;
        import javax.swing.JPanel;
        import javax.swing.JTextArea;
        import javax.swing.JTextField;

public class ClientChatForm extends JFrame implements ActionListener {
    static Socket conn;
    JPanel panel;
    JTextField NewMsg;
    JTextArea ChatHistory;
    JButton Send;

    public ClientChatForm() throws UnknownHostException, IOException 
{
        panel = new JPanel();
        NewMsg = new JTextField();
        ChatHistory = new JTextArea();
        Send = new JButton("Send");
        this.setSize(500, 500);
        this.setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        panel.setLayout(null);
        this.add(panel);
        ChatHistory.setBounds(20, 20, 450, 360);
        panel.add(ChatHistory);
        NewMsg.setBounds(20, 400, 340, 30);
        panel.add(NewMsg);
        Send.setBounds(375, 400, 95, 30);
        panel.add(Send);
        56        Send.addActionListener(this);
        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) {
// TODO Auto-generated catch block                    e.printStackTrace();
                }
            }
        }
    }
    @Override    public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub        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) {
// TODO Auto-generated catch block                    e2.printStackTrace();
                }
            }
            NewMsg.setText("");
        }
    }
    public static void main(String[] args) throws UnknownHostException,
            IOException {
        ClientChatForm chatForm = new ClientChatForm();
    }
}


serverChatForm.java

        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;
        import javax.swing.JButton;
        import javax.swing.JFrame;
        import javax.swing.JPanel;
        import javax.swing.JTextArea;
        import javax.swing.JTextField;
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 {
        panel = new JPanel();
        NewMsg = new JTextField();
        ChatHistory = new JTextArea();
        Send = new JButton("Send");
        this.setSize(500, 500);
        this.setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        panel.setLayout(null);
        this.add(panel);
        ChatHistory.setBounds(20, 20, 450, 360);
        panel.add(ChatHistory);
        NewMsg.setBounds(20, 400, 340, 30);
        panel.add(NewMsg);
        Send.setBounds(375, 400, 95, 30);
        panel.add(Send);
        this.setTitle("Server");
        Send.addActionListener(this);
        server = new ServerSocket(2000, 1, InetAddress.getLocalHost());
        ChatHistory.setText("Waiting for Client");
        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) {
// TODO Auto-generated catch block                    e.printStackTrace();
                }
            }
        }
    }
    @Override    public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub        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) {
// TODO Auto-generated catch block                    e2.printStackTrace();
                }
            }
            NewMsg.setText("");
        }
    }
    public static void main(String[] args) throws UnknownHostException,
            IOException {
        new serverChatform();
    }
}

Create a JSP page to accept a number from an user and display it in words: Example: 123 – One Two Three. The output should be in red color.

NumberWord.html


<html>
<body>
<form method="get" action="NumberWord.jsp">
<fieldset>
<legend>Enter Any Number </legend>
<input type=text name=num><br><br>
</fieldset>
<div align=center>
<input type="submit" value="Display">
</div>
</form>
<body>
</html>
       

 NumberWord.jsp

<html>
<body>
<font color=red>
<%! int i,n;
        String s1;
        %>
<%
        s1=request.getParameter("num");
        n=s1.length();
        i=0;
        do        {
        char ch=s1.charAt(i);
        switch(ch)
        {
        case '0': out.println("Zero ");
        break;
        case '1': out.println("One ");
        break;
        case '2': out.println("Two ");
        break;
        51        case '3': out.println("Three ");
        break;
        case '4': out.println("Four ");
        break;
        case '5': out.println("Five ");
        break;
        case '6': out.println("Six ");
        break;
        case '7': out.println("Seven ");
        break;
        case '8': out.println("Eight ");
        break;
        case '9': out.println("Nine ");
        break;
        }
        i++;
        }
        while(i<n);
        %>
</font>
</body>
</html>

Write a JSP script to check whether given mail ID is valid or not. (Mail ID should contain one @ symbol and atleast one Dot(.) symbol)

Email.html

<html>
<body>
<form name=f1 action="EmailCheck.jsp">
<fieldset>
<legend>Enter Email Id...!!!</legend>
        Enter Email Id:<input type="text" name="t1" >
</fieldset>
<div align=center>
<input type="submit" name="Submit" value="Submit">
</div>
</form>
</body>
</html>
        

Emailcheck.jsp
<html>
<body>
<%@ page language="java" %>
<%
        String email=request.getParameter("t1");
        if(email.contains("@") && email.contains("."))
        {
        out.println("Given Email Id is Valid");
        }
        else        {
        out.println("Given Email id is not Valid");
        }
        %>
</body>
</html>

Write a JSP script to accept the details of Student (RNo, SName, Gender, Computer_ Knowledge , Class) and display it on the browser. Use appropriate controls for accepting data.

student.html

<html>
<body>
<form method=get action="Student.jsp">
<fieldset>
<legend>Enter Student Details...!!!</legend>
        Enter Roll No :&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp <input type=text name=rno><br><br>
Enter Student Name:&nbsp <input type=text name=sname><br><br>
Enter Gender: &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp <input type=text name=gen><br><br>
Computer Knowledge:<input type=text name=ckn><br><br>
Enter Class: &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp<input type=text name=cl><br><br>
</fieldset>
<div align=center>
<input type=submit value="Save">
</div>
</form>
</body>
</html>


student.html
<html>
<body>
<%@ page import="java.io.*;" %>
<%! int rno;
        String name,gen,ckn,cl; %>
<%
        try        {
        rno=Integer.parseInt(request.getParameter("rno"));
        name=request.getParameter("sname");
        gen=request.getParameter("gen");
        ckn=request.getParameter("ckn");
        46        cl=request.getParameter("cl");
        out.println("Roll No: "+rno+"<br>");
        out.println("Name : "+name+"<br>");
        out.println("Gender :"+gen+"<br>");
        out.println("Comp Knowledge : "+ckn+"<br>");
        out.println("Class :"+cl+"<br>");
        }
        catch(Exception e)
        {
        out.println(e);
        }
        %>
</body>
</html>

Write a JSP script to accept username, store it into the session, compare it with password in another jsp file, if username matches with password then display appropriate message in html file.

index.html
<html>
<head>
<title>Login Page</title>
</head>
<body>
<form action="checkdetails.jsp">
<fieldset>
<legend>Enter User Id and Password...!!!</legend>
        UserId:&nbsp &nbsp &nbsp <input type="text" name="id" /> <br><br>
Password: <input type="text" name="pass" /> <br><br>
</fieldset>
<div align=center>
<input type="submit" value="Sign In!!"/>
</div>
</form>
</body>
</html>


checkdetails.jsp
<html>
<head>
<title>Check Credentials</title>
</head>
<body>
<%
        String uid=request.getParameter("id");
        String password=request.getParameter("pass");
        session.setAttribute("session-uid", uid);
        if(uid.equals("Sachin") && password.equals("Sachin"))
        {
        response.sendRedirect("success.jsp");
        }
        else        {
        43        response.sendRedirect("failed.jsp");
        }
        %>
</body>
</html>
 
success.jsp
<html>
<head><title>Success Page</title></head>
<body>
<%
        String data=(String)session.getAttribute("session-uid");
        out.println(" Login Successfully...!!!");
        %>
</body>
</html>
        

failed.jsp
<html>
<head><title>Sign-in Failed Page</title></head>
<body>
<%
        String data2=(String)session.getAttribute("session-uid");
        out.println(" User Id and Password are wrong. Please try Again.");
        %>
</body>
</html>

Write a SERVLET program in java to accept details of student (SeatNo, Stud_Name, Class, Total_Marks). Calculate percentage and grade obtained and display details on page.

Student.html
<html>
<body>
<form name="f1" method="Post" action="http://localhost:8080/Servlet/Student">
<fieldset>
<legend><b><i>Enter Student Details :</i><b></legend>
        Enter Roll No :&nbsp <input type="text" name="txtsno"><br><br>
Enter Name :&nbsp &nbsp <input type="text" name="txtnm"><br><br>
Enter class :&nbsp &nbsp &nbsp <input type="text" name="txtclass"><br><br>
<fieldset>
<legend><b><i>Enter Student Marks Details :</i><b></legend>
        Subject 1 :&nbsp &nbsp &nbsp <input type="text" name="txtsub1"><br><br>
Subject 2 :&nbsp &nbsp &nbsp <input type="text" name="txtsub2"><br><br>
Subject 3 :&nbsp &nbsp &nbsp <input type="text" name="txtsub3"><br><br>
</fieldset>
</fieldset>
<div align=center>
<input type="submit" value="Result">
</div>
</form>
</body>
</html>

Student.php
        import java.io.*;
        import javax.servlet.*;
        import javax.servlet.http.*;
public class Student extends HttpServlet
{
    public void doPost(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
    {
        int sno,s1,s2,s3,total;
        String snm,sclass;
        float per;
        40        res.setContentType("text/html");
        PrintWriter out=res.getWriter();
        sno=Integer.parseInt(req.getParameter("txtsno"));
        snm=req.getParameter("txtnm");
        sclass=req.getParameter("txtclass");
        s1=Integer.parseInt(req.getParameter("txtsub1"));
        s2=Integer.parseInt(req.getParameter("txtsub2"));
        s3=Integer.parseInt(req.getParameter("txtsub3"));
        total=s1+s2+s3;
        per=(total/3);
        out.println("<html><body>");
        out.print("<h2>Result of student</h2><br>");
        out.println("<b><i>Roll No :</b></i>"+sno+"<br>");
        out.println("<b><i>Name :</b></i>"+snm+"<br>");
        out.println("<b><i>Class :</b></i>"+sclass+"<br>");
        out.println("<b><i>Subject1:</b></i>"+s1+"<br>");
        out.println("<b><i>Subject2:</b></i>"+s2+"<br>");
        out.println("<b><i>Subject3:</b></i>"+s3+"<br>");
        out.println("<b><i>Total :</b></i>"+total+"<br>");
        out.println("<b><i>Percentage :</b></i>"+per+"<br>");
        if(per<50)
            out.println("<h1><i>Pass Class</i></h1>");
        else if(per<55 && per>50)
            out.println("<h1><i>Second class</i></h1>");
        else if(per<60 && per>=55)
            out.println("<h1><i>Higher class</i></h1>");
        out.close();
    }
}

Web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
<servlet-name>Student</servlet-name>
<servlet-class>Student</servlet-class>
</servlet>
        41<servlet-mapping>
<servlet-name>Student</servlet-name>
<url-pattern>/Student</url-pattern>
</servlet-mapping>
</web-app>

Write a SERVLET program that provides information about a HTTP request from a client, such as IP address and browser type. The servlet also provides information about the server on which the servlet is running, such as the operating system type, and the names of currently loaded servlets.

serverInfo.java
        import java.io.*;
        import javax.servlet.*;
        import javax.servlet.http.*;
public class serverInfo extends HttpServlet implements Servlet
{
    protected void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
    {
        res.setContentType("text/html");
        PrintWriter pw=res.getWriter();
        pw.println("<html><body><h2>Information about Http Request</h2>");
        pw.println("<br>Server Name: "+req.getServerName());
        pw.println("<br>Server Port: "+req.getServerPort());
        pw.println("<br>Ip Address: "+req.getRemoteAddr());
//pw.println("<br>Server Path: "+req.getServerPath());        pw.println("<br>Client Browser: "+req.getHeader("User-Agent"));
        pw.println("</body></html>");
        pw.close();
    }
}
Web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
<servlet-name>serverInfo</servlet-name>
<servlet-class>ServerInfo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>serverInfo</servlet-name>
<url-pattern>/server</url-pattern>
</servlet-mapping>
</web-app>

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)

VisitServlet.java
        import java.io.*;
        import javax.servlet.*;
        import javax.servlet.http.*;
public class VisitServlet 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++;
    }
}
Web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
<servlet-name>VisitServlet</servlet-name>
<servlet-class>VisitServlet</servlet-class>
</servlet>
        36<servlet-mapping>
<servlet-name>VisitServlet</servlet-name>
<url-pattern>/VS</url-pattern>
</servlet-mapping>
</web-app>

Write a Multithreading program in java to convert smile face into the crying face after 5 seconds and vice versa(Use Applet).

/*<applet code= "smileface.class" height="300" width="300"></applet>*/import java.awt.*;
import java.applet.*;
public class smileface extends Applet implements Runnable
{
    int aflag;
    Thread t;
    public void init() {
        t=new Thread(this); aflag=0;
        t.start();
    }
    public void run()
    {
        try        {
            if (aflag==0)
            {
                t.sleep(1000);
                aflag=1;
            } else            {
                t.sleep(1000);
                aflag=0;
            }
            repaint();
            run();
        }
        catch(Exception e)
        {
        }
    }
    public void paint(Graphics g) {
        34        g.drawOval(100,100,100,100);
        g.fillOval(120,125,20,20);
        g.fillOval(160,125,20,20);
        g.drawLine(150,135,150,165);
        if (aflag==0)
        { g.drawArc(140,160,20,20,0,-180);
            aflag=1;
        }
        else        {
            g.drawArc(140,160,20,20,0,180);
            aflag=0;
        }
    }
}

Write a java program which will display name and priority of current thread. Change name of Thread to MyThread and priority to 2. Display the details of Thread.

public class MainThread
{
    public static void main(String arg[])
    {
        Thread t=Thread.currentThread();
        System.out.println("Current Thread:"+t);
//Change Name        t.setName("My Thread ");
        System.out.println ("After the name is Changed:"+t);
        try        {
            for(int i=2;i>0;i--)
            {
                System.out.println(i);
                Thread.sleep(1000);
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}
**OUTPUT***

        D:\TY\Adv Java>javac MainThread.java
        D:\TY\Adv Java>java MainThread
        Current Thread:Thread[main,5,main]
        After the name is Changed:Thread[My Thread ,5,main]
        2        1

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 javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MultiThread extends JFrame implements ActionListener
{
    Container cc;
    JButton b1,b2;
    JTextField t1;
    MultiThread()
    {
        setVisible(true);
        setSize(1024,768);
        cc=getContentPane();
        setLayout(null);
        t1=new JTextField(500);
        cc.add(t1);
        t1.setBounds(10,10,1000,30);
        b1=new JButton("start");
        cc.add(b1);
        b1.setBounds(20,50,100,40);
        b1.addActionListener(this);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==b1)
        {
            new Mythread();
        }
    }
    class Mythread extends Thread
    {
        Mythread()
        {
        start();
        }
        public void run()
        {
            for(int i=1;i<=100;i++)
            {
                try                {
                    Thread.sleep(1000);
                }
                catch (InterruptedException e) {
                }
                t1.setText(t1.getText()+""+i+"\n");
//System.out.println()            }
        }
    }
    public static void main(String arg[])
    {
        new MultiThread().show();
    }
}

Write a java program using multithreading to execute the threads sequentially .(Use Synchronized Method)

class SynchronizationDemo
{
    synchronized void printTable(int n,String s2) //synchronized method synchronized    {
        System.out.println(s2);
        for(int i=1;i<=5;i++)
        {
            System.out.println(n*i);
            try            {
                Thread.sleep(400);
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }
    }
}
class MyThread1 extends Thread
{
    Table t;
    String s1="Table of five";
    MyThread1(Table t)
    {
        this.t=t;
    }
    public void run()
    {
        t.printTable(7,s1);
    }
}
class MyThread2 extends Thread
{
    Table t;
    String s1="Table of Ten";
    MyThread2(Table t)
    {
        this.t=t;
    }
    29    public void run()
    {
        t.printTable(9,s1);
    }
}
public class SynchronizationDemo
{
    public static void main(String args[])
    {
        Table obj = new Table();//only one object        MyThread1 t1=new MyThread1(obj);
        MyThread2 t2=new MyThread2(obj);
        t1.start();
        t2.start();
    }
}

**OUTPUT***
        D:\TY\Adv Java>javac SynchronizationDemo.java
        D:\TY\Adv Java>java SynchronizationDemo
        Table of five
        7        14        21        28        35        Table of Ten
        9        18        27        36        45

Write a Multithreading program in java using Runnable interface to move text on the frame as follow

import java.awt.*;
import java.awt.event.*;
class MoveText extends Frame implements Runnable
{
    Label l1;
    Thread t;
    int x,y,side;
    public MoveText()
    {
        setLayout(null);
        l1=new Label("Sachin ");
        l1.setFont(new Font("",Font.BOLD,14));
        l1.setForeground(Color.red);
        setSize(400,400);
        setVisible(true);
        t=new Thread(this);
        t.start();
        x=5;
        y=200;side=1;
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        });
    }
    public void run()
    {
        try        {
            if(side==1)
            {
                t.sleep(50);
                l1.setBounds(x+=5,y-=5,70,15);
                add(l1);
                26                if(y==20)
                    side=2;
            }
            if(side==2)
            {
                t.sleep(50);
                l1.setBounds(x+=5,y+=5,70,15);
                add(l1);
                if(y==200)
                    side=3;
            }
            if(side==3)
            {
                t.sleep(50);
                l1.setBounds(x-=5,y+=5,70,15);
                add(l1);
                if(y==390)
                    side=4;
            }
            if(side==4)
            {
                t.sleep(50);
                l1.setBounds(x-=5,y-=5,70,15);
                add(l1);
                if(x==0)
                {
                    side=1;
                    x=0;y=200;
                }
            }
        }catch(Exception e)
        {
            System.out.println(e);
        }
        run();
    }
    public static void main(String args[])
    {
        new MoveText();
    }
    27}

Write a java program to simulate traffic signal using multithreading

/*<applet code= "signal.class" height="600" width="600"></applet>*/import java.awt.*;
import java.applet.*;
public class signal extends Applet implements Runnable
{
    int r,g1,y,i;
    Thread t;
    public void init()
    {
        r=0;g1=0;y=0;
        t=new Thread(this);
        t.start();
    }
    public void run()
    {
        try        { for (i=24;i>=1;i--)
        {
            t.sleep(100);
            if (i>=16 && i<24)
            {
                g1=1;
                repaint();
            }
            if (i>=8 && i<16)
            {
                y=1;
                repaint();
            }
            if (i>=1 && i<8)
            {
                r=1;
                repaint();
            }
        }
            23            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.drawString("start",200,200);
        if (r==1)
        { g.setColor(Color.red);
            g.fillOval(100,100,100,100);
            g.drawOval(100,100,100,100);
            g.drawString("stop",200,200);
            r=0;
        }
        if (g1==1)
        { g.setColor(Color.green);
            g.fillOval(100,225,100,100);
            g1=0;
            g.drawOval(100,225,100,100);
            g.drawString("go",200,200);
        }
        if (y==1)
        { g.setColor(Color.yellow);
            g.fillOval(100,350,100,100);
            y=0;
            g.drawOval(100,350,100,100);
            g.drawString("slow",200,200);
        }
    }
}