Presentation is loading. Please wait.

Presentation is loading. Please wait.

Catie Welsh April 6, 2011.  Lab 8 due, Friday by 1pm 2.

Similar presentations


Presentation on theme: "Catie Welsh April 6, 2011.  Lab 8 due, Friday by 1pm 2."— Presentation transcript:

1 Catie Welsh April 6, 2011

2  Lab 8 due, Friday by 1pm 2

3 3

4  Lab 7  More about arrays  Array Review Worksheet 4

5  Class solutions posted  Cool patterns!  Pat yourself on the back for coming up with interesting stuff 5

6  Set your color before you draw a shape ◦ Imagine being a painter  You put your paintbrush on the palette to mix and pick up a color before you touch the paintbrush to the canvas 6

7  When you write a method ◦ The method’s name should tell you the method’s purpose ◦ The parameter names should be descriptive and support the method’s purpose public static void drawRect(Graphics g, int x, int y, int radius) when you really meant public static void drawSquare(Graphics g, int x, int y, int side) 7

8  Important to know how to convert from one range to another ◦ Useful in many situations  Scaling images  Converting mouse clicks to regions on your user interface (for example, the grid cells in Program 4)  Drawing parametric functions, or doing computations with parametric functions 8

9  Math.random() returns a value in the range [0.0, 1.0) ◦ In other words  0.0 <= Math.random() < 1.0 9 0.01.0

10  We want 5 colors, chosen randomly ◦ How do we choose them? ◦ Divide our range into 5 subranges  Decide which subrange maps to which color ◦ If we get a random number in a certain range, use the color we decided on for that range 10 0.01.0 0.20.40.60.8 Math.random() returns 0.2374

11 double rnd = Math.random(); if (rnd >= 0.0 && rnd < 0.2) g.setColor(Color.RED); else if (rnd >= 0.2 && rnd < 0.4) g.setColor(Color.GREEN); else if (rnd >= 0.4 && rnd < 0.6) g.setColor(Color.BLUE); else if (rnd >= 0.6 && rnd < 0.8) g.setColor(Color.YELLOW); else g.setColor(Color.BLACK); 11

12  Scale the range, and then divide it into subranges  What if we multiply rnd by 5?  Then 0.0 <= rnd < 5.0 12 0.05.0 1.02.03.04.0 0.01.0 0.20.40.60.8

13  We could use if/else statements as before  Or we can typecast rnd to an int, and then use a switch statement  What is (int) (rnd * 5) if rnd is… ◦ 0.3?  (int) (1.5) is 1 ◦ 0.1?  (int) (0.5) is 0 ◦ 0.91?  (int) (4.55) is 4 13 0.05.0 1.02.03.04.0

14 double rnd = Math.random(); int choice = (int) (rnd * 5); switch (choice) { case 0: g.setColor(Color.RED); break; case 1: g.setColor(Color.GREEN); break; case 2: g.setColor(Color.BLUE); break; case 3: g.setColor(Color.YELLOW); break; case 4: g.setColor(Color.BLACK); break; } 14

15  It depends, but imagine if you suddenly decide you want 6 random colors instead of 5  How would you do it with the if/else statements? 15

16 double rnd = Math.random(); if (rnd >= 0.0 && rnd < (1.0 / 6.0)) g.setColor(Color.RED); else if (rnd >= (1.0 / 6.0) && rnd < (2.0 / 6.0)) g.setColor(Color.GREEN); else if (rnd >= (2.0 / 6.0) && rnd < (3.0 / 6.0)) g.setColor(Color.BLUE); else if (rnd >= (3.0 / 6.0) && rnd < (4.0 / 6.0)) g.setColor(Color.YELLOW); else if (rnd >= (4.0 / 6.0) && rnd < (5.0 / 6.0)) g.setColor(Color.YELLOW); else g.setColor(Color.BLACK); 16

17 double rnd = Math.random(); int choice = (int) (rnd * 6); switch (choice) { case 0: g.setColor(Color.RED); break; case 1: g.setColor(Color.GREEN); break; case 2: g.setColor(Color.BLUE); break; case 3: g.setColor(Color.YELLOW); break; case 4: g.setColor(Color.BLACK); break; case 5: g.setColor(Color.MAGENTA); break; } 17

18 Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, Color.BLACK }; double rnd = Math.random(); int choice = (int) (rnd * 5); g.setColor(colors[choice]); 18

19 for (int i = 0; i < 360; i += 10) { int x = (int) (Math.sin(Math.toRadians(i)) * 20); int y = (int) (Math.cos(Math.toRadians(i)) * 20); drawCircle(g, x, y, 20); } for (int i = 0; i < 360; i += 10) { int x = (int) (Math.sin(Math.toRadians(i)) * 20); int y = (int) (Math.cos(Math.toRadians(i)) * 20); drawCircle(g, x + 50, y, 20); } for (int i = 0; i < 360; i += 10) { int x = (int) (Math.sin(Math.toRadians(i)) * 20); int y = (int) (Math.cos(Math.toRadians(i)) * 20); drawCircle(g, x + 100, y, 20); } 19

20 for (int count = 0; count < 3; count++) { for (int i = 0; i < 360; i += 10) { int x = (int) (Math.sin(Math.toRadians(i)) * 20); int y = (int) (Math.cos(Math.toRadians(i)) * 20); drawCircle(g, x + (50 * count), y, 20); } 20

21  Arrays can be instance variables  Arrays can be of any base type  Arrays can be method parameters  Arrays can be returned from a method  Lots of stuff about multidimensional arrays  Multidimensional arrays are awesome 21

22 Smiley[] smilies = new Smiley[3]; for (int i = 0; i < smilies.length; i++) { smilies[i] = new Smiley(); } 22 104525842836??? true GREEN 3 false BLUE 1 false CYAN 4

23  When you create an array of objects like this: Student[] students = new Student[35];  Each of the elements of students is not yet an object  You have to instantiate each individual one students[0] = new Student(); students[1] = new Student();  …or do this in a loop 23

24  Therefore, they are subject to the same sorts of behaviors as objects 24

25 public static void changeArray(int[] arr) { int[] newArray = new int[arr.length]; newArray[0] = 12; arr = newArray; } public static void main(String[] args) { int[] arr = { 3, 6, 15 }; changeArray(arr); for (int x : arr) { System.out.println(x); } 25 Output: 3 6 15 The parameter is local to changeArray, reassigning does not change the argument

26 public static void changeArray(int[] arr) { arr[0] = 12; } public static void main(String[] args) { int[] arr = { 3, 6, 15 }; changeArray(arr); for (int x : arr) { System.out.println(x); } 26 Output: 12 6 15 The parameter is local to changeArray, but it contains the address of the array passed in, so we can change its elements

27 public static void changeArray(int[] arr) { arr[0] = 12; } public static void main(String[] args) { int[] arr = { 3, 6, 15 }; int[] newArray = arr; changeArray(newArray); for (int x : arr) { System.out.println(x); } 27 Output: 12 6 15 arr and newArray both contain the same address, and therefore refer to the same data in memory

28  Help with Program 4 28


Download ppt "Catie Welsh April 6, 2011.  Lab 8 due, Friday by 1pm 2."

Similar presentations


Ads by Google