Answer to Question #33518 in Java | JSP | JSF for Jimbo

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)
1
Expert's answer
2013-07-31T08:47:25-0400
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);
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS