Program that we want to write

 

  • Write a Java program that:

      • Define variables to store the radius and area of a circle

      • Computes the area of the circle

      • Print out the area that was computed

  • New things that you will learn:

      • Fundamental data types in Java

      • How to define and using double typed variables

Steps to write a Java program

(1) Sepecify what the Java program must do:



 
   
    
   
  
      // (1) Assign a radius


      // (2) Compute area
     

      // (3) Print result
      
                          
                       


  

 

Define a class --- pick an appropriate class name for the class

(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

Define the main( ) method

(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:

Define variables used to store the necessary information

(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
  
                         
   }                       
}

  

 

Initialize the radius of the circle

(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
  
                         
   }                       
}

  

 

Compute the area of the circle

(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
  
                         
   }                       
}

  

 

Print the result stored inside variable area

(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

What next ?

 

  • The radius is currently being hard-coded into the program

  • Next we learn how to read in the radius from the keyboard

  • Note:

    • The variable System.in in Java is associated with the keyboard