Please help with simple applet.
Hey guys this is my first post. I am very new to java and have had trouble understanding this program out of a textbook, hopefully you guys can help me understand it.
package squareint;
import java.awt.Container;
import javax.swing.*;
public class SquareInt extends JApplet
{
public void init()
{
String output = "";
JTextArea outputArea = new JTextArea( 10, 20 );
Container c = getContentPane();
c.add( outputArea );
int result;
for( int x = 1; x <=10; x++)
{
result = square( x );
output += "The square of " + x +
" is " + result + "\n";
}
outputArea.setText( output );
}
public int square( int y )
{
return y * y;
}
}
The parts that i am stuck on are:
String output = "" (the textbook hasn't said anything about it)
Container c = getContentPane() (don't know what a container is, or what getContentPane() means)
result = square( x ) (where does square come from)
public int square( int y) (i think this is a method or something i haven't ever created two in one program before what does it mean public int, and what is y?)
Re: Please help with simple applet.
Re: Please help with simple applet.
Code:
String output = "";
This is a declaration with an initialization.
Code:
Container c = getContentPane();
A container is a somewhat generic class for holding GUI components, and getContentPage returns the ContentPane (which is a container) for the given object receiving the call.
Quote:
(where does square come from)
it comes from:
Code:
public int square( int y )
{
return y * y;
}
Quote:
public int, and what is y?
public is the scope, int is the return type, y is a parameter.
Sounds like before you go any further, you need to learn the essential parts of a java program:
- primitive types
- class structure
- method structure
- statement structure
- scope
- control structures
etc....
Time to do some reading!