Answer to Question #167689 in Java | JSP | JSF for Hanbun

Question #167689

Write a program that finds the area of circle, triangle, rectangle and square using a function named area. In this program, use a single class named shape which contains various implementations of ‘area’. Your program should dynamically decide which shapes area has to be found depending on the type of input. The radius is float value, base and height of triangle are double values, sides of rectangle and square are integer values.


1
Expert's answer
2021-03-01T02:04:35-0500
class Shape {
	static double area(float radius) {
		return Math.PI * radius * radius;
	}
	
	static double area(double base, double height) {
		return base * height / 2;
	}
	
	static int area(int sideA, int sideB) {
		return sideA * sideB;
	}
	
	static int area(int side) {
		return side * side;
	}
}


public class Main {
	public static void main (String[] args){
		System.out.println(Shape.area(3.0f));
		System.out.println(Shape.area(3.0, 4.0));
		System.out.println(Shape.area(3, 4));
		System.out.println(Shape.area(3));
	}
}

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
New on Blog