Program to accept the value of temperature in Celsius using param tag and display the temperature in Fahrenheit on Applet

In this program, we pass temperature in Celcius as a parameter to applet and get it in applet's init( ) method to convert it in Fahrenheit. After that we will display the result on applet. The formula for the conversion from Celcius to Fahrenheit is given below:
temp_in_fah = temp_in_cel * 1.8 + 32
To convert temperatures in degrees Celsius to Fahrenheit, multiply by 1.8 (or 9/5) and add 32. For example: 15°C * 1.8 + 32 = 59°F


PROGRAM
import java.awt.*;  // import awt package
import java.applet.*; // import applet package

public class CelsiFahrApplet extends Applet 
{
    String msg;
    float celsius,fahrenheit;
    
    public void init() // override init() method
    {
        msg = getParameter("Temp");  // receiving parameter value
        celsius = Float.parseFloat(msg); // Convert string to int
        System.out.println(celsius);
        fahrenheit = (1.8f * celsius)+32; // Formula F = ((9/5)*C)+32
    }
    
    public void paint(Graphics g)
    {
        g.drawString("Temperature in Fahrenheit = "+fahrenheit,10,50); // display message on applet
    }
}
/*  <applet code=CelsiFahrApplet.class height=100 width=300>
  <param name="Temp" value="15">
 </applet>*/
OUTPUT 

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



Comments

Popular posts from this blog

Develop an Android application to display “Hello World!” on screen

Program to design an applet which draws a circle (having color BLUE) inside a triangle (having color YELLOW)

Build a Simple Android App to Display Student Details