|
(1) Sepecify what the Java program must do:
// (1) Assign a radius
// (2) Compute area
// (3) Print result
|
(2) A Java program contains a class:
public class ComputeArea
{
// (1) Assign a radius
// (2) Compute area
// (3) Print result
}
|
DEMO: demo/02-elem-prog/01-ComputeArea/ComputeArea.java
(3) A Java program starts execution in the main( ) method:
public class ComputeArea
{
public static void main(String[] args)
{
// (1) Assign a radius
// (2) Compute area
// (3) Print result
}
}
|
Copy and paste in BleuJ:
(4) Define variables to store the radius and the area of the circle:
public class ComputeArea
{
public static void main(String[] args)
{
double radius; // double is the data type
double area;
// (1) Assign a radius
// (2) Compute area
// (3) Print result
}
}
|
(4a) The assignment statement will store the value at the RHS of = to the variable on the LHS:
public class ComputeArea
{
public static void main(String[] args)
{
double radius; // double is the data type
double area;
// (1) Assign a radius
radius = 20; // Assignment statement
// (2) Compute area
// (3) Print result
}
}
|
(4b) The assignment statement will store the value at the RHS of = to the variable on the LHS:
public class ComputeArea
{
public static void main(String[] args)
{
double radius; // double is the data type
double area;
// (1) Assign a radius
radius = 20; // Assignment statement
// (2) Compute area
area = 3.14159 * radius * radius; // Assignment statement
// (3) Print result
}
}
|
(5) The System.out.print( ) method will print its argument out to the terminal "System.out":
public class ComputeArea
{
public static void main(String[] args)
{
double radius; // Data type
double area;
// (1) Assign a radius
radius = 20; // Assignment statement
// (2) Compute area
area = 3.14159 * radius * radius; // Assignment statement
// (3) Print result
System.out.println(area); // Print area to System.out (= terminal)
}
}
|
In Java, the variable System.out represents the termimal
|