Question #33518

Write a class Square whose constructor receives the height of the sides. Then supply methods to compute
-the area and parameter of the square
-the length of the diagonal (use the Pythagorean theorem)

Expert's answer

import java.util.*;

import java.lang.*;

class Square {

double height;

public Square(double Hscan) {

height = Hscan;}

public double area(double H) {return H*H;}

public double perimeter(double H) {return 4*H;}

public double diagonal(double H) {return H*Math.sqrt(2);}

public static void main (String[] args) {

System.out.println("Enter the height of a square:");

System.out.print("the height = ");

Scanner scan = new Scanner(System.in);

String s = scan.nextLine();

double h = Double.parseDouble(s);

Square sq = new Square(h);

System.out.println("An area of the square = "+sq.area(h)+" with the height = "+h);

System.out.println("A perimeter of the square = "+sq.perimeter(h)+" with the height = "+h);

System.out.println("A lenght of the diagonal of the square = "+sq.diagonal(h)+" with the height = "+h);

}

}

import java.lang.*;

class Square {

double height;

public Square(double Hscan) {

height = Hscan;}

public double area(double H) {return H*H;}

public double perimeter(double H) {return 4*H;}

public double diagonal(double H) {return H*Math.sqrt(2);}

public static void main (String[] args) {

System.out.println("Enter the height of a square:");

System.out.print("the height = ");

Scanner scan = new Scanner(System.in);

String s = scan.nextLine();

double h = Double.parseDouble(s);

Square sq = new Square(h);

System.out.println("An area of the square = "+sq.area(h)+" with the height = "+h);

System.out.println("A perimeter of the square = "+sq.perimeter(h)+" with the height = "+h);

System.out.println("A lenght of the diagonal of the square = "+sq.diagonal(h)+" with the height = "+h);

}

}

## Comments

## Leave a comment