Posts

Showing posts from April 24, 2014

Moving ball in applet

import java.applet.*;
import java.awt.*;
import java.lang.*;
import java.math.*;
import java.awt.Color;


/*<applet code="ball" height=100 width=100></applet>
  */
public class ball extends Applet implements Runnable

{
int i=0,j=0;

Thread t;
public void init()
{
t=new Thread(this);
t.start();
}
public void run()
{
int sec=0;
setSize(1200,1200);
try
{
while(true)
{
repaint();
sec+=1;

t.sleep(100);

}
}catch(Exception e)
{
System.out.println(e);
}
}

public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
setBackground(Color.black);

Dimension d= getSize();
g.setColor(Color.black);
g.fillRect(0,0,d.width,d.height);
g.setColor(Color.white);
g.fillOval(i+10,j+10,100,100);
i=i+10;
g.setColor(Color.black);


}
}

Font List in java applet

import java.awt.*;
import java.applet.*;

/*
<applet code="Fontlist" height=1000 width=190 ></applet>*/
public class Fontlist extends Applet {

  private String[] availableFonts;

  public void init () {
    Toolkit t = Toolkit.getDefaultToolkit();
    availableFonts = t.getFontList();
  }

  public void paint(Graphics g) {
    for (int i = 0; i < availableFonts.length; i++) {
      g.drawString(availableFonts[i], 5, 15*(i+1));
    }
  }

}

User defined exception for negative values entered by the user

import java.io.*;
import java.lang.*;
class E extends Exception
{
E(String s)
{
super(s);
}
}
class excep
{
public static void main(String args[])throws E
{
DataInputStream d= new DataInputStream(System.in);
System.out.println("Enter a no");
try
{
int a=Integer.parseInt(d.readLine());
sub(a);
}
catch(Exception e)
{
System.out.println(e);
}


}

public static void sub(int x)throws E
{
if(x<0)
throw new E("Invalid");
}


}

Program to check whether a number is special or not

import java.io.*;
import java.lang.*;
class special{

static int r,n,s=0,fact=1,i;

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

n=Integer.parseInt(args[0]);
while(n>0)
{
r=n%10;
fact=1;
for(i=1;i<=r;i++)
fact=fact*i;
s=s+fact;
n=n/10;
}


if(Integer.parseInt(args[0])==s)
System.out.println("Special");
else
System.out.println("NOT");

}

}

Input a natural number >1 and output its prime factors

import java.io.*;
import java.lang.*;
class p{
static int i,c;
static int prim(int n)
{
c=0;
for(i=1;i<=n;i++)
if(n%i==0)
c=c+1;

if(c==2)
{
System.out.print(n+",");
return(1);

}
else
return(0);
}
public static void main(String args[])throws Exception{
int no=Integer.parseInt(args[0]);

while(no>=2)
{
for(int j=2;j<=no;j++)
{
if(no%j==0&&prim(j)==1)
{
no=no/j;
break;

}
}
}
System.out.print('\b');
}
}