COMP 110 Lab 6, more arrays - PowerPoint PPT Presentation

About This Presentation
Title:

COMP 110 Lab 6, more arrays

Description:

Lab 6: making methods. When you write a method ... g.setColor(Color.YELLOW); else. g.setColor(Color.BLACK); 14. Lab 6: setRandomColor, technique 2 ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 32
Provided by: luv
Learn more at: http://www.cs.unc.edu
Category:
Tags: comp | arrays | lab | more | yellow

less

Transcript and Presenter's Notes

Title: COMP 110 Lab 6, more arrays


1
COMP 110Lab 6, more arrays
  • Luv Kohli
  • November 12, 2008
  • MWF 2-250 pm
  • Sitterson 014

2
Announcements
  • Lab 7 due, Friday 2pm

3
Questions?
4
Today in COMP 110
  • Lab 6
  • More about arrays
  • Program 4 exercise?

5
Extra credit points on Program 4 lowered
6
Why?
  • They are somewhat arbitrary right now

7
Extra credit policy
  • Working on it, but there will probably be some
    sort of cap
  • Basic idea (not finalized) is that extra credit
    can help pull you up half a letter grade if
    youre on the border

8
Lab 6
  • Class solutions posted
  • Cool patterns!
  • Pat yourself on the back for coming up with
    interesting stuff
  • I made a couple of mistakes
  • radius in drawCircle
  • setBackground

9
Lab 6 setting a color
  • 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

10
Lab 6 making methods
  • When you write a method
  • The methods name should tell you the methods
    purpose
  • The parameter names should be descriptive and
    support the methods 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)

11
Lab 6 setRandomColor
  • 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

12
Lab 6 setRandomColor
  • Math.random() returns a value in the range 0.0,
    1.0)
  • In other words
  • 0.0 lt Math.random() lt 1.0

0.0
1.0
13
Lab 6 setRandomColor
  • 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

Math.random() returns 0.2374
0.0
1.0
14
Lab 6 setRandomColor, technique 1
  • double rnd Math.random()
  • if (rnd gt 0.0 rnd lt 0.2)
  • g.setColor(Color.RED)
  • else if (rnd gt 0.2 rnd lt 0.4)
  • g.setColor(Color.GREEN)
  • else if (rnd gt 0.4 rnd lt 0.6)
  • g.setColor(Color.BLUE)
  • else if (rnd gt 0.6 rnd lt 0.8)
  • g.setColor(Color.YELLOW)
  • else
  • g.setColor(Color.BLACK)

15
Lab 6 setRandomColor, technique 2
  • Scale the range, and then divide it into
    subranges
  • What if we multiply rnd by 5?
  • Then 0.0 lt rnd lt 5.0

0.0
1.0
16
Lab 6 setRandomColor, technique 2
  • 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

17
Lab 6 setRandomColor, technique 2
  • 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

18
Why? Is it any better?
  • 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?

19
Lab 6 setRandomColor, 6 colors, technique 1
  • double rnd Math.random()
  • if (rnd gt 0.0 rnd lt (1.0 / 6.0))
  • g.setColor(Color.RED)
  • else if (rnd gt (1.0 / 6.0) rnd lt (2.0 / 6.0))
  • g.setColor(Color.GREEN)
  • else if (rnd gt (2.0 / 6.0) rnd lt (3.0 / 6.0))
  • g.setColor(Color.BLUE)
  • else if (rnd gt (3.0 / 6.0) rnd lt (4.0 / 6.0))
  • g.setColor(Color.YELLOW)
  • else if (rnd gt (4.0 / 6.0) rnd lt (5.0 / 6.0))
  • g.setColor(Color.YELLOW)
  • else
  • g.setColor(Color.BLACK)

20
Lab 6 setRandomColor, 6 colors, technique 2
  • 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

21
Lab 6 setRandomColor, how about arrays?
  • Color colors Color.RED, Color.GREEN,
  • Color.BLUE, Color.YELLOW, Color.BLACK
  • double rnd Math.random()
  • int choice (int) (rnd 5)
  • g.setColor(colorschoice)

22
Lab 6 think about nested loops
  • for (int i 0 i lt 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 lt 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 lt 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)

23
Lab 6 nested loops
  • for (int count 0 count lt 3 count)
  • for (int i 0 i lt 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)

24
What did we learn earlier?
  • 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
  • I shouldnt try to explain things on the
    whiteboard unless Ive rehearsed what Im
    explaining

25
Arrays of objects
  • Smiley smilies new Smiley3
  • for (int i 0 i lt smilies.length i)
  • smiliesi new Smiley()

26
Arrays of objects
  • When you create an array of objects like this
  • Student students new Student35
  • Each of the elements of students is not yet an
    object
  • You have to instantiate each individual one
  • students0 new Student()
  • students1 new Student()
  • or do this in a loop

27
Arrays are special kinds of objects
  • Therefore, they are subject to the same sorts of
    behaviors as objects

28
What does this code output?
  • public static void changeArray(int arr)
  • int newArray new intarr.length
  • newArray0 12
  • arr newArray
  • public static void main(String args)
  • int arr 3, 6, 15
  • changeArray(arr)
  • for (int x arr)
  • System.out.println(x)

Output 3 6 15 The parameter is local to
changeArray, reassigning does not change the
argument
29
What does this code output?
  • public static void changeArray(int arr)
  • arr0 12
  • public static void main(String args)
  • int arr 3, 6, 15
  • changeArray(arr)
  • for (int x arr)
  • System.out.println(x)

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
30
What does this code output?
  • public static void changeArray(int arr)
  • arr0 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)

Output 12 6 15 arr and newArray both contain
the same address, and therefore refer to the same
data in memory
31
Friday
  • Help with Program 4
Write a Comment
User Comments (0)
About PowerShow.com