In Java, create a Square class that can be queried for its perimeter and area. Create a second class , with a main(), to test the Square class. Write code in full detail.
The text inside // // symbol are the comments *Leave this line*
import java.util.Scanner; //Scanner package used to take user
input//
class Square
{
double a; //varible declaration for side of
square//
double are; //variable using to find area//
double peri; //variabe using to find perimeter//
void method() //method to find area and
perimeter//
{
System.out.println("The formula for
the area of the square is : a^2");
System.out.println("The formula for
the perimeter of the square is : 4a");
System.out.println("Enter side of
the square: ");
Scanner sc=new
Scanner(System.in); //Scanner class for user input//
a=sc.nextDouble();
are=a*a; //formula for
area of square//
peri=4*a; //formula for perimeter
of square//
System.out.println("Area of the
Square: "+are+"cm/m/km"); //print statement for area///
System.out.println("Perimeter of
the Square: "+peri+"cm/m/km"); //print statement for
perimeter//
}
}
class Second
{
public static void main(String[] args) { //main
method//
Square s=new Square(); //object of
the Square class///
s.method(); //calling method in
Square class with object name//
}
}
If you need any extra info, kindly comment me..
Get Answers For Free
Most questions answered within 1 hours.