Assignment 3
1. Write a Java Program to implement Thread by Extending Thread
Class.
Program:
import [Link].*;
class th1 extends Thread
{
public void run()
{
for(int i=0;i<=10;i++)
{
[Link]("Thread 1 : "+i);
}
}
}
class th2 extends Thread
{
public void run()
{
for(int i=0;i<=10;i++)
{
[Link]("Thread 2 : "+i);
}
}
}
class thr
{
public static void main(String[] args) {
th1 t1=new th1();
th2 t2=new th2();
[Link]();
[Link]();
}
}
Output:
2. Write a Java Program to Implement Thread by Implement
Runnable Method.
Program:
import [Link].*;
class th1 implements Runnable
{
public void run()
{
for (int i=1;i<=5;i++)
{
[Link]("Thread 1 : "+i);
}
}
}
class th2 implements Runnable
{
public void run()
{
for (int i=1;i<=5;i++)
{
[Link]("Thread 2 : "+i);
}
}
}
class thred
{
public static void main(String[] args) {
th1 t1=new th1();
Thread thr1=new Thread(t1);
[Link]();
th2 t2=new th2();
Thread thr2=new Thread(t2);
[Link]();
}
}
Output:
3. Write a Java Program to Implement Thread Priority.
Program:
import [Link].*;
class thprio extends Thread
{
public void run()
{
[Link]("Running Thread Name
:"+[Link]().getName());
[Link]("Priority Thread Name
:"+[Link]().getPriority());
}
public static void main(String[] args) {
thprio t1=new thprio();
[Link](Thread.NORM_PRIORITY);
[Link]();
}
}
Output:
4. Write a Java Program to print “Hello World” in Applet.
Program:
import [Link];
import [Link].*;
public class helo extends Applet
{
public void paint(Graphics g)
{
[Link]("Hello World",100,100);
}
}
/*
<applet code="[Link]" height="600" width="700"></applet>*/
Output:
5. Write a Java Program to Draw Circle in Applet
Program:
import [Link].*;
import [Link];
import [Link].*;
public class cir extends Applet
{
public void paint(Graphics g)
{
[Link](172,88,704,620);
}
}
/*<applet code="[Link]" width="800" height="800">
</applet>*/
Output:
6. Write a Java Program to Draw Rectangle in Applet.
Program:
import [Link].*;
import [Link];
import [Link].*;
public class rec extends Applet
{
public void paint(Graphics g)
{
[Link](100,100,400,400);
}
}
/*
<applet code="[Link]" height="1000" width="1000"></applet>
*/
Output:
7. Write a Java Program to Draw Triangle in Applet
Program:
import [Link].*;
import [Link].*;
import [Link];
public class tr extends Applet
{
public void paint(Graphics g)
{
[Link](256,82,456,354);
[Link](256,82,110,354);
[Link](110,354,456,354);
}
}
/*
<applet code="[Link]" height="800" width="800"></applet>
*/
Output:
8. Write a Java Program to Draw Pentagon in Applet
Program:
import [Link].*;
import [Link].*;
import [Link];
public class pant extends Applet
{
public void paint(Graphics g)
{
int[] xPoints = {100, 150, 200, 175, 125};
int[] yPoints = {150, 50, 150, 200, 200};
int numPoints = 5;
[Link](xPoints, yPoints, numPoints);
}
}
/*
<applet code="[Link]" height="800" width="800">
</applet>
*/
Output:
9. Write a Java Program to Draw Smile Face in Applet.
Program:
import [Link].*;
import [Link];
import [Link].*;
public class smiley extends Applet
{
public void paint(Graphics g)
{
[Link](80,70,150,150);
[Link]([Link]);
[Link](120,120,15,15);
[Link](170,120,15,15);
[Link](130,180,50,20,180,180);
}
}
/*
<applet code="[Link]" height="700" width="800">
</applet>
*/
Output:
10. Write a java Program to implement String buffer all methods.
Program:
import [Link].*;
public class strbuff
{
public static void main(String[] args)
{
StringBuffer str= new StringBuffer("Hello ");
[Link]("User");
[Link]("After append:-"+str);
[Link](5, "Java");
[Link]("after Insert"+str);
[Link](5, 10);
[Link]("After delete:- " + str);
[Link]();
[Link]("After reverse:- " + str);
int length = [Link]();
[Link]("Length of StringBuffer:- " + length);
int capacity = [Link]();
[Link]("Capacity of StringBuffer: -" + capacity);
[Link](3);
[Link]("After setLength:- " + str);
[Link](0, 3, "Dear");
[Link]("After replace:- " + str);
[Link](30);
[Link]("Capacity after ensureCapacity:- " + [Link]());
[Link]();
[Link]("Capacity after trimToSize:- " + [Link]());
String s1 = [Link]();
[Link]("String representation:- " + s1);
}
}
Output:
11. Write a Java Program to implement Singly Link List.
import [Link].*;
class Node {
int data;
Node next;
public Node(int data) {
[Link] = data;
[Link] = null;
}
}
class SinglyLinkedList {
private Node head;
public SinglyLinkedList() {
[Link] = null;
}
public void insert(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node current = head;
while ([Link] != null) {
current = [Link];
}
[Link] = newNode;
}
}
public void display() {
Node current = head;
while (current != null) {
[Link]([Link] + " ");
current = [Link];
}
[Link]();
}
}
public class link {
public static void main(String[] args) {
SinglyLinkedList list = new SinglyLinkedList();
[Link](1);
[Link](2);
[Link](3);
[Link](); // Output: 1 2 3
}
}
Output