我正在尝试编写一些代码来计算正则多边形的面积,使用此公式(边*长度^ 2)/ 4 * tan(Pi / sides)我的代码看起来像
double area = (nSides * Math.pow(sLength, 2)) /
4 * Math.tan ((Math.PI) / nSides);
我预计在nSides == 5和sLength == 6.5时,我应该得到72.69017017488385,但它输出38.370527260283126。
答案 0 :(得分:3)
它被称为Order of Operations。 Java文档说不清楚here:
下表中的运算符按优先顺序列出
问题在于除法和乘法具有相同的优先级。
同一行上的运算符具有相同的优先级。 ...除了赋值运算符之外的所有二元运算符都是从左到右计算的;
因此,假设它们具有相同的优先级,则在乘以Math.tan
之前将对除以4的除法进行求值。您需要通过插入一组额外的括号来明确指定顺序:
double area = (nSides * Math.pow(sLength, 2)) /
(4 * Math.tan ((Math.PI) / nSides));
答案 1 :(得分:0)
您需要为分母使用加法括号
(4 * Math.tan ((Math.PI) / nSides))
Else java认为你将整个表达式乘以Math.tan ((Math.PI) / nSides)
。
double area = (nSides * Math.pow(sLength, 2)) /
(4 * Math.tan ((Math.PI) / nSides));
答案 2 :(得分:0)
如果任何人需要使用OOP概念计算正多边形的面积和周长,这是我的试验。有人问我这样的学校作业,这就是我试图解决的问题。我是一名学生,希望成为一名程序员,并乐于学习和分享知识。
正多边形是具有3个或更多边的形状。每一边的长度必须相等。计算面积的公式根据边数而不同。
方形区域=边长*边长;
三角形=边的长度*边的长度* sqrt(3)/ 4
在C#中,我的代码似乎如下,但我尽可能地应用了oop的概念,并且我还实现了工厂设计模式。我也会用java编写代码。
这是IRegularPolygon接口,在这个问题中,我使用的是接口而不是抽象数据类型。
public interface IRegularPolygon
{
int NumberOfSides { get; set; }
double SideLength { get; set; }
double getPerimeter();
double getArea();
}
// you can also use abstract class the code is as follows
public abstract class RegularPolygon
{
public int NumberOfSides { get; set; }
public double SideLength { get; set; }
public double getPerimeter()
{
return NumberOfSides * SideLength;
}
public double getArea();
}
//The Square class that implement IRegularPolygon interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RegularPolygon.BL
{
public class Square : IRegularPolygon
{
public int NumberOfSides { get; set; }
public double SideLength { get; set; }
public Square(double SideLength)
{
this.NumberOfSides=4;
this.SideLength = SideLength;
}
/// <summary>
/// Get the Perimeter of the square
/// </summary>
/// <returns></returns>
public double getPerimeter()
{
return NumberOfSides * SideLength;
}
/// <summary>
/// Get the area of a square
/// </summary>
/// <returns></returns>
public double getArea()
{
return SideLength * SideLength;
}
public override string ToString()
{
return String.Format("{0}-{1}", "Area of Sqare:" + getArea(), "Perimeter Of Sqaure :" + getPerimeter());
}
}
}
//The EquilateralTriangle class that implement IRegularPolygon interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RegularPolygon.BL
{
public class EquilateralTriangle : IRegularPolygon
{
public int NumberOfSides { get; set; }
public double SideLength { get; set; }
public EquilateralTriangle(double SideLength)
{
this.NumberOfSides=3;
this.SideLength = SideLength;
}
/// <summary>
/// Get the Perimeter of EquilateralTriangle
/// </summary>
/// <returns></returns>
public double getPerimeter()
{
return NumberOfSides * SideLength;
}
/// <summary>
/// Get the area of EquilateralTriangle
/// </summary>
/// <returns></returns>
public double getArea()
{
return SideLength * SideLength * Math.Sqrt(3) / 4;
}
public override string ToString()
{
return String.Format("{0}-{1}", "Area of Sqare:" + getArea(), "Perimeter Of Sqaure :" + getPerimeter());
}
}
}
//The RegularPentagon class that implement IRegularPolygon interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RegularPolygon.BL
{
public class RegularPentagon : IRegularPolygon
{
public int NumberOfSides { get; set; }
public double SideLength { get; set; }
public double Apothem { get; set; }
public RegularPentagon(double SideLength, double Apothem)
{
this.NumberOfSides = 5;
this.SideLength = SideLength;
this.Apothem = Apothem;
}
/// <summary>
/// Get the Perimeter of RegularPentagon
/// </summary>
/// <returns></returns>
public double getPerimeter()
{
return NumberOfSides * SideLength;
}
/// <summary>
/// Get the area of RegularPentagon
/// </summary>
/// <returns></returns>
public double getArea()
{
return 2.5 * SideLength * Apothem;
}
public override string ToString()
{
return String.Format("{0}-{1}", "Area of Sqare:" + getArea(), "Perimeter Of Sqaure :" + getPerimeter());
}
}
}
// RegPolygonFactory class is a factory class that instantiate object based on shape
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RegularPolygon.BL
{
public class RegPolygonFactory
{
/// <summary>
/// Create instance of an object based on the type of shape
/// </summary>
/// <param name="shapeType"></param>
/// <param name="side"></param>
/// <returns></returns>
public IRegularPolygon getShape(string shapeType, double sideLength)
{
return getShape(shapeType, sideLength, 0.0);
}
public IRegularPolygon getShape(string shapeType, double sideLength, double apothem)
{
if (shapeType == null) throw new ArgumentNullException("shapeType");
if (sideLength < 0.0) throw new ArgumentOutOfRangeException(sideLength.ToString());
if (apothem < 0.0) throw new ArgumentOutOfRangeException(apothem.ToString());
else if (shapeType.Equals("SQUARE"))
{
return new Square(sideLength);
}
else if (shapeType.Equals("EQUILATERALTRIANGLE"))
{
return new EquilateralTriangle(sideLength);
}
//shapeType.equalsIgnoreCase("REGULARPENTAGON")
else if (shapeType.Equals("REGULARPENTAGON"))
{
return new RegularPentagon(sideLength, apothem);
}
return null;
}
}
}
//For unit Test: To test for instance square
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RegularPolygon.BL;
namespace RegularPolygonTest.BL
{
[TestClass]
public class SquareTest
{
RegPolygonFactory regularPolygon = new RegPolygonFactory();
/// <summary>
/// Test the Area Of Square with valid input(side=4)
/// </summary>
[TestMethod]
public void getAreaOfSquare_ValidArgument()
{
//Arrange
double sideLength = 4;
var square = regularPolygon.getShape("SQUARE", sideLength);
var expected = sideLength * sideLength;
//Act
var actual = square.getArea();
//Assert
Assert.AreEqual(expected, actual);
}
/// <summary>
/// Test the Perimeter Of Square with valid input(side=6, edges=4)
/// </summary>
[TestMethod]
public void getPerimeterOfSquare_ValidAregument()
{
//Arrange
double sideLength = 6;
double NumberOfSides = 4;
var square = regularPolygon.getShape("SQUARE", sideLength);
var expected = sideLength * NumberOfSides;
//Act
var actual = square.getPerimeter();
//Assert
Assert.AreEqual(expected, actual);
}
/// <summary>
/// Test the Perimeter Of Square with invalid input(edges=6)
/// </summary>
[TestMethod]
public void getPerimeterOfSquare_InValidPostiveAregument()
{
//Arrange
double sideLength = 5;
double NumberOfSides = 6;
var square = regularPolygon.getShape("SQUARE", sideLength);
var expected = sideLength * NumberOfSides;
//Act
var actual = square.getPerimeter();
//Assert
Assert.AreEqual(expected, actual);
}
}
}