Lab2

In this assignment, you will create two classes: GravityCalculator and RegularPolygon :

Part I: GravityCalculator

Step1:

  • 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):

GravityCalculator.java
public class GravityCalculator {
    double g = 9.8;
}

g is the value of acceleration of gravity, which you will use later:

Step2:

  • Add a method freeFallSpeed() in the class. The method has been provided for you, so you can directly copy and paste it:

public double freeFallSpeed(double t) {
    return g*t;
}
  • The method returns the speed of an object after free falling for t seconds. The formula in math notation is: v=gtv = g * t, 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.

Step3:

Write a method: doublefreeFallDistance(double t), which returns the distance s(in meters)an object is free falling after t seconds. The formula is: s=0.5gt2s = 0.5 * g * t^2. So freeFallDistance(1.5) will return 11.025.

Please use at least 3 datasets to test your method.

Part II: Polygon

Step1

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 Lab2with following code. You can directly copy paste it.

RegularPolygon.java
public class RegularPolygon {
    public double side_length;
    public int sides;
    public double pi = 3.14;
    
    public RegularPolygon(int x) {
        side_length = x;
        sides = 4;
    }
    
    public double getArea() {
        return side_length*side_length;
    }

Step2

In main(), called getArea() by using the following code:

RegularPolygon poly = new RegularPolygon(5);
System.out.println(poly.getArea());

It should return 25.0, since it computes the area of a square(正方形) with side length equals to 5.

Step3

  • 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:

RegularPolygon poly = new RegularPolygon(1.5,3);

Step4

Write a method called getPerimeter() to return the perimeter of the polygon. Here is 2 examples of the expected output of the method:

RegularPolygon poly = new RegularPolygon(5,6);
System.out.println(poly.getPerimeter());    //should print 30.0
RegularPolygon poly = new RegularPolygon(4.5, 3);
System.out.println(poly.getPerimeter());    //should print 13.5

Step5

Modify the method getArea() so that it is able to compute the area for all polygons. The math formula is:

Area=¼×n×l2/tan(π/n)Area = ¼ × n × l^2 / tan(π/n)

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

Submit

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.

Last updated