In: Computer Science
In java. Prefer Bluej
Create a program in java that calculates area and perimeter of a square - use a class and test program to calculate the area and perimeter; assume length of square is 7 ft.
//save this in Square.java
class Square
{
int side;
Square(int s){
side=s;
}
//method to return area of Square
int area(){
return side*side;
}
int perimeter(){
return 4*side;
}
}
//Save it in Main.java
//Main class to create object of Square class and call area and perimeter methods
public class Main{
public static void main(String[] args){
Square obj=new Square(7);//create a square with side=7
System.out.println("Area of square : "+obj.area());
System.out.println("Perimeter of square : "+obj.perimeter());
}
}