Page 67 - TP_iPlus_V2.1_Class8
P. 67

// 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;
                               amount = unitPrice * units;                                       Output
                               System.out.println("Total amount to be paid: " + amount);
                        }
                 }

                  i +  TAKING VALUES AS ARGUMENTS

                 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







                                                                                                                  65
                                                                                               Program Coding
   62   63   64   65   66   67   68   69   70   71   72