0% found this document useful (0 votes)
26 views6 pages

Java Applet Code Examples

The document contains code examples for Java applets and programs. It includes an applet that displays "Hello World", an applet that adds two numbers, examples of running applets from the command line, a normal Java program that takes input and calls a method, programs to calculate factorials recursively and using a loop, and HTML code to embed the applets in a web page.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

Java Applet Code Examples

The document contains code examples for Java applets and programs. It includes an applet that displays "Hello World", an applet that adds two numbers, examples of running applets from the command line, a normal Java program that takes input and calls a method, programs to calculate factorials recursively and using a loop, and HTML code to embed the applets in a web page.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

//java applet code for hello world

import [Link].*;

import [Link].*;

/*

<applet code="appletEx" width=100 height=100>

</applet>

*/

public class appletEx extends Applet

public void paint(Graphics g)

[Link]("Hello World", 50, 50);

HTML code fro above program

<html>

<applet code="appletEx", width=100 height=100>

</applet>

</html>

[Link]
Run method in command line

C:\Users\awill\Desktop\practical>set path="C:\Program Files\Java\jdk-10.0.2\bin"

C:\Users\awill\Desktop\practical>javac -deprecation [Link]

[Link]: warning: [deprecation] Applet in [Link] has been deprecat

ed

public class appletEx extends Applet

1 warning

C:\Users\awill\Desktop\practical>appletviewer [Link]

Warning: Applet API and AppletViewer are deprecated.

Warning: Can't read AppletViewer properties file: C:\Users\awill\.hotjava\proper

ties Using defaults.

//program to add two variable and print in applet

import [Link].*;

import [Link].*;

public class addapplet extends Applet

public void paint(Graphics g)

int a=10;

[Link]
int b=20;

int c= a+b;

String str= "Sum is: " + [Link](c);

[Link](str,100,100);

<html>

<body>

<p>this will print sum in applet</p>

<applet code="addapplet" height=400 width=400>

</applet>

</body>

</html>

//normal java program

import [Link].*;

public class test

public static void main(String args[])

[Link]("Enter a value");

Scanner sc= new Scanner([Link]);

[Link]
int value= [Link]();

[Link]("value in main:"+value);

test t =new test();

int aw=[Link](value);

[Link]("now return value:"+aw);

int awill(int v)

[Link]("value in awill:"+v);

return 20;

//program to find factorial by recursion

class FactRec

public static void main(String args[])

int i,fact=1;

int number=5;//It is the number to calculate factorial

fact = factorial(number);

[Link]("Factorial of "+number+" is: "+fact);

[Link]
static int factorial(int n)

if (n == 0)

return 1;

else

return(n * factorial(n-1));

} }

//program to find factorial in a loop

import [Link].*;

public class facto

public static void main(String args[])

{ boolean b;

String f;

Scanner sc= new Scanner([Link]);

Scanner ch=new Scanner([Link]);

do{

[Link]("Enter a value for factorial:");

int n=[Link]();

int fact=fact(n);

[Link]("The factorial is:"+fact);

[Link]
[Link]("more factorial press y . For exit press n");

f=[Link]();

[Link]("you entered::"+f);

} while([Link]("yes")|| [Link]("y"));

static int fact(int n)

if(n==0 || n==1)

return 1;

else return n*fact(n-1);

[Link]

You might also like