[go: up one dir, main page]

0% found this document useful (0 votes)
21 views18 pages

Practical Program Slip

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 18

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 java.awt.*;
import java.awt.event.*;

public class Slip12 extends Frame implements ActionListener,Runnable


{
Thread y;
TextField t;
Button b;
Slip12()
{
t=new TextField(20);
b=new Button("start");
setLayout(new FlowLayout());
add(b);
add(t);
b.addActionListener(this);
setSize(300,300);
setVisible(true);
y=new Thread(this);
}
public void actionPerformed(ActionEvent e)
{
String msg=e.getActionCommand();
if(msg.equals("start"))
{
y.start();
}
}
public void run()
{
for(int i=1;i<=100;i++)
{
t.setText(i+"");
try
{
Thread.sleep(100);
}
catch(Exception e){}
}
}

public static void main(String[] d)


{
new Slip12();
}

Write a java program to blink image on the Frame continuously.

import java.awt.*;
import java.awt.event.*;
class slip8_1 extends Frame implements Runnable
{
Thread t;
Label l1;
int f;
slip8_1( )
{
t=new Thread(this);
t.start( );
setLayout(null);
l1=new Label("Hello Java");
l1.setBounds(100,100,100,40);
add(l1);
setSize(300,300);
setVisible(true);
f=0;
}
public void run( )
{
try
{
if(f==0)
{
t.sleep(200);
l1.setText(" ");
f=1;
}
if(f==1)
{
t.sleep(200);
l1.setText("hello java");
f=0;
}
}
catch(Exception e)
{
System.out.println(e);
}
run( );
}
public static void main(String a[ ])
{
new slip8_1( );
}
}

A) Write a java program using multithreading for the following:


1. Display all the odd numbers between 1 to n.
2. Display all the prime numbers between 1 to n.

public class NumberPrinter {


public static void main(String[] args) {
int n = 100; // Change the value of n as needed

// Create threads for odd numbers and prime numbers


Thread oddThread = new Thread(new OddNumberPrinter(n));
Thread primeThread = new Thread(new PrimeNumberPrinter(n));

// Start the threads


oddThread.start();
primeThread.start();
}

static class OddNumberPrinter implements Runnable {


private final int n;

public OddNumberPrinter(int n) {
this.n = n;
}

@Override
public void run() {
System.out.println("Odd numbers between 1 to " + n + ":");
for (int i = 1; i <= n; i += 2) {
System.out.print(i + " ");
}
System.out.println();
}
}

static class PrimeNumberPrinter implements Runnable {


private final int n;

public PrimeNumberPrinter(int n) {
this.n = n;
}

@Override
public void run() {
System.out.println("Prime numbers between 1 to " + n + ":");
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}
}
System.out.println();
}

private boolean isPrime(int num) {


if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
}

Write a java program to display name and priority of a Thread.

public class ThreadInfoExample {


public static void main(String[] args) {

Thread thread = new Thread();

System.out.println("Thread Name: " + thread.getName());


System.out.println("Thread Priority: " + thread.getPriority());
}
}

Write a java program to calculate factorial of a number. (Use sleep () method).

import java.util.*;
public class Slip18_q1
{
public static void main(String []args)
{
//Take input from the user
//Create an instance of the Scanner Class
Scanner sc=new Scanner(System.in);
//Declare and Initialize the variable
System.out.println("Enter the number: ");
int num=sc.nextInt();
int i=1,fact=1;
while(i<=num)
{
fact=fact*i;
i++;
}
try
{
Thread.sleep(2000);
System.out.println("Factorial of the number: "+fact);
}
catch(Exception e){}

}
}

Write a java program to accept a String from user and display each vowel from a String after 3
seconds.
Solution:

import java.util.*;

class ThreadVowel1 extends Thread

String v;

ThreadVowel1(String k)

v=k;

public void run()


{

for(int i=0;i<v.length();i++)

if( v.charAt(i)=='a' ||

v.charAt(i)=='e' ||

v.charAt(i)=='i' ||

v.charAt(i)=='o' ||

v.charAt(i)=='u')

System.out.println(v.charAt(i));

try

Thread.sleep(3000);

catch(Exception e){}

public class Slip17

public static void main(String args[])

{
Scanner sc=new Scanner(System.in);

System.out.println("Enter a string");

String n=sc.next();

ThreadVowel1 tv=new ThreadVowel1(n);

tv.start();

Write a java program to display each alphabet after 2 seconds between ‘a’ to ‘z’.

class Slip15 implements Runnable


{
public void run()
{
try
{
for(char c='A';c<='Z';c++)
{
System.out.println(c);
Thread.sleep(3000);
}
}
catch(Exception e)
{
System.out.println("error");
}
}

public static void main(String[] d)


{
Slip26 x=new Slip26();
Thread y=new Thread(x);
y.start();
//x.run();
}
}

Write a java program to display name of currently executing Thread in multithreading.

import java.util.*;
public class Slip13_q1 extends Thread {
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println("The thread name is
"+Thread.currentThread().getName());
}
}

public static void main(String []args)


{
Slip13_q1 t1= new Slip13_q1();
t1.setName("Main Thread");
t1.start();
Thread t2 = currentThread();
t2.setName("Current thread");
for(int i=0;i<5;i++)
{
System.out.println("The thread name is "+t1.currentThread().getName());
}
}

B) Design a screen in Java to handle the Mouse Events such as MOUSE_MOVED and
MOUSE_CLICK and display the position of the Mouse_Click in a TextField.

Answer :

import java.awt.*;

import java.awt.event.*;

class MyFrame extends Frame

TextField t,t1;

Label l,l1;

int x,y;

Panel p;

MyFrame(String title)
{

super(title);

setLayout(new FlowLayout());

p=new Panel();

p.setLayout(new GridLayout(2,2,5,5));

t=new TextField(20);

l= new Label("Mouse clicking");

l1= new Label("Mouse Movement");

t1=new TextField(20);

p.add(l);

p.add(t);

p.add(l1);

p.add(t1);

add(p);

addMouseListener(new MyClick());

addMouseMotionListener(new MyMove());

setSize(500,500);

setVisible(true);

class MyClick extends MouseAdapter

public void mouseClicked(MouseEvent me)

x=me.getX();

y=me.getY();
t.setText("X="+x+" Y="+y);

class MyMove extends MouseMotionAdapter

public void mouseMoved(MouseEvent me)

x=me.getX();

y=me.getY();

t1.setText("X="+ x +" Y="+y);

class Slip2B

public static void main(String args[])

MyFrame f = new MyFrame("Slip Number 4");

Slip 3 A) Write a ‘java’ program to check whether given number is Armstrong or


not. (Use static keyword)

Answer :
import java.io.DataInputStream;

class Slip3A {

static int temp;

public static void main(String args[]){

int n,r,sum=0;

DataInputStream dr = new DataInputStream(System.in);

try {

System.out.print("Enter Number");

n = Integer.parseInt(dr.readLine());

temp=n;

while(n>0){

r = n%10;

sum=sum+(r*r*r);
n=n/10;

if(temp==sum){

System.out.println(temp + " Is Armstrong Number : ");

}else{

System.out.println(temp + " Is Not Armstrong Number : ");

} catch (Exception e) {}

Slip 3 B) Define an abstract class Shape with abstract methods area () and
volume (). Derive abstract class Shape into two classes Cone and Cylinder. Write
a java Program to calculate area and volume of Cone and Cylinder.(Use Super
Keyword.)

Answer :

import java.io.*;

abstract class Shape{

int a,b;

Shape(int x, int y){

a = x;

b = y;

abstract double area();

abstract double volume();

class Cone extends Shape{

Cone(int x, int y){

super(x,y);

double area(){

return (a*b*3.14);

double volume(){

return (3.14*a*a*b);

class Cylinder extends Shape{


Cylinder(int x, int y){

super(x,y);

double area(){

return (2*3.14*a*b*3.14*a*b);

double volume(){

return (3.14*a*a*b);

class Slip3B{

public static void main(String args[]) throws Exception{

int r,h,s;

DataInputStream dr = new DataInputStream(System.in);

System.out.println("Enter Radius, Height and Side Values : ");

r = Integer.parseInt(dr.readLine());

h = Integer.parseInt(dr.readLine());

s = Integer.parseInt(dr.readLine());

Shape s1;

Cone c1 = new Cone(r,s);

s1=c1;

System.out.println("Area of Cone is : " + s1.area());

System.out.println("Volume of Cone is : " +s1.volume());

Cylinder cy = new Cylinder(r,h);


s1 =cy;

System.out.println("Area of Cylinder is : " + s1.area());

System.out.println("Area of Cylinder is : " + s1.volume());

A) Write a java program to display Label with text “Dr. D Y Patil College”, background
color Red and font size 20 on the frame.

Answer :

import java.awt.*;

import java.awt.event.*;

public class Slip7A extends Frame{

public void paint(Graphics g){

Font f = new Font("Georgia",Font.PLAIN,20);

g.setFont(f);

g.drawString("Dr D Y Patil College", 50, 70);

setBackground(Color.RED);

public static void main(String args[]){

Slip7A sl = new Slip7A();

sl.setVisible(true);

sl.setSize(200,300);

}
Slip 7 B) Write a java program to accept details of ‘n’ cricket player (pid, pname,
totalRuns, InningsPlayed, NotOuttimes). Calculate the average of all the players.
Display the details of player having maximum average. (Use Array of Object)

Answer :
import java.io.*;
class Cricket{
String Name;
int Total_runs;
int Notout;
int Inning;
float avg;

void accept(){
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
try{
System.out.print("Enter Name of Player : ");
Name = br.readLine();
System.out.print("Enter Total Runs of Player : ");
Total_runs = Integer.parseInt(br.readLine());
System.out.print("Enter Name of Tixes Not out : ");
Notout = Integer.parseInt(br.readLine());
System.out.print("Enter Innings played by players : ");
Inning = Integer.parseInt(br.readLine());
}catch (Exception e) {}
}
void average(){
avg = Total_runs/Inning;
System.out.println("Name : "+Name+"\nTotal runs : "+Total_runs+"\
nAvergae : "+avg+"\nInning : "+ Inning);
}
}
public class Slip7B {
public static void main(String args[]){
float max =0;
int n;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
try {
System.out.print("How many Players : ");
n = Integer.parseInt(br.readLine());
Cricket ob1[]= new Cricket[n];
for(int i=0; i<n; i++){
ob1[i] = new Cricket();
ob1[i].accept();
}
for(int i=0; i<n; i++){
ob1[i].average();
}
for(int i=0; i<n; i++){
if(max<ob1[i].avg){
max = ob1[i].avg;
}
}
System.out.println("-----------------------------\nMax avg : "+max);
} catch (Exception e) {
System.out.println("Error........."+e);
}
}
}

A) Write a ‘java’ program to display characters from ‘A’ to ‘Z’.


B) Write a java program to display all the vowels from a given string
C) Print following pattern.
5
45
345
2345
12345
D) Write a java program to accept a number from user, if it zero then throw user
defined Exception “Number Is Zero”, otherwise calculate the sum of first and last digit
of that number. (Use static keyword).
E) Define an Interface Shape with abstract method area(). Write a java program
tocalculate an area of Circle and Sphere.(use final keyword)
f) Write a java program to validate PAN number and Mobile Number. If it is invalid
then throw user defined Exception “Invalid Data”, otherwise display it.
class InvalidDataException extends Exception {
InvalidDataException(String string) {}
}

public class Q1_B {


public static void main(String[] args) {
String pan = "PAN123456789";
String mobile = "1234567890";
try {
if(pan.length() != 10) {
throw new InvalidDataException("Invalid PAN Number");
}
if(mobile.length() != 10) {
throw new InvalidDataException("Invalid Mobile Number");
}
System.out.println("Valid PAN Number and Mobile Number");
}
catch(InvalidDataException e) {
System.out.println("Invalid Data");
}
}
}
G) Write a menu driven java program using command line arguments for thefollowing:
1. Addition
2. Subtraction
3. Multiplication
4. Division.
H) Write a Java program to calculate area of Circle, Triangle & Rectangle.(Use
Method Overloading)
I) Create a package TYBBACA with two classes as class Student (Rno, SName, Per)
with a method disp() to display details of N Students and class Teacher (TID,
TName, Subject) with a method disp() to display the details of teacher who is
teaching Java subject.
J) Create a package named Series having three different classes to print series:
i. Fibonacci series
ii. Cube of numbers
iii. Square of numbers
Write a java program to generate ‘n’ terms of the above series
K) Write a java program to accept a number from user, If it is greater than 1000 then
throw user defined exception “Number is out of Range” otherwise display the factors
of that number.
L) Write a java program to accept a number from a user, if it is zero then throw
userdefined Exception “Number is Zero”. If it is non-numeric then generate an
error“Number is Invalid” otherwise check whether it is palindrome or not.

You might also like