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

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

Programming and Data Structure
1 Various Methods of Populating Arrays Randomly generated integers.
Computer Programming Lab 8.
CS0007: Introduction to Computer Programming Methods: Documentation, Reference Parameters, Modularization 2.
Catie Welsh March 2,  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
Math class methods & User defined methods Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods.
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.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
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.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
JAVA Arrays. Objectives Be able to declare and initialize arrays Be able to conceptualize (draw) how arrays are represented in computer memory. Be able.
10/5: Primitives, the for loop Primitive data types –why we mention them Return to counter-controlled repetition.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Lec 20 More Arrays--Algorithms. Agenda Array algorithms (section 7.5 in the book) – An algorithm is a well-defined specification for solving a problem.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
Catie Welsh February 23,  Lab 4 due on Friday  Lab 5 will be assigned on Friday 2.
10/4: the for loop & the switch structure Primitive data types –why we mention them Return to counter-controlled repetition.
Catie Welsh March 23,  Lab 6 due Friday by 1pm 2.
Review for Nested loops & Math class methods & User defined methods.
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.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
CS 5JA Introduction to Java Graphics One of the powerful things about Java is that there is.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
COMP More About Arrays Yi Hong June 05, 2015.
A High Flying Overview CS139 – Fall 2006 How far we have come.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Catie Welsh February 14,  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 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.
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
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:
COMP 110 More arrays, 2D arrays, Program 4
Michele Weigle - COMP 14 - Spr 04 Catie Welsh March 30, 2011
Building Java Programs
Building Java Programs
Midterm Review Problems
An Introduction to Java – Part I
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
class PrintOnetoTen { public static void main(String args[]) {
Michele Weigle - COMP 14 - Spr 04
Building Java Programs
CIS 110: Introduction to Computer Programming
Building Java Programs
Scope scope: The part of a program where a variable exists. From its declaration to the end of the { } braces A variable declared in a for loop exists.
Building Java Programs
Building Java Programs
Building Java Programs
Graphics Reading: Supplement 3G
Presentation transcript:

Catie Welsh April 6, 2011

 Lab 8 due, Friday by 1pm 2

3

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

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

 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

 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

 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

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

 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

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

 Scale the range, and then divide it into subranges  What if we multiply rnd by 5?  Then 0.0 <= rnd <

 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

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

 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

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

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

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

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

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

 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

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

 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

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

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: The parameter is local to changeArray, reassigning does not change the argument

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: The parameter is local to changeArray, but it contains the address of the array passed in, so we can change its elements

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: arr and newArray both contain the same address, and therefore refer to the same data in memory

 Help with Program 4 28