Page 64 - Trackpad_ipro 4.1_Class8
P. 64

WRITING SOME MORE PROGRAMS

                  Let us write programs using Java's various concepts.

                  Program 1: Program to calculate area and perimeter of a rectangle.

                  public class Calculate {

                         public static void main(String args[]) {
                                int length, width, area, perimeter;

                                length = 5;

                                width = 10;

                                // Area of rectangle = length * width
                                area = length * width;

                                // Perimeter of rectangle = 2 * (length + width)

                                perimeter = 2 * (length + width);

                                System.out.println("Area of the rectangle is: " + area);
                                  System.out.println("Perimeter  of the rectangle  is: " +

                                perimeter);
                         }

                  }

                  Program 2: Program to find the amount to be paid.
                  public class Amount {

                         public static void main(String args[]) {

                                int unitPrice, units, amount;

                                unitPrice = 20;
                                units = 100;

                                amount = unitPrice * units;

                                System.out.println("Total amount to be paid: " + amount);

                         }
                  }

                      TAKING VALUES AS ARGUMENTS


                  Until now, we have initialised the variables before using them. Java also allows us to take values
                  of variables from the user at runtime. We can do this by passing arguments into the main()
                  method.





                    62     iPro (Ver. 4.1)-VIII
   59   60   61   62   63   64   65   66   67   68   69