Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Write a program to calculate the area of two geometric shapes: triangles and circles. You must use functions. Here are the functions you should create:
public static double area_triangle( int base, int height ) // returns the area of a triangle
public static double area_circle( int radius ) // returns the area of a circle

Your program should present a menu (triangle or circle) for the user to choose which shape to calculate, then ask them for the appropriate values (length, width, radius, etc.). Then it should pass those values to the appropriate function and display the resulting area.
Notice that you must not input the values inside the functions, and you must not display the values inside the functions. All input and output must be in the main(), and values must be passed to the functions and returned from them.
You'll need the value of π for area_circle(); feel free to use the built-in double variable called Math.PI.

What I have tried:

i tried this and got stuck

Java
import java.util.Scanner;

public class Area {
	

	public static void main(String[] args) {
		public static double area_triangle( int base, int height )  {
			
			System.out.print("Menu");

			
			
			
			
			Scanner input = new Scanner (System.in); 
			base=input.nextInt(); 
			height=input.nextInt(); 
			area_triangle=height*base/2; 
			
			
			
		}
			
	 
	}

}
Posted
Updated 11-Feb-18 8:06am
v2
Comments
Maciej Los 11-Feb-18 13:34pm    
This is your HomeWork!
Member 13672899 11-Feb-18 13:36pm    
yes it is my homework due tomorrow i got stuck doing it :( would u help me if u can ?
CPallini 11-Feb-18 15:54pm    
You have just to perform a similar computation for the circle area and let the user to choose between the two shapes. What's exactly the problem?

1 solution

Well, it seems that you're defining the "area_triangle" method inside the "main" method. You'll have to define it outside the main method and then call it inside the main method. Try something like:
Java
<pre>import java.util.Scanner;

public class Area {
	

	public static void main(String[] args) {
		Area a=new Area();
                System.out.print("Menu");
	        Scanner input = new Scanner (System.in); 
		double base=input.nextInt(); 
		double height=input.nextInt();
                a.area_triangle(base,height);
                			
		}
          public static double area_triangle( int base, int height )  {
			
			 
			double area_triangle_1=height*(base/2);
                        System.out.println(area_triangle_1);
                        return area_triangle_1; 
			
	 
	}

}

Hope this helps.
Cheers.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900