Page 65 - iPro_trackGPT_V5_Class8
P. 65
TAKING VALUES AS ARGUMENTS
Until now, we have been initialising variables before using them. However, Java also allows us
to obtain variable values from the user at runtime. You can do this by passing arguments into
the main() method. We can accept a single value or multiple values by separating them with
commas, as shown below:
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 run the above program in BlueJ, a
Method Call dialog box will appear, prompting
you to enter the necessary arguments.
Method Call dialog box
After entering the value of the arguments, click
on the OK button. The output will appear in the
terminal window.
Output
Program 4: Write a program in Java to print the area and perimeter of the square.
public class SquareProperties {
public static void main(float side) {
float Area=side*side;
float Perimeter=4 * side;
System.out.println("Area of the square: " + Area);
System.out.println("Perimeter of the square: " + Perimeter);
}
}
Output
Program Coding 63

