Variance is a measurement of how spread out numbers are from their mean
One way to calculate it requires that we find the mean first The formula is:
Where N is the number of elements, xi is the ith element, and �x is the mean
∑=
−=N
ii xx
N 1
22 )(1σ
Given an array of doubles called numbers, here's the code for finding their variance
double average = 0;double variance = 0;
for (int i = 0; i < numbers.length; ++i)average += numbers[i];
average /= numbers.length;
for (int i = 0; i < numbers.length; ++i) {double temp = numbers[i] – average;variance += temp*temp;
}variance /= numbers.length;
Swapping the values of two variables is a fundamental operation in programming
The simplest way to swap two variables involves using a third variable as a temporary location
Think about what you would do if you had two flasks of chemicals and you wanted to switch their contents
x ytemp
y tempx
xtempy
We can implement this algorithm in code, using a temporary variable
This kind of swap is often referred to as a three-line swap
int temp = x;x = y;y = temp;
We can use the same idea to swap values inside of arrays This idea is even more useful for arrays because the order of
variables inside of an array matters Just like before, we use a third variable as a temporary
location
Here is an example of swapping two Strings indexed i and j in an array of Strings called array
int i = in.nextInt();int j = in.nextInt();
String temp = array[i];array[i] = array[j];array[j] = temp;
We can represent a deck of cards as an array of 52 items One easy way is to make each item a String giving the
name of the card We can extend the lab with cards and store each of these
names in an array
Using the swap code, we can do a random shuffling of a deck To do so, we go through each element of the array, and
randomly swap it with any of the later elements
for (int i = 0; i < deck.length; ++i) {int exchange = i + (int)(Math.random() *(deck.length - i));
temp = deck[i];deck[i] = deck[exchange];deck[exchange] = temp;
}
Searching through an array is an important operation The simplest way to do so is just linear search: check every
element in the array Searching and sorting are really key to all kinds of problems We'll cover both topics in depth in a few weeks
StdDraw is a library of Java code developed by Robert Sedgewick and Kevin Wayne
StdDraw allows you to draw output on the screen easily You can draw points, lines, and polygons in various colors You can clear and resize the drawing area and even save the
results StdDraw is not standard Java that everyone uses, but it's a
nice tool for graphics
The simplest things you can draw with StdDraw are lines and points
The first thing you should be aware of is that the canvas is drawn like Quadrant I of a Cartesian plane
(0,0)
(0,1) (1,1)
(1,0)
The following methods can be used to draw lines and points
Method Use
void line(double x0, double y0, double x1, double y1)
Draw a line from (x0,y0) to (x1,y1)
void point(double x, double y) Draw a point at (x,y)