Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock Exam

 
beginner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone explain this for me? Thanks in advance.
import java.applet.Applet;
import java.awt.*;
public class Sample extends Applet{
private String text = "Hello World";
public void init () {
add(new Label(text));
}
public Sample(String string) {
text = string;
}
}
The HTML page:
//<html>
//<title>SampleApplet</title>
//<body>
//<applet code ="Sample.class" width=200 height=100></applet>
//</body>
//</html>

Answer is "Generates a running error"
 
kevin jia
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For an applet you can't provide a constructer with arguments.
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For an applet you may provide a constructer with arguments,
but if you want to use it, you have to instantiate the Applet from an application.
When you instatiate and applet from an HTML page the default constructor is called so you must have one. If you need to pass values from the HTML page to the applet you may use the PARAMETER tag in the HTML page and the getParameter() method in your applet code
 
beginner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after I remove the code
public Sample (String string){
text = string;
}
still get the run time error
my html code like this:
html
title SampleApplet
body
applet code="Sample.class" width=200 height=200 /applet
/body
/html
I left the angle blanket out because it'll display nothing
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic