Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. Next, use the Strategy design pattern (shown below) to replace the field g wi

ID: 3912214 • Letter: 3

Question

3. Next, use the Strategy design pattern (shown below) to replace the field g with a reference to a strategy object (as outlined below). This will allow different kinds of gravity models to be associated with a Pendulum instance dynamically. Context interface Strategy +execute0 ConcreteStrategyA +exeaute0 ConcreteS trat egyB +exeaute Name the strategy interface GravityModel, with a single method public double getGravitationalField O; Assume that objects implementing GravityModel will be initialized with all state required to produce the gravitational field value under their specific model, and that this occurs before being assigned to a pendulum. In other words, the pendulums will just use the model assigned to it via the GravityModel interface. Adjust the pendulum constructors so that a GravityModel is assigned on initialization, and add a new method that will allow the GravityMode1 to be changed dynamically Define one concrete strategy called GravityConstant. This class simply receives a constant value for g in its constructor.

Explanation / Answer

package dal.gravity;

import java.text.NumberFormat;

/**

* compares the values of a simple pendulum using the harmonic motion equation

* versus the Euler algorithm approximation

*/

public class PendulumRunner {

public static void main (String [] args) {

NumberFormat nf = NumberFormat.getInstance ();

nf.setMaximumFractionDigits (3);

double delta = (args.length == 0) ? .1 : Double.parseDouble (args[0]);

double sLen = 10, pMass = 10, theta0 = Math.PI/30;

RegularPendulum rp = new RegularPendulum (sLen, pMass, theta0, delta);

SimplePendulum sp = new SimplePendulum (sLen, pMass, theta0);

RegularPendulum rpCoarse =

new RegularPendulum (sLen, pMass, theta0, .1);

// print out difference in displacement in 1 second intervals

// for 20 seconds

int iterations = (int) (1/delta);

System.out.println ("analytical vs. numerical displacement (fine, coarse)");

for (int second = 1; second <= 20; second++) {

for (int i = 0; i < iterations; i++) rp.step ();

for (int i = 0; i < 10; i++) rpCoarse.step ();

System.out.println ("t=" + second + "s: " +

nf.format (Math.toDegrees (sp.getTheta (second)))

+ " " +

nf.format (Math.toDegrees (rp.getLastTheta ()))

+ " " +

nf.format (Math.toDegrees (rpCoarse.getLastTheta ())));

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote