Trying to learn Java on my own with the book "Starting out with java" The question is this
Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes:
circles
rectangles
cylinders
(it gives the formulas for the are of them)
Because the three methods are to be overloaded, they should each have the same name, but different parameter lists. Demonstrate the class in a complete program.

My answer is this:

public class Area
{

	static double Area (double rad)
	{
		return Math.PI* (rad * rad);
	}
	
	
	static int Area (int wid, int len)
	{
		return wid * len;
	}
	
	static double Area (double rad, double heig)
	{
		return Math.PI * (rad * rad) * heig;
	}
}
public class Main 
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		double radiusCircle = 3;
		int lengthRectangle = 10;
		int widthRectangle = 15;
		int heightCylinder = 20;
		double raidusCylinder = 4;
		
		System.out.println ("The area of a circle with radius " + radiusCircle +
				" is " + Area.Area(radiusCircle));
		System.out.println ("The area of a rectangle with a lenght of " + 
				lengthRectangle + " and a width of " + widthRectangle +
				" is " + Area.Area(widthRectangle, lengthRectangle));
		System.out.println ("The area of a cylinder with a radius " + raidusCylinder +
				" and a height of " + heightCylinder + " is " +
				Area.Area(raidusCylinder, heightCylinder));
		

	}

}

It works, but i'm not sure that it fits the way it should? How does it look?
Thanks

Recommended Answers

All 2 Replies

Looking only at the "Area" class, which is what the assigment text was concerning, it looks just fine (you probably want to make the methods public, however), and give all classes a package. Do not use the "default" package, that will only, eventually, cause heartache and pain.

method signature is fine ... but method is wrong..
you are calculation cylinder volume instead of area.
cylinder area will be 2*pi*r*r +2*pi*r*h

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.