Area of a Pentagon - java

I was trying to write a code to take the radius of a pentagon and turn it into a side, then output the area. Somewhere along the way my use of the math class is incorrect. (New to this site but tried to format everything so you could understand my program)
Class: CS 1301/16
// Term: Fall
// Name: Gabriel Tomasetto
// Intsructor: Ms. Tulin
// Assignment 1
import java.util.*;
public class Pentagon {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double radius, side, area;
System.out.println("Please enter the length of the Pentagon from the center to the vertex: ");
radius = scan.nextDouble();
side = (2 * radius) * (Math.sin(Math.PI/5)); //I'm assuming I'm not correctly using the Math class to represent the equation? Thoughts.
area = (5 * Math.pow(radius, 2)) / (4 * Math.tan(Math.PI /5));
System.out.println("The area of the Pentagon is:\t" + area);
}
}

Shouldn't it be side = (2 * radius) * (Math.sin(Math.PI/2/5));?
What's the error you're getting? Just the wrong answer?
Just a simple tpyo I think, try this.
area = (5 * Math.pow(side, 2)) / (4 * Math.tan(Math.PI /5));
...assuming this is the right formula using side length and not radius
http://www.mathopenref.com/polygonregulararea.html

Related

Why does nothing display when I run my java code?

When I run my java code on one computer it works fine but on the other nothing displays.
When I type java CircleComputation it just returns to command line with nothing displaying but when I copy the file to another computer it works. Both have java installed and both have Path set. I can't find any difference in the computers.
Here is the code (not that it matters as I tried multiple programs):
/*
* Print the area and circumferencee of a circle, given its radius double.
*/
public class CircleComputation { // Saved as "CircleComputation.java"
public static void main(String[] args) {
// Declare variables
double radius, area, circumference;
final double PI = 3.14159265;
// Assign a value to radius and height
radius = 1.2;
// Compute area and circumference
area = radius * radius * PI;
circumference = 2.0 * radius * PI;
// Print results
System.out.print("The radius is "); // Print description
System.out.println(radius); // Print the value stored in the variable
System.out.print("The area is ");
System.out.println(area);
System.out.print("The circumference is ");
System.out.println(circumference);
}
}

How to get the circumference, area and radius of a circle in java?

I'm trying to print out the radius, circumference and area of a circle, and it is not working. I'm a complete beginner at java and coding so thank you for the help.
public class program54c {
public static void main (String args[]) {
double pi = 3.14159;
double radius = 15.337;
double circumference = (2*pi*radius);
double area = (pi*(radius*2));
System.out.println("The radius of the circle" = radius);
System.out.println("The circumference of the circle" = circumference);
System.out.println("The area of the circle" = area);
}
}
Your code seems correct except for one thing (assuming you know your mathematical calculations).
System.out.println("The radius of the circle" = radius);
You are not using the correct string concatenation for your output.
Replace = with + and try. For example,
System.out.println("The radius of the circle " + radius);
In your print statements, you need to use the + operator instead of = to append the variables value to your string. For example
"The area of the circle " = area
should be
"The area of the circle " + area
this will concatenate (append) the value of area to the string "The area of the circle "
Full example below:
public class program54c
{
public static void main (String args[])
{
double pi = 3.14159;
double radius = 15.337;
double circumference = (2*pi*radius);
double area = (pi*(radius*2));
System.out.println("The radius of the circle " + radius);
System.out.println("The circumference of the circle + circumference);
System.out.println("The area of the circle " + area);
}
}
I spot two errors in your code:
The string concatenation, as precised in the others answers, must be done with + and not =.
radius * 2 is not radiusĀ² ! You should use Math.pow(radius, 2) or radius*radius.
On a side note, you can use Math.PI instead of your own pi.

Law of Cosines used to find third side - why do I get the wrong result?

I keep getting an output of 45.8 when inputting 24 as side a, 32 as side b and 115 as angle C
the correct angle is 47.4
can someone please tell me what I'm doing wrong?
The calculation is:
double sideC = Math.sqrt((Math.pow(sideA, 2) + Math.pow(sideB, 2))- 2*(sideA*sideB)*(Math.cos(angleC)));
Source:
import java.util.Scanner;
public class TriangleCalc
{
/**
* #param args
*/
public static void main(String[] args)
{
System.out.println(" Triangle Calculator ");
Scanner inputab = new Scanner(System.in);
System.out.println("Input lenghts of sides 'a' and 'b':");
double sideA = inputab.nextDouble();
double sideB = inputab.nextDouble();
System.out.println("Input the size of Angle C in degrees:");
double angleC = inputab.nextDouble();
inputab.close();
double sideC = Math.sqrt(((Math.pow(sideA, 2) +
Math.pow(sideB, 2))- (2*(sideA*sideB)*(Math.cos(angleC)))));
System.out.println("\t /\\\n\t / \\\n\t / \\\n\t/ \\");
System.out.printf(" %3.1f",sideA);
System.out.print("/ \\");
System.out.printf("%3.1f",sideB);
System.out.println("\n / \\\n / \\\n"
+" /______________\\");
System.out.print(sideC);
As the Javadoc for Math.cos states
a - an angle, in radians.
You need to convert degrees to radians if you want to use degrees.
You can use Math.toRadians
Replace
Math.cos(angleC)
with
Math.cos(Math.toRadians(angleC))

JAVA Formula for the calculation of the area of a pentagon

I hope you can help me out with this. I am trying to find the area of a pentagon. I know the formula however my answer does not match the correct answer.
I should be able to get the area by utilizing the math.sqrt but I am having a rough time simplifying the formula and combining it in such a way that the output is correct.
Here is my code
System.out.println("What shape do you need to know the area of?\n" +
"1: Determine the Area of a Square?\n" +
"2: Determine the Area of a Circle?\n" +
"3: Determine the Area of a Ellipse?\n" +
"4: Determine the Area of a Pentagon? \n" +
"5: Exit\n"
);
Scanner reader = new Scanner(System.in);
System.out.println("Selection: ");
int input = reader.nextInt();
reader.nextLine();
Scanner scan = new Scanner(System.in);
}
if (input == 4){
System.out.println("What is a length of 1 side of the Pentagon? ");
int n = scan.nextInt();
// Calculate the area based on formula
double area = (n * Math.pow(n, 2)) / (4 * Math.tan(Math.PI / n));
//Print result
System.out.println (" Area of regular pentagon is " + area);
}
double area = (n * Math.pow(n, 2)) / (4 * Math.tan(Math.PI / n));
The above is not the correct formula (n=length of side) for area of a regular pentagon. The correct formula is
double area = (5 * n * n)) / (4.0 * Math.tan(Math.PI / 5));
It's five time the size of one of the triangles comprising the pentagon.
If you have trouble figuring out the area of that triangle, remember it's 1/2 the area of the parallelogram comprised of two of those triangles stuck together (with equal angles across the diagonal joining the triangles)
If you have trouble figuring out the area of a parallelogram, it's the length of one of the parallel sides times the perpendicular distance between the parallel sides.
Wikipedia gives a formula for the area of a pentagram as 1.72 * n^2
You can use area=1.720477*n*n for a little more precision
Area for regular pentagons is =~ 1.7204774*s^2, being s the side of the pentagon, thus you should replace your calculation with:
// Calculate the area based on formula
double area = (n * n * 1.7204774);

how do I get the output answer 4 decimal places

I need to round the outputs to 4 decimal places but I am not quite sure on how to do that my thing runs exactly how I want it too just need to round off the last decimals off too 4 places
import java.util.Scanner; //Needed for the Scanner class
public class SphereCalculations
{
public static void main(String[] args) //all the action happens here!
{ Scanner input = new Scanner (System.in);
double radius;
double volume;
double surfaceArea;
System.out.println("Welcome to the Sphere Calculator. ");
System.out.print( "Enter radius of sphere: " );
radius = input.nextDouble();
volume = ((4.0 / 3.0) * (Math.PI * Math.pow(radius, 3)));
surfaceArea = 4 * (Math.PI * Math.pow(radius, 2));
System.out.println("The Results are: ");
System.out.println("Radius: " + radius);
System.out.println("Sphere volume is: " + volume);
System.out.println("Sphere Surface Area is: " + surfaceArea);
}
}
Output:
Radius: 7.5
Volume: 1767.1459
Surface Area: 706.8583
System.out.printf("Sphere Surface Area is: %.4f%n", surfaceArea);
If you find that you need more control, you can use DecimalFormat which has many more formatting options. This is especially helpful if you want to print out a double in scientific notation.
//specify a number locale, some countries use other symbols for the decimal mark
NumberFormat f = NumberFormat.getInstance(Locale.ENGLISH);
if(f instanceof DecimalFormat) {
DecimalFormat d = (DecimalFormat) f;
d.applyPattern("#.0000"); //zeros are non optional, use # instead if you don't want that
System.out.println(d.format(surfaceArea));
}

Categories

Resources