Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
49 views

Assignment 3 Java

Assignment 3 java

Uploaded by

vanshitpatel23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Assignment 3 Java

Assignment 3 java

Uploaded by

vanshitpatel23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Assignment 3

1. Write a Java Program to implement Thread by Extending Thread


Class.

Program:

import java.io.*;

class th1 extends Thread


{
public void run()
{
for(int i=0;i<=10;i++)
{
System.out.println("Thread 1 : "+i);
}
}
}

class th2 extends Thread


{
public void run()
{
for(int i=0;i<=10;i++)
{
System.out.println("Thread 2 : "+i);
}
}
}
class thr
{
public static void main(String[] args) {

th1 t1=new th1();


th2 t2=new th2();
t1.start();
t2.start();
}
}
Output:
2. Write a Java Program to Implement Thread by Implement
Runnable Method.
Program:
import java.io.*;

class th1 implements Runnable


{
public void run()
{
for (int i=1;i<=5;i++)
{
System.out.println("Thread 1 : "+i);
}
}
}

class th2 implements Runnable


{
public void run()
{
for (int i=1;i<=5;i++)
{
System.out.println("Thread 2 : "+i);
}
}
}

class thred
{
public static void main(String[] args) {

th1 t1=new th1();


Thread thr1=new Thread(t1);
t1.run();

th2 t2=new th2();


Thread thr2=new Thread(t2);
t2.run();
}
}
Output:
3. Write a Java Program to Implement Thread Priority.
Program:
import java.io.*;

class thprio extends Thread


{
public void run()
{
System.out.println("Running Thread Name
:"+Thread.currentThread().getName());

System.out.println("Priority Thread Name


:"+Thread.currentThread().getPriority());
}

public static void main(String[] args) {

thprio t1=new thprio();


t1.setPriority(Thread.NORM_PRIORITY);
t1.start();
}
}

Output:
4. Write a Java Program to print “Hello World” in Applet.
Program:
import java.applet.Applet;
import java.awt.*;

public class helo extends Applet


{
public void paint(Graphics g)
{
g.drawString("Hello World",100,100);
}
}
/*
<applet code="helo.class" height="600" width="700"></applet>*/
Output:
5. Write a Java Program to Draw Circle in Applet
Program:
import java.io.*;
import java.applet.Applet;
import java.awt.*;

public class cir extends Applet


{
public void paint(Graphics g)
{
g.drawOval(172,88,704,620);
}
}
/*<applet code="cir.class" width="800" height="800">
</applet>*/

Output:
6. Write a Java Program to Draw Rectangle in Applet.
Program:
import java.io.*;
import java.applet.Applet;
import java.awt.*;
public class rec extends Applet
{
public void paint(Graphics g)
{
g.drawRect(100,100,400,400);

}
}

/*
<applet code="rec.class" height="1000" width="1000"></applet>
*/

Output:
7. Write a Java Program to Draw Triangle in Applet
Program:
import java.io.*;
import java.awt.*;
import java.applet.Applet;

public class tr extends Applet


{
public void paint(Graphics g)
{
g.drawLine(256,82,456,354);
g.drawLine(256,82,110,354);
g.drawLine(110,354,456,354);
}
}
/*
<applet code="tr.class" height="800" width="800"></applet>
*/

Output:
8. Write a Java Program to Draw Pentagon in Applet
Program:
import java.io.*;
import java.awt.*;
import java.applet.Applet;

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;
g.drawPolygon(xPoints, yPoints, numPoints);
}
}

/*
<applet code="pant.class" height="800" width="800">

</applet>
*/

Output:
9. Write a Java Program to Draw Smile Face in Applet.
Program:
import java.io.*;
import java.applet.Applet;
import java.awt.*;

public class smiley extends Applet


{
public void paint(Graphics g)
{
g.drawOval(80,70,150,150);
g.setColor(Color.BLACK);
g.fillOval(120,120,15,15);
g.fillOval(170,120,15,15);
g.drawArc(130,180,50,20,180,180);
}
}

/*
<applet code="smiley.class" height="700" width="800">
</applet>
*/

Output:
10. Write a java Program to implement String buffer all methods.
Program:
import java.io.*;

public class strbuff


{
public static void main(String[] args)
{
StringBuffer str= new StringBuffer("Hello ");
str.append("User");
System.out.println("After append:-"+str);
str.insert(5, "Java");
System.out.println("after Insert"+str);
str.delete(5, 10);
System.out.println("After delete:- " + str);
str.reverse();
System.out.println("After reverse:- " + str);
int length = str.length();
System.out.println("Length of StringBuffer:- " + length);
int capacity = str.capacity();
System.out.println("Capacity of StringBuffer: -" + capacity);
str.setLength(3);
System.out.println("After setLength:- " + str);
str.replace(0, 3, "Dear");
System.out.println("After replace:- " + str);
str.ensureCapacity(30);
System.out.println("Capacity after ensureCapacity:- " + str.capacity());
str.trimToSize();
System.out.println("Capacity after trimToSize:- " + str.capacity());
String s1 = str.toString();
System.out.println("String representation:- " + s1);
}
}

Output:
11. Write a Java Program to implement Singly Link List.
import java.io.*;
class Node {
int data;
Node next;

public Node(int data) {


this.data = data;
this.next = null;
}
}

class SinglyLinkedList {
private Node head;
public SinglyLinkedList() {
this.head = null;
}

public void insert(int data) {


Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
}

public void display() {


Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
}
public class link {
public static void main(String[] args) {
SinglyLinkedList list = new SinglyLinkedList();
list.insert(1);
list.insert(2);
list.insert(3);
list.display(); // Output: 1 2 3
}
}
Output

You might also like