Page 64 - iPlus_Ver_2.0_class_8
P. 64

// 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: Write a 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;
                                                                                                  Output
                                amount = unitPrice * units;

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

                  }

                   i +  TAKING VALUES AS ARGUMENTS

                  Up till 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. We can accept a single value as well as multiple values by separating them with a
                  comma in the following way:

                  public class Arguments
                  {
                      public static void main(int roll_no, String name)

                      {
                          System.out.println("Roll number is: " + roll_no);
                          System.out.println("Name is: " + name);
                      }

                  }
                  When we execute the preceding program in BlueJ,
                  the Method Call dialog box appears, asking us to
                  enter the required arguments as shown:








                                                                                        Method Call dialog box



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