Back to Applets page
Click in the boxes below, select all and copy to grab the applet
HTML listing:
<HTML> <HEAD> <TITLE>Listing 5.7</TITLE> </HEAD> <BODY BGCOLOR=WHITE> <APPLET CODEBASE="classes" CODE="Applet7.class" WIDTH=500 HEIGHT=500> </APPLET> </BODY> </HTML>
Applet listing:
/* This applet demonstrates type conversion via object methods */ import java.awt.*; import java.applet.Applet; public class Applet7 extends Applet{ Double myDouble = new Double(Math.PI); Byte myByte = new Byte(myDouble.byteValue()); Short myShort = new Short(myDouble.shortValue()); Integer myInt = new Integer(myDouble.intValue()); Long myLong = new Long(myDouble.longValue()); Float myFloat = new Float(myDouble.floatValue()); Font f = new Font("TimesRoman",Font.BOLD,16); public void init() { setBackground(Color.white); } public void paint(Graphics g) { g.setFont(f); g.drawString("The value of pi as a byte is: "+this.myByte, 10 , 20); g.drawString("The value of pi as a short is: "+this.myShort, 10 , 40); g.drawString("The value of pi as an integer is: "+this.myInt, 10 , 60); g.drawString("The value of pi as a long is: "+this.myLong, 10 , 80); g.drawString("The value of pi as a float is: "+this.myFloat, 10 , 100); g.drawString("The value of pi as a double is: "+this.myDouble, 10 , 120); } }