Chapter 11—Arrays and ArrayLists

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

Made with love, by Zachary Langley Applets The Graphics Presentation.
CS 101 Computer Programming Statements Review++. Reviewed so far.. Variables Constants Order of Execution Boolean expressions and variables if statements,
Standard Workflow Scheme v2.0. This is an interactive presentation of the Standard Workflow Scheme v2.0. Feel free to click on the boxes to see the possible.
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Arrays and ArrayLists C H A P T E R slides partially adapted from.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
1 10/30/06CS150 Introduction to Computer Science 1 Functions Chapter 3, page 313.
SE320: Introduction to Computer Games Week 8: Game Programming Gazihan Alankus.
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.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
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.
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.
Little Boxes Words and music by Malvina Reynolds © 1962 by Schoder Music Co. (ASCAP)
Greenfoot Game Programming Club.
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
Overview of Game Maker. Game Maker Version 7.0 Lite (free version) For MS-Windows platforms
Chapter 4—Statement Forms The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Statement Forms C H A P T E R 4 The statements.
EGR 115 Introduction to Computing for Engineers 3D animation in MATLAB Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers.
Practical Session 10: Animation. Animation kinds There are three basic kinds of animation: 1. Moving object 2. Changing pictures 3. Combination of 1 and.
More GUI Components We finish off our examination of GUI components today by looking at a few that we haven’t seen yet –we start off with the JSlider the.
A cannon game ?. Simple version angle from command line, one shot only Coordinate system is “upside-down”: Use dy(int) method to transform y coordinate:
עקרונות תכנות מונחה עצמים תרגול 7: אנימציה. בשבוע שעבר  paint  Graphics  repaint.
KeyListener and Keyboard Events Just as we can implement listeners to handle mouse events, we can do the same for keyboard events (keypresses) –to implement.
Quiz: Design a Product Class Create a definition for a class called Product, which keeps track of the following information: –Name of the product –Weight.
VG101 RECITATION 5 By TAs. CONTENTS How to read Vg101Class.h Samples about graphics About assignment 5 Array.
Five-Minute Review 1.What are the three data structures realized in the Java Collections Framework? 2.What is hashing used for, how does it work? 3.What.
Mouse, Keyboard, Sounds, and Images JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Greenfoot.
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
More Events.
Review.
Chapter 1: Motion.
Karel J Robot.
Interfaces I once attended a Java user group meeting where James Gosling (Java's inventor) was the featured speaker. During the memorable Q&A session,
Week 15 – Monday CS221.
Chapter 19 Java Never Ends
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
reading: Art & Science of Java,
Chapter 13: Advanced GUIs and Graphics
CS 106A, Lecture 24 Interactors and NameSurfer
Project Snake. Project Snake Snake For this project, we’re going to make Snake This includes Making the overall game logic (using a 2D array) Handling.
reading: Art & Science of Java, Ch. 9
CS 106A, Lecture 12 More Graphics
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
CS 106A, Lecture 28 Final Exam Review 2
CS 106A, Lecture 14 Events and Instance Variables
Topic 16 Queues "FISH queue: n.
Board: Objects, Arrays and Pedagogy

עקרונות תכנות מונחה עצמים תרגול 7: אנימציה
SSEA Computer Science: Track A
Graphics.
Review for Final Exam.
Border Layout, using Panels, Introduction to PinBallGame
Lecture 9 Announcements.
CE 221 Data Structures and Algorithms
Greenfoot November 8, 2009.
A Methodical Approach to Methods
Chapter 1 Observing Motion by Using a Reference Point
Session 29 Introducing CannonWorld (Event-driven Programming)
Advanced GUIs and Graphics
Presentation transcript:

Chapter 11—Arrays and ArrayLists The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java C H A P T E R 1 1 Arrays and ArrayLists Little boxes, on a hillside, little boxes made of ticky-tacky Little boxes, little boxes, little boxes, all the same There’s a green one and a pink one and a blue one and a yellow one And they're all made out of ticky-tacky and they all look just the same —Malvina Reynolds, “Little Boxes,” 1962 Lecture Slides, Part III CS101 @ Özyeğin University Slides are adapted from the originals available at http://www-cs-faculty.stanford.edu/~eroberts/books/ArtAndScienceOfJava/ Chapter 11—Arrays and ArrayLists

Example: SimpleSnake In this problem we will implement a simple version of the snake game in which a snake bounces from the walls of the screen.

SimpleSnake Each part of the snake body will have a size of BODY_PART_WIDTH. There are SNAKE_LENGTH many body parts. Each body part is a GOval object. All the body parts are kept in a GOval array. Corresponding to each body part, we keep the velocity in the x direction and in the y direction. Food for thought: Why don’t we keep a single velocity value? private GOval[] snakeBody = new GOval[SNAKE_LENGTH]; private double[] xVelocities = new double[SNAKE_LENGTH]; private double[] yVelocities = new double[SNAKE_LENGTH];

SimpleSnake public void run() { initializeSnakeBody(); initializeVelocities(); while(true) { moveSnake(); checkBounds(); pause(20); }

SimpleSnake (initializing body parts) BODY_PART_WIDTH BODY_PART_WIDTH BODY_PART_WIDTH / sqrt(2)

SimpleSnake public void initializeSnakeBody() { int xCenter = getWidth() / 2; int yCenter = getHeight() / 2; for(int i = 0; i < snakeBody.length; i++) { snakeBody[i] = createSnakeBodyPartCenteredAt(xCenter, yCenter); xCenter -= (BODY_PART_WIDTH/Math.sqrt(2)); yCenter -= (BODY_PART_WIDTH/Math.sqrt(2)); add(snakeBody[i]); } public GOval createSnakeBodyPartCenteredAt(int xCenter, int yCenter) { GOval circle = new GOval(xCenter - BODY_PART_WIDTH/2, yCenter - BODY_PART_WIDTH/2, BODY_PART_WIDTH, BODY_PART_WIDTH); circle.setFilled(true); circle.setFillColor(Color.BLUE); return circle;

SimpleSnake public void initializeVelocities() { for(int i = 0; i < xVelocities.length; i++) { xVelocities[i] = 3; } for(int i = 0; i < yVelocities.length; i++) { yVelocities[i] = 3; public void moveSnake() { for(int i = 0; i < snakeBody.length; i++) { snakeBody[i].move(xVelocities[i], yVelocities[i]); public void checkBounds() { double x = snakeBody[i].getX(); double y = snakeBody[i].getY(); if(x < 0 || x > getWidth() - BODY_PART_WIDTH) { xVelocities[i] *= -1; if(y < 0 || y > getHeight() - BODY_PART_WIDTH) { yVelocities[i] *= -1;

SimpleSnake v.2 In this problem we will implement yet another simple version of the snake game. P.S. You may need to click on the window to request focus so that key events will be captured.

SimpleSnake v.2 In this version, there is a single, constant SPEED value. The head of the snake has a direction, which can be one of NORTH, EAST, SOUTH, WEST. After each move, a body part replaces the part in the front. snake direction

SimpleSnake v.2 In this version, there is a single, constant SPEED value. The head of the snake has a direction, which can be one of NORTH, EAST, SOUTH, WEST. After each move, a body part replaces the part in the front. snake direction

SimpleSnake v.2 Moving the snake parts: Each part element gets the location of the element in front of it. Head element is moved according to its direction. 0 1 2 3 4 5 6 7

SimpleSnake public class SimpleSnakeV2 extends GraphicsProgram { private final int BODY_PART_WIDTH = 20; private final int SNAKE_LENGTH = 12; private final int SPEED = BODY_PART_WIDTH; private GOval[] snakeBody = new GOval[SNAKE_LENGTH]; private final int NORTH = 1; private final int EAST = 2; private final int SOUTH = 3; private final int WEST = 4; // Direction of the head is one of {N, W, S, E} private int direction = EAST; // Cont’d on the next slide // . . .

public void run() { addGrid(); initializeSnakeBody(); while(true) { moveSnake(); pause(200); } public void addGrid() { for(int i = 0; i < getWidth(); i += BODY_PART_WIDTH) { add(new GLine(i, 0, i, getHeight())); for(int i = 0; i < getHeight(); i += BODY_PART_WIDTH) { add(new GLine(0, i, getWidth(), i));

public void initializeSnakeBody() { int x = SNAKE_LENGTH * BODY_PART_WIDTH; int y = getHeight() / 2; for(int i = 0; i < snakeBody.length; i++) { snakeBody[i] = createSnakeBodyAt(x, y); x -= BODY_PART_WIDTH; add(snakeBody[i]); } public GOval createSnakeBodyAt(int x, int y) { GOval circle = new GOval(x, y, BODY_PART_WIDTH, BODY_PART_WIDTH); circle.setFilled(true); circle.setFillColor(Color.BLUE); return circle;

public void moveSnake() { // Each body part replaces the one in front of it for(int i = snakeBody.length - 1; i >= 1; i--) { GOval prevPart = snakeBody[i-1]; snakeBody[i].setLocation(prevPart.getX(), prevPart.getY()); } // Move the head if(direction == EAST) { snakeBody[0].move(SPEED, 0); } else if(direction == WEST) { snakeBody[0].move(-SPEED, 0); } else if(direction == NORTH) { snakeBody[0].move(0, -SPEED); } else if(direction == SOUTH) { snakeBody[0].move(0, SPEED);

// The method below is invoked whenever the user presses a key // You do not need to spend too much time understanding this part public void keyPressed(KeyEvent keyEvent) { // VK_UP: key code for up arrow key // VK_RIGHT: key code for right arrow key // VK_DOWN: key code for down arrow key // VK_LEFT: key code for left arrow key if(keyEvent.getKeyCode() == KeyEvent.VK_UP) { direction = NORTH; } else if(keyEvent.getKeyCode() == KeyEvent.VK_RIGHT) { direction = EAST; } else if(keyEvent.getKeyCode() == KeyEvent.VK_DOWN) { direction = SOUTH; } else if(keyEvent.getKeyCode() == KeyEvent.VK_LEFT) { direction = WEST; } public void init() { setSize(600, 400); addKeyListeners(); // required for key interaction