Organizing Memory in Java

Slides:



Advertisements
Similar presentations
Introduction to Computing Science and Programming I
Advertisements

Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Games and Simulations O-O Programming in Java The Walker School
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Java is an object oriented programming language In this model of programming all entities are objects that have methods and fields Methods perform tasks.
Introduction to programming in the Java programming language.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer.
Lesson 3: Arrays and Loops. Arrays Arrays are like collections of variables Picture mailboxes all lined up in a row, or storage holes in a shelf – You.
Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:
Python Let’s get started!.
The for loop.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
int [] scores = new int [10];
Jeopardy $100 VariablesErrorsLoops Classes and Objects Program Structure $200 $300 $400 $500 $400 $300 $200 $100 $500 $400 $300 $200 $100 $500 $400 $300.
Comp1004: Introduction III Java. Content How Java Works: The JVM Writing a Class in Java – Class – Member Variables – Method – Statement Magic incantations.
CSC 211 Java I for loops and arrays.
Information and Computer Sciences University of Hawaii, Manoa
Arrays.
Array in C# Array in C# RIHS Arshad Khan
Chapter 4 - Finishing the Crab Game
Values vs. References Lecture 13.
Whatcha doin'? Aims: To start using Python. To understand loops.
Python Let’s get started!.
Tuples and Lists.
Lecture 5: Some more Java!
Debugging and Random Numbers
Programming – Touch Sensors
CS 177 Week 15 Recitation Slides
Statements, Comments & Simple Arithmetic
The Little Crab Scenario
Arrays, For loop While loop Do while loop
File Handling Programming Guides.
Variables ICS2O.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
int [] scores = new int [10];
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Arrays 6-Dec-18.
Coding Concepts (Data Structures)
Module 4 Loops.
Game Loop Update & Draw.
Arrays ICS2O.
int [] scores = new int [10];
Stacks Abstract Data Types (ADTs) Stacks
Chapter 2 Programming Basics.
Loops and Arrays in JavaScript
Data Structures & Algorithms
Unit 3: Variables in Java
Arrays 2-May-19.
Assignment due Write a program the generates a random integer expression, presents the two operands and the result to the user, and asks the user to tell.
Just Enough Java 17-May-19.
Variables in C Topics Naming Variables Declaring Variables
How do you do the following?
Review for Midterm 3.
Class code for pythonroom.com cchsp2cs
Web Programming and Design
SPL – PS2 C++ Memory Handling.
Introduction to Computer Science
Presentation transcript:

Organizing Memory in Java Arrays Organizing Memory in Java

What is an Array You can think of an Array like a container, it holds several things Previously when we worked with variables each variable represented ONE item (number, text, image, etc). Arrays are unique in that they are stored in a single contiguous block of memory

Declaring an Array int[] scores = new int[5]; Square brackets, [], tell Java that this is more than just an integer

Elements can be initialized at declaration: Index/Subscript 84 93 97 78 62 score[1] score[3] score[4] score[0] score[2] Elements can be initialized at declaration: int scores[] = {78,84,62,93,97};

Computer Scientists count from 0 84 93 97 78 62 score[1] score[3] score[4] score[0] score[2] The first position is score[0] For efficiency, an array is the address of the first element. The number in brackets represents how far from the first item to move. score[4] means start at score and move over by 4 integers (arriving at the 5th)

length In Java, arrays have a handy property called length You can find out how many items are in the array scores.length

Task #1 We’re going to play more with Greenfoot and use an Array to do it. Your task will be to bring a character to life with an animation

Finding an animation

Copy/Paste the URL

Getting the frames The process we are about to do is called “exploding” a gif There are free online sites that are usually the simplest option http://ezgif.com/split

Greenfoot Project Folder Save your images in the images folder Keeps your work organized Allows you to refer to images by relative path rather than absolute

Other useful tasks

Animation with an array Think of the Array like a filmstrip Each index holds 1 image in the animation As the “filmstrip” plays, individual frames make up a movie 1 2 3 4 5

Java private final static GreenfootImage[] IDLE = { new GreenfootImage("MarioIdle (1).gif"), new GreenfootImage("MarioIdle (2).gif"), new GreenfootImage("MarioIdle (3).gif"), new GreenfootImage("MarioIdle (4).gif"), new GreenfootImage("MarioIdle (5).gif"), new GreenfootImage("MarioIdle (6).gif") }; 1 2 3 4 5 MarioIdle (1) MarioIdle (2) MarioIdle (3) MarioIdle (4) MarioIdle (5) MarioIdle (6)

Java private final static GreenfootImage[] IDLE = { new GreenfootImage("MarioIdle (1).gif"), new GreenfootImage("MarioIdle (2).gif"), new GreenfootImage("MarioIdle (3).gif"), new GreenfootImage("MarioIdle (4).gif"), new GreenfootImage("MarioIdle (5).gif"), new GreenfootImage("MarioIdle (6).gif") }; private – hide the array from everyone but Mario (no one else needs it) final – a constant, don’t allow changes!  NOTICE THE ALL CAPS static – shared by all the Marios that are put in the game GreenfootImage[] – tells Java what we are keeping track of, an array of images IDLE – the name in PROPER style for a constant { contents, of, array, separated, by, commas };

Control You’ve probably never though of it before but something has to keep track of which frame to display We need a variable for Mario to remember 1 2 3 4 5 frame_

Java public class Mario extends Actor { //initialized in the class because it is static – shared by all Marios private final static GreenfootImage[] IDLE = {new GreenfootImage("MarioIdle (1).gif"), new GreenfootImage("MarioIdle (2).gif"), new GreenfootImage("MarioIdle (3).gif"), new GreenfootImage("MarioIdle (4).gif"), new GreenfootImage("MarioIdle (5).gif"), new GreenfootImage("MarioIdle (6).gif")}; private int frame_;//declare the things Mario needs to remember public Mario() frame_ = 0;//initialized in the constructor – belongs to a Mario } …

static demo private static int frame_ vs private int frame Mario has 6 frames in his animation To help you see the bug of sharing (static) the frame_ variable I’ll put 6 Marios into the World

act() Greenfoot is a game engine with a simple rule Every frame of the movie it asks everything in the game (Worlds/Levels, Actors/Characters, etc.) how it should behave It does this through the act() method public void act() Everyone gets a turn to act before the next frame is rendered

act() How should Mario act()? Update frame of animation Display the current image

Update animation frame Increment the frame number If you have gone past the end of the animation move the frame back to the beginning this will loop the animation frame_++; if( frame_ >= IDLE.length ) { frame_ = 0; }

If statements conditional statements allow you to tell Java when code should (and should not) be executed Syntax: if( condition ) { code to execute when condition is true (ignored when false) } Note indenting and braces {}

updateAnimation public void updateAnimation() { setImage( IDLE[ frame_ ] ); frame_++; if( frame_ >= IDLE.length ) frame_ = 0; }

act public void act() { // Add your action code here. updateAnimation(); } The [ ] brackets let you access the pieces in the array

[] brackets 1 2 3 4 5 IDLE[0] IDLE[1] IDLE[2] IDLE[3] IDLE[4] IDLE[5]

Your turn We will develop a character that can later be built into a video game To cut down on complexity, try remove gravity from your game (top down – Legend of Zelda, aircraft, space, etc.) We can negotiate games that require gravity/platforms but you should run it past me first We will improve their functionality later Learn to use: An array If statements Declare class variables Continue to: Make methods Declare local variables