CSX, also know as Advanced Math and Physics Modeling, is three classes taught
in a unified manner. The classes are Precalculus 2A, Physics A, and Computer
Science X. In teaching Math and Physics in a unified way, we recognize the
historical development of the two subjects. Math and Physics fit together like a hand in a glove. It’s most instructive, and most enjoyable, if the two
subjects are presented in tandem. In teaching Math, Physics, and Computer
Science in a unified way, we recognize the way in which contemporary
scientists work. Whereas once all scientists were either experimentalists or
theorists – and most were experimentalists – scientists are now increasingly
engaged in the creation of computer models. This has become a third way in
which to understand the physical world. Computer models are based upon an
understanding of the relevant Math and Science. However, through the use of a
computer, relatively simple Math and Science can be used to model extremely
complicated situations. Consequently, computer models are ubiquitous in areas
as disparate as Global Warming and the Human Genome.
Almost all Computer Science projects will be based on Physics or Mathematics.
Some programs will be computer models of physical situations. For example,
you’ll model balls dropping, planets orbiting the Sun, and masses connected to a spring. Some programs will be realizations of mathematical algorithms. For instance, early on in the year you’ll write code to invert a matrix.
When possible, the same topics will be covered at the same time in both Math
and Physics. Many of the concepts covered in Precalculus and Physics were
developed simultaneously. Math is necessary for a deep understanding of
Physics, and Physics provides beautiful applications of what would otherwise
be abstract Mathematics.
We certainly hope you enjoy Math, Physics and Computer Science this year. In
particular, we hope you find it helpful to approach the three subjects as, in
some sense, one. Throughout the class, you should try to think about the ways
in which Math is used in Physics, the ways in which Physics allows for a
deeper understanding of Mathematics, and the ways in which computers could be
used to gain a deeper understanding of either Math or Physics. In addition,
bear in mind that Math and Physics provide a limitless source of programming
challenges. Consequently, Math and Physics provide an excellent context in
which to teach programming concepts.
Step 3. Expand the src and test folders. These folders have
example classes and are where you will put all your code for this course.
Your project should look like the above.¶
In the test folder of XClassProjects, create a new Java class called QuadraticTest. At the top of the class, import all the necessary JUnit libraries, like this:
In order to get your test to pass, continue with the following exercise:
Exercise
In the src folder, create a new Java class called Quadratic. This will be the object class that defines a quadratic function and methods that analyze its different characteristics.
Create 3 private attributes a, b and c, all doubles.
Write a constructor which takes a, b and c as params and uses them to set the a, b and c attributes.
Write a public method called getA which returns the value of a.
Run QuadraticTest again. It should pass this time.
Once your QuadraticTest passes, continue with the following exercise:
Exercise
Note: For this exercise, you will switch back and forth between the Quadratic and QuadraticTest classes.
Write the following methods in Quadratic. For each method, write at least one test method in QuadraticTest.
publicdoublegetB()
Return the value of b.
In QuadraticTest and add test method for getB.
publicdoublegetC()
Return the value of c.
In QuadraticTest and add test method for getC.
publicbooleanhasRealRoots()
See if the quadratic has real roots, return boolean true or false.
Write a test method in QuadraticTest called hasRealRoots to test it .
publicintnumberOfRoots()
Determine the number of real roots, return an int (0, 1, or 2).
Write a test method in QuadraticTest called numberOfRoots to test it .
publicdouble[]getRootArray()
Find the roots, return 2 doubles (put them in an array).
Write a test method in QuadraticTest called getRootArray to test it .
publicdoublegetAxisOfSymmetry()
Find the axis of symmetry. Return that value.
Write a test method in QuadraticTest called getAxisOfSymmetry to test it .
publicdoublegetExtremeValue()
Find the extreme value, the maximum or minimum function value corresponding to the y coordinate of the vertex of the parabola. Return that value.
Write a test method in QuadraticTest called getExtremeValue to test it .
publicbooleanisMax()
Is the extreme value a Max or a Min? Does the parabola open up or down? Return true for Max and false for Min.
Write a test method in QuadraticTest called isMax to test it .
publicdoubleevaluateWith(doublex)
Evaluate the quadratic function at an x value, return f(that x value).
Write a test method in QuadraticTest called evaluateWith to test it .
When all the tests in QuadraticTest pass, you are finished with this exercise.
Open the GettingStarted class and look at the Polynomial examples. (Ignore the rest of the code in the GettingStarted class for now.) Run the code and look at the output in the console. (Again, ignore the pop-up that appears for now.)
After looking over the code and the output, complete the following exercise.
Exercise
Do the following in the GettingStarted class.
Create a Polynomial called gx and set it equal to (4.0)X^4+5.0. Print the Polynomial.
Evaluate vx for when x is 2 and print the answer.
Add fx and vx and print the answer.
For vx, print the coefficient for the X^1 term (also known as simply X).
Use a for-loop or for-each-loop to print all the coefficients of vx
When you get the output below you are finished with this exercise:
Run the GettingStarted class again but this time look at the graph that appears. Hover over the graph for it to render. Look at the code under Open Source Physics (OSP) Example to see how this graph was made.
When you think you understand, do the following exercise:
Exercise
Do the following in the GettingStarted class.
Look at the code that makes the red line. Comment out the lines that append points to the red line. Then use a for-loop to make the red line a graph of fx for when x is values 0 - 10.
Look at the code that makes the green line. Comment out the lines that append points to the green line. Then use a for-loop to make the green line a graph vx for when x is values 0 - 10.
Look at the code that makes the orange trail. Notice how this is different from the previous lines. Comment out the code that adds points to the trail. Then use a for-loop to make the orange line a graph of gx (which you created in the Introduction to Polyfun exercise) for when x is values 0 - 10.
When you get the output below you are finished with this exercise:
Which do you prefer, appending x and y plot points, or creating a Trail?
This is an example of an animation. It is different from a static graph, which you did in Step 1, in that you have to write at least three methods (reset, intialize, and doStep) in order for it to work.
MovingBallApp is an extension of AbstractSimulation, an abstract class.
An abstract class is a class that is meant to be extended and one where the author left some methods empty, which are meant to be overridden. These empty methods are called abstract methods.
In AbstractSimulation, these are the abstract methods that MovingBallApp needs to override:
reset - Adds options to the Control Panel and returns the simulation to its default state. All commands within the reset() method are executed the FIRST time the simulation is INITIALIZED, and every time the RESET button is clicked after that. Note that the RESET button appears when the app is first loaded, but does not appear again until the app has been STARTED and STOPPED, and NEW is clicked.
initialize - Sets the initial conditions of your simulation. It should read and store values from the control panel and add objects to the PlotFrame. The commands within this method are executed once, or every time the INITIALIZE button in clicked.
doStep - Invoked every 1/10 second, it defines the actions that do the animation. It is also invoked each time the Step button is pressed.
You also need a main method to run the simulation.
Examine the MovingBallApp code, then do the following exercise.
Exercise
Do the following in the MovingBallApp class.
Add code to let the user set the starting X position of the ball.
Make the ball move diagonally across the graph, so as y decreases by 1, x simultaneously increases by 1.
When you get the output below you are finished with this exercise:
For this exercise, you have to determine on your own to which method - reset, initialize, or doStep - to edit for each step.
Note: If you like, you may first make this a static graph, and then change it to an animation.
In src folder, Create a new Java class called SpiralTrailApp. The class should extend AbstractSimulation.
Set up empty methods for reset, initialize, and doStep.
Add a main method to run the simulation.
Create a rectangle using the DrawableShape.createRectangle() method like this:
// The x and y parameters place the center of the rectangle.DrawableShaperect=DrawableShape.createRectangle(x,y,width,height);
Edit the rectangle to be a square with the lower left corner at (1,1) and upper right corner at (5,5). Note that the x and y represent the center of the rectangle, not the upper left corner.
Create a PlotFrame and add the square.
Set the PlotFrame’s preferred min and max x and y so that the square is in the middle of the plot frame.
On the PlotFrame draw a circle centered at each of the lattice points contained in the square. (There should be a total of 25 circles.)
Draw a Trail that starts at the origin, (3, 3), and “steps” outward in a spiral-like shape by first going to (2, 3), then (2, 2), (4, 2), etc. The path should consist of 2 segments of length 1, then 2 segments of length 2, then 2 segments of length 3, etc. and the path turns 90° counter clockwise at the end of each segment. End the Trail at (1, 5).
When you get the output below you are finished with this exercise: