Lab2 Answer

GravityCalculator.java
public class GravityCalculator {
    double g = 9.8;
    
    public double freeFallSpeed(double t) {
        return g*t;
    }
    
    public double freeFallDistance(double t) {
        return 0.5*g*t*t;
    }
}
RegularPolygon.java
public class RegularPolygon {
    public double side_length;
    public int sides;
    
    public RegularPolygon(double x) {
        side_length = x;
        sides = 4;
    }  
    public RegularPolygon(double x, int y) { //constructor overloading
        side_length = x;
        sides = y;
    }
    
    public double getPerimeter() {
        return side_length * sides;
    }
    public double getArea() {
        return (double)1/4 * sides * side_length * side_length / Math.tan(3.14/sides);
    }
}
public class Main
{
    public static void main(String[] args) {
        GravityCalculator cal = new GravityCalculator();
        double t1 = 5.0;
        double t2 = 8.5;
        double t3 = 15;
        System.out.println(cal.freeFallDistance(t1));
        System.out.println(cal.freeFallDistance(t2));
        System.out.println(cal.freeFallDistance(t3));
        
        RegularPolygon p1= new RegularPolygon(4.5, 4);
        RegularPolygon p2= new RegularPolygon(5.0, 6);
        RegularPolygon p3= new RegularPolygon(2, 3);
        System.out.println(p1.getArea());
        System.out.println(p1.getPerimeter());
        
        System.out.println(p2.getArea());
        System.out.println(p2.getPerimeter());	
        
        System.out.println(p3.getArea());
        System.out.println(p3.getPerimeter());
	}
}

Last updated