Firefly Synchronisation Java Demo in 1D Array. 1 Dimension Firefly in Java 4 4 1 1 0 0 5 5 7 7 8 8 3 3 1 1 4 4 0 0 two neighbors 1. Initialization: 2.

Slides:



Advertisements
Similar presentations
IS Programming Fundamentals 1 (Lec) Date: September 14, Array.
Advertisements

Solve problems with Java code Algorithms with Java.
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Introduction to Java 2 Programming Lecture 5 Array and Collections.
Iterations for loop. Outcome Introduction to for loop The use of for loop instead of while loop Nested for loops.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming
Introduction to Programming Java Lab 3: Variables and Number types 25 January JavaLab3.ppt Ping Brennan
Input review If review Common Errors in Selection Statement Logical Operators switch Statements Generating Random Numbers practice.
8/7: Ch. 7: Arrays What is an array? Declaring & allocating arrays Program of the day.
Ch 7 Arrays – The 1 st Data Structure : How Store Data ARRAYS: ARRAYS: Table of same type elements (Objects or Primitives). Table of same type elements.
1 Lecture 16/3/11: Contents Example of an array of user-defined objects: Car and Carr Constructor Providing functionalities for a user- defined object.
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Programming Methodology (1). Implementing Methods main.
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Review BP Dari slide pak cahyo pertemuan 7 dan pertemuan 2 dengan sedikit modifikasi.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Computer Programming Lab(7).
PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Java POWERED BY: ARVIND DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING RADHA GOVIND GROUP OF INSTITUTIONS,MEERUT.
Building Java Programs Interactive Programs w/ Scanner What is a class? What is an object? What is the difference between primitive and object variables?
Objects contains data and methods Class – type of object Create class first Then object or instance String – defined class String s; // creates instance.
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
Lecture 15.1 Static Methods and Variables. © 2006 Pearson Addison-Wesley. All rights reserved Static Methods In Java it is possible to declare.
Building Java Programs
Computer Programming Lab 8.
Building Java Programs Chapter 5
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Computer Programming Lab(4).
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
Computer Programming Lab(5).
Intro to Java Programming  A computer follows the instruction precisely and exactly.  Anything has to be declared and defined before it can be used.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
Lecture 5 Methods. Sometimes we want to perform the same sequence of operations multiple times in a program. While loops allow us to do this, they are.
CS1101: Programming Methodology Aaron Tan.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Using Java Class Library
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
TOPIC 6 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Creating and Using Class Methods. Definition Class Object.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Introduction to programming in java
Chapter 7 User-Defined Methods.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
using System; namespace Demo01 { class Program
Java Course Review.
SELECTION STATEMENTS (1)
Chapter 8: Collections: Arrays
Chapter 5 – Part 2 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Introduction to Classes and Methods
class PrintOnetoTen { public static void main(String args[]) {
Methods/Functions.
Presentation transcript:

Firefly Synchronisation Java Demo in 1D Array

1 Dimension Firefly in Java two neighbors 1. Initialization: 2. After 1 iteration: * reset neighbors flashing 3. Next iteration: *

Input for your program: grid size: number of fireflies (e.g. 10 fireflies) cycle length: value range that fireflies can have (e.g. if length = 10, value increases from 0 to 9; when value = 9, the firefly flashes.) firing threshold: the value to decide whether to reset the current status. (e.g. if threshold = 6, reset firefly when its neighbor is flashing and its status value <= 6) Time step: how many iterations we want firefly to synchronise (e.g. 200), to stop our program.

1 Dimension Firefly in Java A set of fireflies => integer array int [] fireflies = new int[sizeGrid]; Input value from console to set predefined parameters => use class “Scanner” Initialize random integer values to fireflies => use class “Random”, and its method “nextInt()” Can declare your method in “static”: static method can be called without creating any object instance, and usually used to do some generic calculation. e.g. Math.max(i,j)

Main method public static void main(String[] args) { // inputs for running firefly algorithm int gridSize = 10; int cycleLength = 10; int firingThreshold = 6; int numTimeSteps = 200; // call our main method here by passing on the 4 inputs simpleFireflies(gridSize, cycleLength, firingThreshold, numTimeSteps); }

simpleFireflies() public static void simpleFireflies(int sizeGrid, int cycleLength, int firingThreshold, int numTimeSteps) { Random rn = new Random(); // Declare fireflies' states int [] fireflies = new int[sizeGrid]; // Init fireflies' states for (int num=0; num<sizeGrid; num++) fireflies[num] = rn.nextInt(cycleLength); // Check the fireflies' states for a certain number of time steps for (int timeStep = 0; timeStep < numTimeSteps; timeStep++) { printFirefliesFlash(fireflies, cycleLength);//print current status fireflies = updateFirefliesState(fireflies, cycleLength, firingThreshold); }

printFirefliesFlash() – print current status of each firefly // Print flash if it's flashing time public static void printFirefliesFlash(int [] fireflies, int cycleLength) { for (int num=0; num<fireflies.length; num++) { if (fireflies[num] == cycleLength - 1) // it is flashing System.out.print(fireflies[num]+"*\t"); // output a “*” else // not flashing System.out.print(fireflies[num]+"-\t"); // output a “-” } System.out.println(); }

updateFirefliesState() - Update fireflies' state public static int [] updateFirefliesState(int [] fireflies, int cycleLength, int firingThreshold){ int [] firefliesTmp = new int[fireflies.length]; for (int num=0; num<fireflies.length; num++) { // if rules are satisfied, reset the firefly if (fireflies[num] < firingThreshold && neighborIsFlashing(fireflies, num, cycleLength)) { firefliesTmp[num] = 0; } else {// otherwise, increase the status value by 1 firefliesTmp[num] = (fireflies[num] + 1) % cycleLength; } return firefliesTmp; // return the updated status values of fireflies } Symbol “a%b”: modular arithmetic, return the remainder in division a/b.

neighborIsFlashing() - if fireflies[num]'s neighbor or itself is flashing public static boolean neighborIsFlashing(int [] fireflies, int num, int cycleLength){ for (int n = Math.max(0, num-1); n <= Math.min(fireflies.length-1, num+1); n++) { if (fireflies[n] == cycleLength - 1) return true; } return false; } When n = Math.max(0, num-1), fireflies[n] is the left neighbor of fireflies[num] if fireflies[num] is not the first firefly in the array. When n = Math.min(fireflies.length-1, num+1), fireflies[n] is the right neighbor of fireflies[num] if fireflies[num] is not the last firefly in the array num num-1num+1

Output Try to adjust the initial parameter settings you give, and see how they affect the synchronisation. 1 st iteration 100 th iteration

Tips for 2-Dimension Firefly Redefine neighbors Use 2D array: int [][] fireflies = new int[sizeGrid][sizeGrid]; Use nested loops to read and update status values: for(1 st dimension){ for(2 nd dimension){ }