Practical Program Slip
Practical Program Slip
Practical Program Slip
import java.awt.*;
import java.awt.event.*;
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( );
}
}
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();
}
}
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();
}
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.*;
String v;
ThreadVowel1(String k)
v=k;
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){}
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String n=sc.next();
tv.start();
Write a java program to display each alphabet after 2 seconds between ‘a’ to ‘z’.
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());
}
}
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.*;
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);
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);
x=me.getX();
y=me.getY();
t.setText("X="+x+" Y="+y);
x=me.getX();
y=me.getY();
class Slip2B
Answer :
import java.io.DataInputStream;
class Slip3A {
int n,r,sum=0;
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){
}else{
} 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.*;
int a,b;
a = x;
b = y;
super(x,y);
double area(){
return (a*b*3.14);
double volume(){
return (3.14*a*a*b);
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{
int r,h,s;
r = Integer.parseInt(dr.readLine());
h = Integer.parseInt(dr.readLine());
s = Integer.parseInt(dr.readLine());
Shape s1;
s1=c1;
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.*;
g.setFont(f);
setBackground(Color.RED);
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);
}
}
}