Michele Weigle - COMP 14 - Spr 04

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

1 Various Methods of Populating Arrays Randomly generated integers.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
1 COMP 110 More Arrays Tabitha Peck M.S. April 2, 2008 MWF 3-3:50 pm Philips 367.
CS180 Chapter 4 2/1/08. Announcements Project 3 is out –2 weeks due to the exam Exam –Thursday, February 7, 8:30 – 9:30 pm, WTHR 104 –Covers chapters.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
1 Graphical objects We will draw graphics in Java using 3 kinds of objects: DrawingPanel : A window on the screen. Not part of Java; provided by the authors.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
Methods We write methods in our programs for many reasons:
COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014.
Lec 21 More Fun with Arrays: For Loops. Agenda Some backfill for Lab 20: – Using an array in an applet or class – array instance variables – using Math.random()
COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.
Week 61 Introduction to Programming Ms. Knudtzon C Period Tuesday October 12.
Catie Welsh March 23,  Lab 6 due Friday by 1pm 2.
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014.
Catie Welsh April 6,  Lab 8 due, Friday by 1pm 2.
COMP 110 More arrays, 2D arrays, Program 4 Luv Kohli November 10, 2008 MWF 2-2:50 pm Sitterson 014.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
COMP 110 Augustus Gloop, Augustus Gloop… Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110 More about classes Luv Kohli October 3, 2008 MWF 2-2:50 pm Sitterson 014.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Testing It is much better to have a plan when testing your programs than it is to just randomly try values in a haphazard fashion. Testing Strategies:
4. Java language basics: Function
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 21, 2011
COMP 110 More arrays, 2D arrays, Program 4
Michele Weigle - COMP 14 - Spr 04 Catie Welsh March 30, 2011
Building Java Programs
Introduction to programming in java
Building Java Programs
Building Java Programs
Midterm Review Problems
Building Java Programs
COMP 110 More about inheritance
An Introduction to Java – Part I
void Pointers Lesson xx
CS 200 Arrays, Loops and Methods
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Building Java Programs
COMP 110 Information hiding and encapsulation
Topic 8 graphics "What makes the situation worse is that the highest level CS course I've ever taken is cs4, and quotes from the graphics group startup.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh April 11, 2011
COMP 110 Loops, loops, loops, loops, loops, loops…
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
Building Java Programs
Building Java Programs
CIS 110: Introduction to Computer Programming
Building Java Programs
The Generic List<> Collection class
CS150 Introduction to Computer Science 1
Building Java Programs
Announcements Lab 5 was due today Program 3 due Monday by 12pm
Building Java Programs
Building Java Programs
Graphics Reading: Supplement 3G
How do you do the following?
Michele Weigle - COMP 14 - Spr 04
Announcements Lab 5 due Wednesday at noon.
Chapter 2 Lecture 2-3: Loop Figures and Constants reading:
Presentation transcript:

Michele Weigle - COMP 14 - Spr 04 COMP 110 Lab 6, more arrays Luv Kohli November 12, 2008 MWF 2-2:50 pm Sitterson 014

Michele Weigle - COMP 14 - Spr 04 Announcements Lab 7 due, Friday 2pm

Michele Weigle - COMP 14 - Spr 04 Questions? Any other questions?

Michele Weigle - COMP 14 - Spr 04 Today in COMP 110 Lab 6 More about arrays Program 4 exercise?

Extra credit points on Program 4 lowered

Why? They are somewhat arbitrary right now

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 you’re on the border

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

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

Lab 6: making methods When you write a method when you really meant 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)

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

Lab 6: setRandomColor Math.random() returns a value in the range [0.0, 1.0) In other words 0.0 <= Math.random() < 1.0 0.0 1.0

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.2 0.4 0.6 0.8 0.0 1.0

Lab 6: setRandomColor, technique 1 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);

Lab 6: setRandomColor, technique 2 Scale the range, and then divide it into subranges What if we multiply rnd by 5? Then 0.0 <= rnd < 5.0 0.2 0.4 0.6 0.8 0.0 1.0 0.0 5.0 1.0 2.0 3.0 4.0

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 0.0 5.0 1.0 2.0 3.0 4.0

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); case 2: g.setColor(Color.BLUE); case 3: g.setColor(Color.YELLOW); case 4: g.setColor(Color.BLACK); }

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?

Lab 6: setRandomColor, 6 colors, technique 1 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)) else g.setColor(Color.BLACK);

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); case 2: g.setColor(Color.BLUE); case 3: g.setColor(Color.YELLOW); case 4: g.setColor(Color.BLACK); case 5: g.setColor(Color.MAGENTA); }

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(colors[choice]);

Lab 6: think about nested loops 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); } drawCircle(g, x + 50, y, 20); drawCircle(g, x + 100, y, 20);

Lab 6: nested loops 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); }

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 shouldn’t try to explain things on the whiteboard unless I’ve rehearsed what I’m explaining

Arrays of objects 1045 2584 2836 ? true GREEN 3 false BLUE 1 false Smiley[] smilies = new Smiley[3]; for (int i = 0; i < smilies.length; i++) { smilies[i] = new Smiley(); } 1045 2584 2836 ? true GREEN 3 false BLUE 1 false CYAN 4

Arrays of objects 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

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

What does this code output? 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); Output: 3 6 15 The parameter is local to changeArray, reassigning does not change the argument

What does this code output? 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); 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

What does this code output? 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); Output: 12 6 15 arr and newArray both contain the same address, and therefore refer to the same data in memory

Michele Weigle - COMP 14 - Spr 04 Friday Help with Program 4