Week 7 - Friday What did we talk about last time? Array examples - - PowerPoint PPT Presentation

week 7 friday what did we talk about last time array
SMART_READER_LITE
LIVE PREVIEW

Week 7 - Friday What did we talk about last time? Array examples - - PowerPoint PPT Presentation

Week 7 - Friday What did we talk about last time? Array examples 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


slide-1
SLIDE 1

Week 7 - Friday

slide-2
SLIDE 2

 What did we talk about last time?  Array examples

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5

 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

slide-6
SLIDE 6

 Swapping the values of two variables is a fundamental

  • peration in programming

 It is going to become more important in arrays because now

the order of variables has become important

 The simplest way to swap two variables involves using a third

variable as a temporary location

slide-7
SLIDE 7

 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;

slide-8
SLIDE 8

 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; }

slide-9
SLIDE 9

 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

slide-10
SLIDE 10
slide-11
SLIDE 11

 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

slide-12
SLIDE 12
slide-13
SLIDE 13

 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)

slide-14
SLIDE 14

 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)

slide-15
SLIDE 15

 Let's draw a box then divide it into two halves, like so:

slide-16
SLIDE 16
slide-17
SLIDE 17

 There are built in commands for drawing:

  • Circles
  • Squares
  • Arbitrary polygons
  • Filled versions of each one of these

 We won't bother with the arbitrary polygons  It is also possible to set the color

slide-18
SLIDE 18

 Here are some methods for drawing circles and squares and

setting the color for doing so:

Method Use

void circle(double x, double y, double r) Draw a circle centered at (x,y) with radius r void filledCircle(double x, double y, double r) Draw a filled circle centered at (x,y) with radius r void square(double x, double y, double r) Draw a square centered at (x,y) with edges 2r void filledSquare(double x, double y, double r) Draw a filled square centered at (x,y) with edges 2r void setPenColor(Color c) Start drawing with color c

slide-19
SLIDE 19

 Eventually you will be able to define your own colors  For now you are limited to 13 presets  For example, to make something magenta, you would use the

value StdDraw.MAGENTA

BLACK BLUE CYAN DARK_GRAY GRAY GREEN LIGHT_GRAY MAGENTA ORANGE PINK RED WHITE YELLOW

slide-20
SLIDE 20

 Let's write some code for making 100 circles at random

locations with random sizes and random colors

 Location is easy  Size is easy, we just decide on the range of sizes we want and

do some math

 Color is more painful

  • We need a switch statement with 13 choices
slide-21
SLIDE 21

 We just want to make a pattern of black and white squares on

the screen

 Hint: We need two loops

slide-22
SLIDE 22
slide-23
SLIDE 23

 A number of methods are given to give us more control over

the display

Method Use void setXscale(double x0, double x1) Set the x scale void setYscale(double y0, double y1) Set the y scale void setPenRadius(double r) Set the pen radius void setCanvasSize(int w, int h) Set canvas size void clear() Clear canvas to white void clear(Color c) Clear canvas to color c void pause(int delay) Delay for delay ms

slide-24
SLIDE 24

 As you have seen, the default scale of the canvas is in the

range [0,1] for both x and y

 We can use the setXscale() method to set the minimum

and maximum x values

 We can use the setYscale() method to set the minimum

and maximum y values

 Useful for plotting functions

slide-25
SLIDE 25

 Note that changing the scale doesn't change the size of the

window, just what is shown in it

 If you want to change the size of the window, use the

setCanvasSize() method to set the width and the height

  • f the canvas in terms of screen pixels
slide-26
SLIDE 26

 The pause() method lets you specify a delay in milliseconds

before things are drawn on the screen

 You can use it to slow down or speed up animations  To use it right, you need to first call

StdDraw.enableDoubleBuffering()

 Then, whenever you want to actually show all the drawing

that's been done, you call StdDraw.show()

 Doing so draws everything off-screen and then shows it all at

  • nce, which is both more efficient and more attractive for

animations

slide-27
SLIDE 27
slide-28
SLIDE 28

 Finish StdDraw examples  Static methods

slide-29
SLIDE 29

 Read Chapter 8 of the textbook  Keep working on Project 3

  • It's harder than the previous two!