In Java Example: Algorithm and Program For Area Of Square, we met the following program which calculates the area of a square:
import java.util.*;
public class AreaOfSquare {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a, s; //declare the ‘boxes’ to hold side and area
System.out.printf("Enter length of side: ");
s = in.nextInt(); //read and store length in s
a = s * s; //calculate area; store in a
System.out.printf("\nArea of square is %d\n", a);
} //end main
} // end AreaOfSquare
Next, you must type the program to a file named AreaOfSquare.java. You must use the same name as the name used in the program. This is required by Java. Also, Java programs must be stored in files that end with .java.
Java is case-sensitive; this means that it makes a difference if you use an uppercase as opposed to a lowercase letter. For example, AreaOfSquare is different from areaOfSquare.
You must store your program as a text file. You can use Notepad or WordPad. You can use Word (or another word-processor) but be sure to save it as a text file.
For convenience, we assume that you have created a folder C:\MyJava and will store your programs there.
Summary: we assume the JDK has been installed in
C:\Program Files\java\jdk1.6.0_07
and your program is stored in a file AreaOfSquare.java in the folder C:\MyJava.
In the next article, How To Compile and Run Java Programs, we show how to compile and run the program.