Program to create an applet to accept a username as parameter and display number of characters.

In this program, we have to get the username from Applet's <PARAM> tag and display the length of the string. The value of parameter can be get using getParameter( ) method of applet. The length( )function is used to find length of given string.


PROGRAM
import java.applet.Applet;
import java.awt.Graphics;

public class CharCountParam extends Applet{

 String uname="", msg;
 int length;
 
 public void init() 
 {
  
  uname = getParameter("Username");
  length = uname.length();
    
  uname = "Hello " + uname;
  msg = "Length = " + length;
  }
 
 public void paint(Graphics g)
 {
  g.drawString(uname,50,50);
  g.drawString(msg, 50, 90);
 }
}

/* 
<applet code="CharCountParam" width=200 height=200>
<param name="Username" value="RAHUL">
</applet>
*/

OUTPUT 

C:\>javac CharCountParam.java 
C:\>appletviewer CharCountParam.java



Comments

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Program to input age from user and throw user-defined exception if entered age is negative

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.