





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






















!
2
objects functions and modules graphics, sound, and image I/O arrays any program you might want to write conditionals and loops Math text I/O primitive data types assignment statements store and manipulate huge quantities of data
4
// tedious and error-prone double a0, a1, a2, a3, a4, a5, a6, a7, a8, a9; a0 = 0.0; a1 = 0.0; a2 = 0.0; a3 = 0.0; a4 = 0.0; a5 = 0.0; a6 = 0.0; a7 = 0.0; a8 = 0.0; a9 = 0.0; … a4 = 3.0; … a4 = 8.0; … double x = a4 + a8;
5
// easy alternative double[] a = new double[ 10 ]; … a[ 4 ] = 3.0; … a[ 8 ] = 8.0; … double x = a[ 4 ] + a[ 8 ]; declares, creates, and initializes [stay tuned for details]
8
int N = 10 ; // size of array double[] a = new double[N]; // declare, create, init int N = 10 ; // size of array double[] a; // declare the array a = new double[N]; // create the array for (int i = 0 ; i < N; i++) // initialize the array a[i] = 0.0; // all to 0.
9
double[] x = { 0.3, 0.6, 0.1 }; double[] y = { 0.5, 0.1, 0.4 }; int N = x.length; double sum = 0.0; for (int i = 0 ; i < N; i++) { sum = sum + x[i]*y[i]; }
12
String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" }; String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" }; int i = (int) (Math.random() * 13 ); // between 0 and 12 int j = (int) (Math.random() * 4 ); // between 0 and 3 System.out.println(rank[i] + " of " + suit[j]);
14
int N = deck.length; for (int i = 0 ; i < N; i++) { int r = i + (int) (Math.random() * (N-i)); String t = deck[r]; deck[r] = deck[i]; deck[i] = t; } swap^ between i and N- idiom
15
public class Deck { public static void main(String[] args) { String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" }; String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" }; int SUITS = suit.length; int RANKS = rank.length; int N = SUITS * RANKS; String[] deck = new String[N]; for (int i = 0 ; i < RANKS; i++) for (int j = 0 ; j < SUITS; j++) deck[SUITS*i + j] = rank[i] + " of " + suit[j]; for (int i = 0 ; i < N; i++) { int r = i + (int) (Math.random() * (N-i)); String t = deck[r]; deck[r] = deck[i]; deck[i] = t; } for (int i = 0 ; i < N; i++) System.out.println(deck[i]); } } avoid "hardwired" constants build the deck shuffle print shuffled deck
18
assuming each possibility is equally likely for each card that you collect
20
21
under idealized assumptions see ORF 245 or COS 340