Lab 2

For a given formula we can create a class of calculators for the formula. Instance variables can store the formula's parameters.

For example, the distance a particle travels after t seconds is given by the formula:

s = p + vt + at2

Where p = the initial position, v = the initial velocity, and a = the initial acceleration.

Here's a calculator: DistanceCalculator.java

Create calculator classes for the following formulas. Each class should include a main method that creates a couple of calculator objects, sets their instance variables, and displays the formula's output.

The Future Value of an Investment

FV = p(1 + r)n

p = principle
r = interest rate
n = number of months or years

 

Gravitational Attraction

F = Gm1m2/r2

Where:

m1 = mass 1
m2 = mass 2
r = distance between these masses
G = 6.67x10-11

Trigonometric Identities:

sin (s + t) = sin s cos t + cos s sin t

cos (s + t) = cos s cos t – sin s sin t

The Quadratic Formula

root1 = (-b + sqrt(b2 – 4ac))/2a

root2 = (-b - sqrt(b2 – 4ac))/2a