Getting Started¶
Welcome to CSX¶
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.
Have a great year!
Introduction to JUnit¶
For this section, you will review Quadratic equations, how to write objects and get introduced to writing test cases.
Problem: Find the roots of a Quadratic.¶
The x
values that make this function equal to 0
are the roots or
zeroes of the function and can be found by using the quadratic
formula.
You are going to write a program that finds to roots (if they exist) for any quadratic equation.
Step 1: Create a Test Class¶
Exercise
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:
1 2 3 4 | import org.junit.Test;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
|
In the body of the QuadraticTest
class, create a test method called getA
, like this:
@Test
public void getA() {
Quadratic q = new Quadratic(1.0, 2.0, 3.0);
assertThat(q.getA(), is(1.0));
}
If you need help, look at the GettingStartedTest
class, in the test folder as an example.
Run the QuadraticTest class. It will fail at first, which is expected.
Step 2: Create a Quadratic Object¶
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
andc
, all doubles. - Write a constructor which takes
a
,b
andc
as params and uses them to set thea
,b
andc
attributes. - Write a public method called
getA
which returns the value ofa
. - Run
QuadraticTest
again. It should pass this time.
Step 3: Add Methods and Test methods¶
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
.
public double getB()
- Return the value of b.
In
QuadraticTest
and add test method forgetB
.
public double getC()
- Return the value of c.
In
QuadraticTest
and add test method forgetC
.
public boolean hasRealRoots()
- See if the quadratic has real roots, return boolean true or false.
Write a test method in
QuadraticTest
calledhasRealRoots
to test it .
public int numberOfRoots()
- Determine the number of real roots, return an int (0, 1, or 2).
Write a test method in
QuadraticTest
callednumberOfRoots
to test it .
public double[] getRootArray()
- Find the roots, return 2 doubles (put them in an array).
Write a test method in
QuadraticTest
calledgetRootArray
to test it .
public double getAxisOfSymmetry()
- Find the axis of symmetry. Return that value.
Write a test method in
QuadraticTest
calledgetAxisOfSymmetry
to test it .
public double getExtremeValue()
- 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
calledgetExtremeValue
to test it .
public boolean isMax()
- 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
calledisMax
to test it .
public double evaluateWith(double x)
- Evaluate the quadratic function at an x value, return f(that x value).
Write a test method in
QuadraticTest
calledevaluateWith
to test it .
When all the tests in QuadraticTest
pass, you are finished with this exercise.
Introduction to Polyfun¶
Getting Started with Polynomials¶
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 whenx
is2
and print the answer.- Add
fx
andvx
and print the answer.- For
vx
, print the coefficient for theX^1
term (also known as simply X).- Use a
for
-loop orfor-each
-loop to print all the coefficients ofvx
When you get the output below you are finished with this exercise:
More Information¶
For more information, see the Polynomial JavaDocs.
Introduction to Open Source Physics (OSP)¶
Step 1: Plotting Polynomials (Static Graph)¶
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 offx
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 graphvx
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 ofgx
(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?
Step 2: Introduction to Animation¶
Open and run the MovingBallApp
.
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.
Background Information: Abstract Classes¶
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.
Step 3: Create Your Own Animation (Random Walk)¶
Now you’ll create an animation from scratch.
Exercise
- In the src folder, create a new Java class called
RandomWalkApp
.- Copy and paste the
MovingBallApp
class as starter code. Change themain
method to runRandomWalkApp
.- Change
doStep
so thatcircle
randomly moves either 1 spot up, 1 spot down, or stays at the same y. It also randomly moves 1 spot left, 1 spot right or stays at the same x.- Add 50 Circles that move in this way.
When you get output similar to the graph below you are finished with this exercise:
HINT: Change the global circle
variable to an ArrayList
of type Circle
called circles
.
HINT: You’ll need a for
-loop in initialize
to add 50 Circles to circles
, a for-each
-loop in doStep
to move each Circle in circles
, and, optionally, a for-each
-loop in stop
to print how far each Circle in circles
moved.
Step 4: (Advanced) Spiral Trail Animation¶
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 extendAbstractSimulation
. - Set up empty methods for
reset
,initialize
, anddoStep
. - 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.
DrawableShape rect = 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:
More Information¶
For more information on Open Source Physics, read the Open Source Physics JavaDocs