|
Here you can see how to use a simple method with one parameter. The parameter is used to change the y-coodinate of the text output. Everything that is done more than once, should be put inside a method to make it reusable. Remember: elegance always pays off!
//Sourcecode
import java.awt.*;
import java.applet.*;
//Introduces methods
public class Project2 extends Applet
{
//We need a global graphics object if we use functions which use graphics:
public Graphics g;
//This is our method. It's only parameter is the y-coordinate of the text string.
public void Hallo(int y)
{
g.setColor(Color.black);
g.drawString("Hallo Java!",120,y);
}
public void paint (Graphics gr)
{
g=gr;
//We call our method several times with different parameters
Hallo(50);
Hallo(100);
Hallo(150);
Hallo(200);
Hallo(250);
}
}
The most common basic variable types in Java are:
| Name |
What is it |
Range |
Standard value |
Example |
| boolean |
logical |
true or false |
false |
true |
| int |
32 bit signed number |
-231 .. +231-1 |
0 |
123789 |
| long |
64 bit signed number |
-263 .. +263-1 |
0 |
123456789 |
| float |
32 bit floating point number |
ca. 7 places accuracy |
0.0 |
3.14 |
| double |
64 bit floating point number |
ca. 15 places accuracy |
0.0 |
0.00032424 |
| String |
text |
- |
"" |
"This is a text" |
To declare a variable, just write the type (int, float, String, Font etc.) followed by a blank and the variable name and a semicolon. The variable name can be almost any name you can imagine (except for the the reserved words of the Java language, no number at first place and no blanks inside the name), but it should be short and descriptive, of course. In the following examples you will quickly see what I mean. Note that Java is a case sensitive language, you have to be careful about upper/lowercase letters!
You can define variables almost anywhere you like, but you will usually declare them either on top of the class, or (if they only have to be visible inside a method) inside the method, right after the method head. You can declare several variables at the same time: int a, b, c;. You can also initialize a variable at the same time it is declared: int a=10;, String s="Hi!";and you can use a mixture: int a, b=3, c=10;. If you don't initialize a variable when you declare it, Java assigns a standard value (see above table), but don't count on it! In most cases it is neccessary to initialize a variable before using it.
In this applet you can see how to use variables of type String (for text strings) and type int (for integer numbers, without fractional part). To use numbers for text output, you can simply add the number to the string with a + sign. If you have only a number variable to put out with drawString, you can put empty "" before your number, in order to convert them to a string.
//Sourcecode
import java.awt.*;
import java.applet.*;
//Introduces variable output
public class Project3 extends Applet
{
//We need a global graphics object if we use functions using graphics:
public Graphics g;
//This is our method. It's only parameter is the y-coordinate of the text string.
public void Hallo(int y)
{
//str is a variable of type String
String str="Hallo Java!";
//our x-coordinate is of type integer
int x=50;
g.setColor(Color.black);
g.drawString(str+" The y-coodinate is: "+y, x, y);
}
public void paint (Graphics gr)
{
g=gr;
//We call our method several times with different parameters
Hallo(50);
Hallo(100);
Hallo(150);
Hallo(200);
Hallo(250);
}
}
|