Write a program to calculate and display the area of a Square
Answers
Answer:
1) Using Static Method
import java.util.Scanner;
class calculateArea
{
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter length of side.");
double a = sc.nextDouble();
double Area = Math.pow(a,2);
System.out.println("Area: " + Area);
}
}
Refer to the attachment for an example on 6.
2) Using Method
import java.util.Scanner;
class AreaOfSquare
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter length of side of the square:");
double a= sc.nextDouble();
double cal=area(a);
System.out.println("Area of Square is: " + cal);
}
static double area(double r)
{
return r*r;
}
}
Refer to the above attachment for an example on 4.
Static Method:
import java.util.Scanner;
class hey
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter side.");
int side = sc.nextInt();
int area = side*side;
System.out.println("Area of Sqaure: " + area);
}
}