Methods Answers



Excercises

1.
public double rectangleArea(double base, double height) {
    return base * height;
}

public double boxVolume(double base, double height, double depth) {
    return rectangleArea(base,height) * depth;
}

public double boxSurfaceArea(double base, double height, double depth) {
    double side1 = rectangleArea(base,height);
    double side2 = rectangleArea(height,depth);
    double side3 = rectangleArea(base,depth);
    return 2 * (side1 + side2 + side3);
}
2.
public double circleCircumference(double radius) {
    return 2 * Math.PI * radius;
}

public double circleArea(double radius) {
    return Math.PI * Math.pow(radius,2);
}

public double cylinderVolume(double radius,double height ) {
    return circleArea(radius) * height;
}

public double cylinderSurfaceArea(double radius, double height) {
    double bottom = circleArea(radius);
    double side = circleCircumference(radius) * height;
    double top = circleArea(radius);
    return bottom + side + top;
}
3.
public double quadraticRoot1(double a,double b, double c) {
    double rootPart = Math.sqrt(Math.pow(b,2) - 4 * a * c);
    return (-1.0 * b + root) / (2 * a);
}

public double quadraticRoot2(double a,double b, double c) {
    double rootPart = Math.sqrt(Math.pow(b,2) - 4 * a * c);
    return (-1.0 * b - root) / (2 * a);
}

4.
public double seconds2minutes(int seconds) {
    return (double)seconds / 60.0;
}

public double minutes2hours(double minutes) {
    return minutes / 60.0;
}

public double hours2days(double hours) {
    return hours / 24.0;
}

public double days2years(double days) {
    return days / 365.25;
}

public double seconds2years(int seconds) {
    return days2years(hours2days(minutes2hours(seconds2minutes(seconds))));
}

5.
public int randomInteger(int lowerBound, int upperBound) {
    return (int)(Math.random() * (upperBound - lowerBound + 1)) + lowerBound;
}
6.

public void reflectAcrossX(Point p) {
    p.setX(-1 * p.getX());
}

public void reflectAcrossY(Point p) {
    p.setY(-1 * p.getY());
}

public void reflectAcrossOrigin(Point p) {
    p.reflectAcrossX();
    p.reflectAcrossY();
}

public void translateX(Point p, int dx) {
    p.setX(p.getX() + dx);
}

public void translateY(Point p, int dy) {
    p.setY(p.getY() + dy);
}

public void translate(Point p, int dx, int dy) {
    p.translateX(x);
    p.translateY(y);
}

public int dotProduct(Point p1, Point p2) {
    return p1.getX()*p2.getX() + p1.getY()*p2.getY();
}

public double magnitude(Point p) {
    return Math.sqrt( Math.pow(p.getX(),2) , Math.pow(p.getY(),2) );
}

public double angle(Point p) {
    return Math.atan((double)p.getY() / (double)p.getX());
}

public  Point copy(Point p) {
    return new Point(p.getX(),p.getY());
}

public  Point double(Point p) {
    return copy(2 * p.getX(), 2 * p.getY());
}

public  Point invert(Point p) {
    return copy(p.getY(),p.getX());
}
7.
public double acceleration(double v, double r) {
    return Math.pow(v,2) / r;
}

public double force(double m, double v, double r) {
    return m * acceleration(v,r);
}

public double energy(double k, double x) {
    return (1.0/2.0)*k*math.pow(x,2);
}

public double field(double q, double r) {
    double k = 9e9;
    return k * q / Math.pow(r,2);
}

public double wireField(double i, double r) {
    double mu0 = 8.85e-12;
    return (mu0 * i) / (2 Math.PI * r);
}