Lab2
Last updated
Last updated
In this assignment, you will create two classes: GravityCalculator
and RegularPolygon
:
Open OnlineGDB. Create a project called Lab2.
Inside Lab2, create a new class called GravityCalculator
with the following code (you can directly copy and paste it):
g is the value of acceleration of gravity, which you will use later:
Add a method freeFallSpeed() in the class. The method has been provided for you, so you can directly copy and paste it:
The method returns the speed of an object after free falling for t seconds. The formula in math notation is: , where g is the value of acceleration of gravity. Methods can use instance variables. So freeFallSpeed() can use g, which we defined in the class.
In main() method, call freeFallSpeed() and see the results. For example, freeFallSpeed(2) should return 19.6.
Please use at least 3 datasets to test your method. All your work should be saved.
Please use at least 3 datasets to test your method.
A regular polygon(正多边形) is an n-sided polygon in which the sides are all the same length and are symmetrically placed about a common center. Create a class called RegularPolygon
inside Lab2
with following code. You can directly copy paste it.
In main()
, called getArea()
by using the following code:
It should return 25.0, since it computes the area of a square(正方形) with side length equals to 5.
Look at the constructor. The instance variable sides is set to 4, which means we can only create squares. However we also want to create different kinds of polygons.
Modify the constructor public RegularPolygon()
, so that it takes two parameters to assign both side_length and sides. For example, we want to create a regular triangle with side length equals to 1.5, by calling constructor like this:
Step4
Write a method called getPerimeter()
to return the perimeter of the polygon. Here is 2 examples of the expected output of the method:
Modify the method getArea()
so that it is able to compute the area for all polygons. The math formula is:
where n is the number of sides, and l is the length of each side. You can use Math.tan(x)
to compute the tangent of x.
Please use at least three test data to verify your method. You can use the following link to get the area of any polygon:
https://zh.numberempire.com/regular_polygon_calculator.php
Please submit your project link in 钉钉作业本。Your project should contain at least 3 java files: Main.java, GravityCalculator.java, and RegularPolygon.java. In Main.java, all the tests should be included.
Write a method: doublefreeFallDistance(double t)
, which returns the distance s(in meters)an object is free falling after t seconds. The formula is: . So freeFallDistance(1.5) will return 11.025.