You are on page 1of 7

1)Write a java program to show Multithreding in java.

//class inherited from "Thread"


class ThreadClassDemo extends Thread {

private int number;

//class constructor

public ThreadClassDemo(int number) {

this.number = number;
}

//run method => execution code for thread


public void run() {

int counter = 0;

int numInt = 0;

//prints the number till specified number is reached, starting from 10

do {
numInt = (int) (counter + 10);

System.out.println(this.getName() + " prints " + numInt);

counter++;
}
while(numInt != number);

System.out.println("* Correct! " + this.getName() + "printed " + counter


+ " times.*");
}
}

public class MultiThread {

public static void main(String [] args) {

System.out.println("Mustafa Sayyed 261");

System.out.println("Starting thread_1...");

//create a thread class instance


Thread thread_1 = new ThreadClassDemo(10);

//start the thread thread_1

thread_1.start();

try {

//wait for thread_1 to die

thread_1.join();
}
catch (InterruptedException e) {

System.out.println("Thread interrupted.");
}

System.out.println("Starting thread_2...");

Thread thread_2 = new ThreadClassDemo(20);

//start thread_2
OUTPUT:-
2)Create a GUI application using Check boxes and its Listener.

import java.awt.*;
public class CheckboxExample1 {
// constructor to initialize
CheckboxExample1() {
// creating the frame with the title
Frame f = new Frame("Checkbox Mustafa261");
// creating the checkboxes
Checkbox checkbox1 = new Checkbox("C++");
checkbox1.setBounds(100, 100, 50, 50);
Checkbox checkbox2 = new Checkbox("Java", true);
// setting location of checkbox in frame
checkbox2.setBounds(100, 150, 50, 50);
// adding checkboxes to frame
f.add(checkbox1);
f.add(checkbox2);
// setting size, layout and visibility of frame
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
// main method
public static void main (String args[]) {
new CheckboxExample1();
} }
Output:-
3) Write a java program using Nested if else Statement.

class Posative {
public static void main(String[] args) {
System.out.println("Mustafa Sayyed 261");
int number = 10;

// checks if number is greater than 0


if (number > 0) {
System.out.println("The number is positive.");
}

// execute this block


// if number is not greater than 0
else {
System.out.println("The number is not positive.");
}

System.out.println("Statement outside if...else block");


}
}
OUTPUT:-

You might also like