Introduction to programming the FRDM/MBED in Java David J.

Size: px
Start display at page:

Download "Introduction to programming the FRDM/MBED in Java David J. https://www.cs.kent.ac.uk/~djb/"

Transcription

1 Introduction to programming the FRDM/MBED in Java David J. Introduction This document is an introduction to programming a FRDM/MBED in Java for students who have taken the module CO320, Introduction to Object-Oriented Programming, at The University of Kent. It uses a Java API for an MBED-enabled device developed by staff of The Shed. It is particularly intended to support those taking the module CO334, People and Computing. The FRDM/MBED device For the CO334 module, you will be supplied with a piece of hardware called a Freescale FRDM-K64F plus an MBED application shield. For brevity, in this document we will just call the combined unit an MBED. The unit contains a number of input and output devices, such as buttons, a temperature sensor, a speaker and LEDs. It is connected via a USB cable to a computer, from which it can be programmed. For convenience, we will call the computer to which the MBED is connected, the PC, regardless of what it actually is. The USB connection also supplies the MBED with power, so it needs to be connected when being run. Commands can be sent from the PC to the MBED and data values read from it. In this introductory document, we will program the PC to interact with the MBED using Java. You will probably use the BlueJ environment, but that is not a requirement. Any other Java IDE or the command line is appropriate, although those will require a main method see the example Main class below. Getting started In order to interact with the MBED you will need a compiled copy of the Shed API library, which is stored in a file called ShedMBed.jar. You can obtain a copy from: If you are working with BlueJ, then your project folders will need to have a folder created in them called +libs and you must put ShedMBed.jar into +libs. If you are not using BlueJ then you must make ShedMBed.jar available to the compiler and JVM via their classpath. A BlueJ project template, available from: FRDM is pronounced Freedom.

2 contains a complete program that can be used as a basic starting point for coding interactions with the MBED. It already has the required +libs folder and library JAR file. If you are using BlueJ then the Main class can be ignored. The MBed Emulator You will probably want to practice writing programs for the MBED before you have a physical device. A feature of the Shed API library is that if there is no MBED device attached to the PC (or it cannot be connected to for some reason) an emulator will automatically be displayed that looks like this. This can be interacted with via a mouse to mimic pressing the buttons and turning the potentiometers. The LEDs and LCD screen will display outputs. The template program The program contained in the template.zip file provides a pattern for how to connect to an MBED and to write a simple message on its LCD. It will work with both a real device and the emulator. import shed.mbed.lcd; import shed.mbed.mbed; import shed.mbed.mbedutils; public class Program // The object for interacting with the FRDM/MBED. private MBed mbed; /** * Open a connection to the MBED. */ public Program() mbed = MBedUtils.getMBed(); /** * The starting point for the interactions. */ public void run() // Show a message on the LCD. // Where to show the message. int x = 0, y = 0;

3 LCD lcd = mbed.getlcd(); lcd.print(x, y, "Hello, world!"); // Give us time to read the message. sleep(5000); // Clear the display. lcd.clear(); /** * Close the connection to the MBED. */ public void finish() mbed.close(); /** * A simple support method for sleeping the program. millis The number of milliseconds to sleep for. */ private void sleep(int millis) try Thread.sleep(millis); catch (InterruptedException ex) /** * A template main-method class for use from * non-bluej IDEs or the command line. */ public class Main public static void main(string[] args) Program prog = new Program(); prog.run(); prog.finish(); System.exit(0); Compile the project, create a Program object and then call its run method. A "Hello, world!" message should be displayed on the MBED s screen and then cleared after a few seconds. You can call the run method multiple times but you should finally call the finish method to cleanly close the connection to the MBED.

4 For other IDEs or the command line, the Main class contains the main method. For instance, to compile the program from the command line from within the project folder use the command: javac cp.:+libs/shedmbed.jar Main.java to run it use the command: java cp.:+libs/shedmbed.jar Main The MBed and MBedUtils classes The Shed API, shed.mbed, defines classes for interacting with the different input and output devices on the MBED. Interaction is always started by obtaining an object of type MBed via the MBedUtils class. This results in a connection to an attached MBED device by opening a serial port on the PC to allow data to be transferred between the PC and the MBED. If there is no connected device then the emulator will appear. The MBed class defines methods for obtaining objects corresponding to the various devices on the MBED, such as the temperature sensor, the accelerometer, the magnetometer, the two potentiometers, the two buttons on the FRDM board, the joystick on the application shield, the LEDs, the LCD and the Piezo speaker. Each device class defines appropriate methods for interacting with that particular device. Input devices The Thermometer object for the temperature sensor is accessed via the getthermometer method of the MBed object. The Thermometer object has a gettemperature method that returns a double value representing the temperature in Celsius. The API defines the Button class to provide an abstraction for the two FRDM switch buttons and the MBED Joystick. Accessor methods returning Button objects are defined in the MBed class. The FRDM buttons are SW2 and SW3 these names are inscribed on the board (MBed methods getswitch2 and getswitch3). The joystick acts as five mutually-exclusive buttons: called Left, Right, Up, Down and Fire (MBed methods getjoystickleft, getjoystickright, etc.) For instance: Button firebutton = mbed.getjoystickfire(); Button sw2 = mbed.getswitch2(); In general, we will be interesting in knowing whether a button is in the pressed or released state. The central, released position of the joystick is the released state for all five. Pushing the joystick either left, right, up, down or in sets the pressed state for the associated button. The ispressed method of Button returns a boolean value that indicates whether the button is currently pressed (true) or released (false).

5 The Potentiometer class provides an abstraction for the two blue potentiometers. As with Button objects, a Potentiometer object is obtained via an accessor in the MBed; for instance: Potentiometer p1 = mbed.getpotentiometer1(); A Potentiometer object s getvalue method returns a double value in the range 0.0 to 1.0 (inclusive). The Orientation class is used to provide a description for the orientation of the MBED whether it is Up, Down, Left, Right, etc. The MBed object s getmagnetometer method returns a Magnetometer object whose getorientation and getside methods both return Orientation objects. The getmagnetism method of Magnetometer returns an XYZData object that contains an (x,y,z) triplet describing the MBED s magnetic position. However, this data is not considered to be reliable. The getaccelerometerboard and getaccelerometershield methods of the MBed object return instances of an Accelerometer object for the accelerometers located on the FRDM board and the MBED shield respectively. Their getacceleration methods also return an XYZData object describing the MBED s 3D position. Output devices Writing to any of the output devices is performed through the device-specific objects obtained via accessors in the MBed object: getlcd returns an LCD object with methods: print, clear, getwidth, getheight, setpixel, drawcircle, fillcircle, drawline, drawrectangle, fillrectangle and getlocation. getledboard and getledshield both return LED objects with the following methods: getcolor and setcolor. Names for LED colours are defined in the LEDColor class. getpiezo returns a Piezo object that allows some primitive sounds/music to be played. It has the following methods: playnote, playsound and silence.

6 Basic synchronous programming As illustrated in the run method of the Program class in template project, we can write normal looking Java code to interact with the MBED. There, we have a sequence of statements that write some text on the LCD, pause long enough for the text to be read, and then clear the LCD. As an alternative, we might want to obtain some input from the MBED. For instance, read the current value of the temperature sensor and display that: Thermometer temp = mbed.getthermometer(); lcd.print(x, y, "The temperature is " + temp.readtemperature()); The full Java language is available to program the MBED; for instance: // Show a temperature-dependent message on the LCD. if(temp.readtemperature() >= 20) lcd.print(x, y, "Warm"); else lcd.print(x, y, "Cool"); Typically, programs running on the PC continuously monitor the MBED s sensors and respond either with output actions on the MBED or some other form of action on the PC. For instance, one of the potentiometers might be turned to vary a note played on the Piezo speaker: Potentiometer p1 = mbed.getpotentiometer1(); Piezo speaker = mbed.getpiezo(); while(true) double setting = p1.getvalue(); // Play full volume (1.0) at variable frequency. speaker.playsound(1.0, * setting); sleep(100); This example illustrates three themes that occur quite regularly when programming a device such as the MBED: The program is based on an infinite loop because there is no obvious condition for terminating it. The value of a sensor is read inside the loop and used to affect what subsequent actions the program takes.

7 A period of inaction (sleep) is included in the loop to provide a gap between reads of the sensor. The sensor s value is unlikely to change rapidly and there are occasions when a pause will give other parts of the program a chance to act. Continuously reading the value of a sensor like this is often called polling. Busy waiting The polling style of programming based around infinite loops works well for many programs that are only concerned with a single input device, but there are some circumstances where a different approach can be easier to work with. Consider the following code that uses polling to detect the press of button SW2 in order to change the colour of the Shield s LED: Button sw2 = mbed.getswitch2(); LED led = mbed.getledshield(); led.setcolor(ledcolor.blue); // Keep checking to see if the button has been pressed. while(! sw2.ispressed()) sleep(100); led.setcolor(ledcolor.red); In fact, if all we want the program to do is to wait for a particular button to be pressed then it is clearer to use the method, blockuntilpressed, from the Button class: Button sw2 = mbed.getswitch2(); LED led = mbed.getledshield(); led.setcolor(ledcolor.blue); sw2.blockuntilpressed(); led.setcolor(ledcolor.red); One of the problems with loop-based, busy waiting style of coding is that the PC is fully occupied with the process of waiting for something to happen when, most of the time, nothing is happening. Furthermore, if we wanted the program to be doing something else in the loop while waiting for the button press, then we would have to find a way to integrate those other actions with the process of keeping an eye on the state of the button so that the loop could eventually be terminated. In addition, those other actions might also be applicable once the button has been pressed, meaning they would also have to appear after the loop as well. The complex logic is likely to result in the overall code looking very messy. In general, busy waiting for an event to occur is not the best way to interact with devices. Instead, more convenient programming techniques are available using a system of listeners and event-handling code that are activated by interrupts generated by the devices being waited for. These have the added benefit of making it easier to write code in which multiple events are waited for and where the order of occurrence does

8 not have to be governed by the sequential order of the statements in the program. In other words, they support the concept of asynchronous programming. Listeners and Lambdas The following code attaches a listener to the SW2 button, using Java 8 s lambda notation to specify the event-handling code: Button sw2 = mbed.getswitch2(); LED led = mbed.getledshield(); sw2.addlistener( (boolean ispressed) -> if(ispressed) led.setcolor(ledcolor.red); else led.setcolor(ledcolor.blue); ); The -> notation is used to specify the lambda, which is like an anonymous method. On the left-hand side are any parameters and on the right-hand side is the body of the lambda: the code to be executed when an event of interest occurs. The handler is designed to set the Shield s LED to red when SW2 is pressed and blue when it is not pressed. The event handler is called whenever the button is either pressed or released. Shortcuts are often possible in the lambda notation: When the lambda takes only a single parameter, its type and the parentheses are often omitted. When the lambda body is a single, simple statement the curly brackets are often omitted. When the addlistener call is made on the button, the body of the lambda is not executed immediately. Rather, this statement requests that the lambda should be executed whenever the button s state is changed, with the ispressed parameter being set to either true or false depending on whether the button is in the pressed or released state. The MBED will inform the program running on the PC whenever the button s state changes, so the program no longer has to poll the button. So the code following the call to addlistener can be anything else the PC program needs to do while waiting for the button to be pressed. It will be common for listeners to be added to further input devices, such as other buttons or the potentiometers. For instance, we might want to use one of the potentiometers to control the Piezo speaker:

9 Potentiometer p1 = mbed.getpotentiometer1(); Piezo speaker = mbed.getpiezo(); p1.addlistener(value -> speaker.playsound(1.0, value * 1000)); The type of the lambda s parameter is double: a value in the range 0.0 to 1.0. This is used to moderate the frequency of the note played. The remainder of the run method might look as follows: // Set the starting colour. led.setcolor(ledcolor.blue); speaker.playsound(1.0, p1.getvalue() * 1000); while(!finished) sleep(100); In other words, once the listeners have been attached to SW2 and potentiometer 1, as above, the Shield LED s colour is set to its starting colour and the Piezo speaker plays a note based on the initial setting of potentiometer 1. Thereafter, the program loops forever and leaves the listeners to handle the events generated on the monitored input devices. Notice that this style is significantly different from the synchronous style of iteration you are most used to using. Normally, when we write a loop we expect something to happen in the statements inside the loop that causes the loop s condition to become false. Here, it will be an externally-generated event on the MBED that will cause code in one of the listeners to be executed out of the normal sequence of statements in the loop s body. That code will cause a side-effect in the value of the finished variable such that, the next time the loop s condition is evaluated the loop will terminate. The call to sleep in the body of the loop is important in order to avoid the loop s execution dominating the execution of the JVM. The length of the sleep should be short enough that a change to the value of the loop s controlling variable will be detected in good time. The following listener might be used to control the termination of the loop via SW3: Here we assume that some part of the program will eventually set the finished variable to true. For instance, that could be done by a listener attached to another of the buttons.

10 Button sw3 = mbed.getswitch3(); sw3.addlistener(ispressed -> if(ispressed) finished = true; ); In general, attaching listeners is the preferred way to monitor state changes to input devices such as buttons and potentiometers. However, this mode of monitoring is not available for the other input devices, such as the temperature sensor and the accelerometer. Summary This introductory document contains a brief outline of some of the opportunities for writing programs using an MBED via the Shed Java API. The variety of input and output devices available provides the potential for writing some interesting and innovative applications. Several example programs that have been written with the API are available for you to see how the basics are done.

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

Pace University. Fundamental Concepts of CS121 1

Pace University. Fundamental Concepts of CS121 1 Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction

More information

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018 Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 1: Types and Control Flow http://courses.cs.cornell.edu/cs2110/2018su Lecture 1 Outline 2 Languages Overview Imperative

More information

CSE 142 Su 04 Computer Programming 1 - Java. Objects

CSE 142 Su 04 Computer Programming 1 - Java. Objects Objects Objects have state and behavior. State is maintained in instance variables which live as long as the object does. Behavior is implemented in methods, which can be called by other objects to request

More information

Arduino Prof. Dr. Magdy M. Abdelhameed

Arduino Prof. Dr. Magdy M. Abdelhameed Course Code: MDP 454, Course Name:, Second Semester 2014 Arduino What is Arduino? Microcontroller Platform Okay but what s a Microcontroller? Tiny, self-contained computers in an IC Often contain peripherals

More information

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. Today! Build HelloWorld yourself in BlueJ and Eclipse. Look at all the Java keywords. Primitive Types. HelloWorld in BlueJ 1. Find BlueJ in the start menu, but start the Select VM program instead (you

More information

Scope of this lecture. Repetition For loops While loops

Scope of this lecture. Repetition For loops While loops REPETITION CITS1001 2 Scope of this lecture Repetition For loops While loops Repetition Computers are good at repetition We have already seen the for each loop The for loop is a more general loop form

More information

Language Reference Manual

Language Reference Manual ALACS Language Reference Manual Manager: Gabriel Lopez (gal2129) Language Guru: Gabriel Kramer-Garcia (glk2110) System Architect: Candace Johnson (crj2121) Tester: Terence Jacobs (tj2316) Table of Contents

More information

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi... Contents 1 Introduction 3 1.1 Java, the beginning.......................... 3 1.2 Java Virtual Machine........................ 4 1.3 A First Program........................... 4 1.4 BlueJ.................................

More information

Java: Comment Text. Introduction. Concepts

Java: Comment Text. Introduction. Concepts Java: Comment Text Introduction Comment text is text included in source code that is ignored by the compiler and does not cause any machine-language object code to be generated. It is written into the

More information

Java Threads. COMP 585 Noteset #2 1

Java Threads. COMP 585 Noteset #2 1 Java Threads The topic of threads overlaps the boundary between software development and operation systems. Words like process, task, and thread may mean different things depending on the author and the

More information

CS 11 java track: lecture 1

CS 11 java track: lecture 1 CS 11 java track: lecture 1 Administrivia need a CS cluster account http://www.cs.caltech.edu/ cgi-bin/sysadmin/account_request.cgi need to know UNIX www.its.caltech.edu/its/facilities/labsclusters/ unix/unixtutorial.shtml

More information

Introduction to Java Threads

Introduction to Java Threads Object-Oriented Programming Introduction to Java Threads RIT CS 1 "Concurrent" Execution Here s what could happen when you run this Java program and launch 3 instances on a single CPU architecture. The

More information

PROGRAMMING FUNDAMENTALS

PROGRAMMING FUNDAMENTALS PROGRAMMING FUNDAMENTALS Q1. Name any two Object Oriented Programming languages? Q2. Why is java called a platform independent language? Q3. Elaborate the java Compilation process. Q4. Why do we write

More information

Lecture 02, Fall 2018 Friday September 7

Lecture 02, Fall 2018 Friday September 7 Anatomy of a class Oliver W. Layton CS231: Data Structures and Algorithms Lecture 02, Fall 2018 Friday September 7 Follow-up Python is also cross-platform. What s the advantage of Java? It s true: Python

More information

Example Program. public class ComputeArea {

Example Program. public class ComputeArea { COMMENTS While most people think of computer programs as a tool for telling computers what to do, programs are actually much more than that. Computer programs are written in human readable language for

More information

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char Review Primitive Data Types & Variables int, long float, double boolean char String Mathematical operators: + - * / % Comparison: < > = == 1 1.3 Conditionals and Loops Introduction to Programming in

More information

Java s Implementation of Concurrency, and how to use it in our applications.

Java s Implementation of Concurrency, and how to use it in our applications. Java s Implementation of Concurrency, and how to use it in our applications. 1 An application running on a single CPU often appears to perform many tasks at the same time. For example, a streaming audio/video

More information

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail. OOP in Java 1 Outline 1. Getting started, primitive data types and control structures 2. Classes and objects 3. Extending classes 4. Using some standard packages 5. OOP revisited Parts 1 to 3 introduce

More information

CS 351 Design of Large Programs Threads and Concurrency

CS 351 Design of Large Programs Threads and Concurrency CS 351 Design of Large Programs Threads and Concurrency Brooke Chenoweth University of New Mexico Spring 2018 Concurrency in Java Java has basic concurrency support built into the language. Also has high-level

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

APCS Semester #1 Final Exam Practice Problems

APCS Semester #1 Final Exam Practice Problems Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester

More information

Games Course, summer Introduction to Java. Frédéric Haziza

Games Course, summer Introduction to Java. Frédéric Haziza Games Course, summer 2005 Introduction to Java Frédéric Haziza (daz@it.uu.se) Summer 2005 1 Outline Where to get Java Compilation Notions of Type First Program Java Syntax Scope Class example Classpath

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. Data structures Collections of related data items. Discussed in depth in Chapters 16 21. Array objects Data

More information

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015 COMP-202: Foundations of Programming Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015 Announcements Check the calendar on the course webpage regularly for updates on tutorials and office hours.

More information

THE CONCEPT OF OBJECT

THE CONCEPT OF OBJECT THE CONCEPT OF OBJECT An object may be defined as a service center equipped with a visible part (interface) and an hidden part Operation A Operation B Operation C Service center Hidden part Visible part

More information

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others COMP-202 Recursion Recursion Recursive Definitions Run-time Stacks Recursive Programming Recursion vs. Iteration Indirect Recursion Lecture Outline 2 Recursive Definitions (1) A recursive definition is

More information

Info 408 Distributed Applications Programming Exercise sheet nb. 4

Info 408 Distributed Applications Programming Exercise sheet nb. 4 Lebanese University Info 408 Faculty of Science 2017-2018 Section I 1 Custom Connections Info 408 Distributed Applications Programming Exercise sheet nb. 4 When accessing a server represented by an RMI

More information

Expanded Guidelines on Programming Style and Documentation

Expanded Guidelines on Programming Style and Documentation Page 1 of 5 Expanded Guidelines on Programming Style and Documentation Introduction Introduction to Java Programming, 5E Y. Daniel Liang liang@armstrong.edu Programming style deals with the appearance

More information

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java Chapter 3 Syntax, Errors, and Debugging Objectives Construct and use numeric and string literals. Name and use variables and constants. Create arithmetic expressions. Understand the precedence of different

More information

Mr. Monroe s Guide to Mastering Java Syntax

Mr. Monroe s Guide to Mastering Java Syntax Mr. Monroe s Guide to Mastering Java Syntax Getting Started with Java 1. Download and install the official JDK (Java Development Kit). 2. Download an IDE (Integrated Development Environment), like BlueJ.

More information

Getting started with Java

Getting started with Java Getting started with Java by Vlad Costel Ungureanu for Learn Stuff Programming Languages A programming language is a formal constructed language designed to communicate instructions to a machine, particularly

More information

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings 19/10/2017 CE221 Part 2 1 Variables and References 1 In Java a variable of primitive type is associated with a memory location

More information

Assignment Marking Criteria

Assignment Marking Criteria Assignment Marking Criteria Analysis Your analysis documentation must meet the following criteria: All program inputs, processing, and outputs. Each input and output must be given a name and description

More information

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors Outline Overview history and advantage how to: program, compile and execute 8 data types 3 types of errors Control statements Selection and repetition statements Classes and methods methods... 2 Oak A

More information

A First Look At Java. Didactic Module 13 Programming Languages - EEL670 1

A First Look At Java. Didactic Module 13 Programming Languages - EEL670 1 A First Look At Java Didactic Module 13 Programming Languages - EEL670 1 Outline Thinking about objects Simple expressions and statements Class definitions About references and pointers Getting started

More information

Math Modeling in Java: An S-I Compartment Model

Math Modeling in Java: An S-I Compartment Model 1 Math Modeling in Java: An S-I Compartment Model Basic Concepts What is a compartment model? A compartment model is one in which a population is modeled by treating its members as if they are separated

More information

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

More information

CITS1001 week 4 Grouping objects

CITS1001 week 4 Grouping objects CITS1001 week 4 Grouping objects Arran Stewart March 20, 2018 1 / 31 Overview In this lecture, we look at how can group objects together into collections. Main concepts: The ArrayList collection Processing

More information

This is a tutorial about sensing the environment using a Raspberry Pi. measure a digital input (from a button) output a digital signal (to an LED)

This is a tutorial about sensing the environment using a Raspberry Pi. measure a digital input (from a button) output a digital signal (to an LED) Practical 2 - Overview This is a tutorial about sensing the environment using a Raspberry Pi and a GrovePi+. You will learn: digital input and output measure a digital input (from a button) output a digital

More information

Lec 3. Compilers, Debugging, Hello World, and Variables

Lec 3. Compilers, Debugging, Hello World, and Variables Lec 3 Compilers, Debugging, Hello World, and Variables Announcements First book reading due tonight at midnight Complete 80% of all activities to get 100% HW1 due Saturday at midnight Lab hours posted

More information

Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07

Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07 Informatica 3 Marcello Restelli 9/15/07 10/29/07 Laurea in Ingegneria Informatica Politecnico di Milano Structuring the Computation Control flow can be obtained through control structure at instruction

More information

OBJECT INTERACTION. CITS1001 week 3

OBJECT INTERACTION. CITS1001 week 3 OBJECT INTERACTION CITS1001 week 3 2 Fundamental concepts Coupling and Cohesion Internal/external method calls null objects Chaining method calls Class constants Class variables Reading: Chapter 3 of Objects

More information

School of Informatics, University of Edinburgh

School of Informatics, University of Edinburgh CS1Ah Lecture Note 17 Case Study: Using Arrays This note has three main aims: 1. To illustrate the use of arrays introduced in Lecture Note 16, on a slightly larger example. 2. To introduce the idea of

More information

A First Parallel Program

A First Parallel Program A First Parallel Program Chapter 4 Primality Testing A simple computation that will take a long time. Whether a number x is prime: Decide whether a number x is prime using the trial division algorithm.

More information

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Basic Operations jgrasp debugger Writing Programs & Checkstyle Basic Operations jgrasp debugger Writing Programs & Checkstyle Suppose you wanted to write a computer game to play "Rock, Paper, Scissors". How many combinations are there? Is there a tricky way to represent

More information

Java Bytecode (binary file)

Java Bytecode (binary file) Java is Compiled Unlike Python, which is an interpreted langauge, Java code is compiled. In Java, a compiler reads in a Java source file (the code that we write), and it translates that code into bytecode.

More information

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ AP Computer Science Unit 1. Writing Programs Using BlueJ 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

More information

ECGR 4101/5101, Fall 2016: Lab 1 First Embedded Systems Project Learning Objectives:

ECGR 4101/5101, Fall 2016: Lab 1 First Embedded Systems Project Learning Objectives: ECGR 4101/5101, Fall 2016: Lab 1 First Embedded Systems Project Learning Objectives: This lab will introduce basic embedded systems programming concepts by familiarizing the user with an embedded programming

More information

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003 Outline Object Oriented Programming Autumn 2003 2 Course goals Software design vs hacking Abstractions vs language (syntax) Java used to illustrate concepts NOT a course about Java Prerequisites knowledge

More information

1) Discuss the mutual exclusion mechanism that you choose as implemented in the chosen language and the associated basic syntax

1) Discuss the mutual exclusion mechanism that you choose as implemented in the chosen language and the associated basic syntax Lab report Project 3 Mihai Ene I have implemented the solution in Java. I have leveraged its threading mechanisms and concurrent API (i.e. concurrent package) in order to achieve the required functionality

More information

CS112 Lecture: Working with Numbers

CS112 Lecture: Working with Numbers CS112 Lecture: Working with Numbers Last revised January 30, 2008 Objectives: 1. To introduce arithmetic operators and expressions 2. To expand on accessor methods 3. To expand on variables, declarations

More information

Java+- Language Reference Manual

Java+- Language Reference Manual Fall 2016 COMS4115 Programming Languages & Translators Java+- Language Reference Manual Authors Ashley Daguanno (ad3079) - Manager Anna Wen (aw2802) - Tester Tin Nilar Hlaing (th2520) - Systems Architect

More information

Grouping objects. Main concepts to be covered

Grouping objects. Main concepts to be covered Grouping objects Collections and iterators 3.0 Main concepts to be covered Collections Loops Iterators Arrays Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling

More information

Threads and Locks. CSCI 5828: Foundations of Software Engineering Lecture 09 09/22/2015

Threads and Locks. CSCI 5828: Foundations of Software Engineering Lecture 09 09/22/2015 Threads and Locks CSCI 5828: Foundations of Software Engineering Lecture 09 09/22/2015 1 Goals Cover the material presented in Chapter 2, Day 1 of our concurrency textbook Creating threads Locks Memory

More information

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview Introduction to Visual Basic and Visual C++ Introduction to Java Lesson 13 Overview I154-1-A A @ Peter Lo 2010 1 I154-1-A A @ Peter Lo 2010 2 Overview JDK Editions Before you can write and run the simple

More information

CST242 Concurrency Page 1

CST242 Concurrency Page 1 CST242 Concurrency Page 1 1 2 3 4 5 6 7 9 Concurrency CST242 Concurrent Processing (Page 1) Only computers with multiple processors can truly execute multiple instructions concurrently On single-processor

More information

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them. Dec. 9 Loops Please answer the following questions. Do not re-code the enclosed codes if you have already completed them. What is a loop? What are the three loops in Java? What do control structures do?

More information

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal COMSC-051 Java Programming Part 1 Part-Time Instructor: Joenil Mistal Chapter 5 5 Controlling the Flow of Your Program Control structures allow a programmer to define how and when certain statements will

More information

Controls Structure for Repetition

Controls Structure for Repetition Controls Structure for Repetition So far we have looked at the if statement, a control structure that allows us to execute different pieces of code based on certain conditions. However, the true power

More information

CS 160: Interactive Programming

CS 160: Interactive Programming CS 160: Interactive Programming Professor John Canny 3/8/2006 1 Outline Callbacks and Delegates Multi-threaded programming Model-view controller 3/8/2006 2 Callbacks Your code Myclass data method1 method2

More information

Introduction to Computer Science Unit 2. Notes

Introduction to Computer Science Unit 2. Notes Introduction to Computer Science Unit 2. Notes Name: Objectives: By the completion of this packet, students should be able to describe the difference between.java and.class files and the JVM. create and

More information

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet. Mr G s Java Jive #2: Yo! Our First Program With this handout you ll write your first program, which we ll call Yo. Programs, Classes, and Objects, Oh My! People regularly refer to Java as a language that

More information

CS 177 Recitation. Week 1 Intro to Java

CS 177 Recitation. Week 1 Intro to Java CS 177 Recitation Week 1 Intro to Java Questions? Computers Computers can do really complex stuff. How? By manipulating data according to lists of instructions. Fundamentally, this is all that a computer

More information

Unit 7. 'while' Loops

Unit 7. 'while' Loops 1 Unit 7 'while' Loops 2 Control Structures We need ways of making decisions in our program To repeat code until we want it to stop To only execute certain code if a condition is true To execute one segment

More information

CS1114: Matlab Introduction

CS1114: Matlab Introduction CS1114: Matlab Introduction 1 Introduction The purpose of this introduction is to provide you a brief introduction to the features of Matlab that will be most relevant to your work in this course. Even

More information

The Nervous Shapes Example

The Nervous Shapes Example The Nervous Shapes Example This Example is taken from Dr. King s Java book 1 11.6 Abstract Classes Some classes are purely artificial, created solely so that subclasses can take advantage of inheritance.

More information

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to: Get JAVA To compile programs you need the JDK (Java Development Kit). To RUN programs you need the JRE (Java Runtime Environment). This download will get BOTH of them, so that you will be able to both

More information

SUBCLASSES IN JAVA - II

SUBCLASSES IN JAVA - II SUBCLASSES IN JAVA - II Subclasses and variables Any instance of a class A can also be treated as an instance of a superclass of A. Thus, if B is a superclass of A, then every A object can also be treated

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

Lab 1 The Alarm Clock (Threads and Semaphores)

Lab 1 The Alarm Clock (Threads and Semaphores) Exercises and Labs Lab 1 The Alarm Clock (Threads and Semaphores) Exercise session - preparation In this exercise you will design a real-time system for an alarm clock application using threads, semaphores

More information

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks) M257 MTA Spring2010 Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks) 1. If we need various objects that are similar in structure, but

More information

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit Threads Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multitasking Thread-based multitasking Multitasking

More information

Assignment 3: Optimizing solutions

Assignment 3: Optimizing solutions Assignment 3: Optimizing solutions Algorithmic Thinking and Structured Programming (in Greenfoot) c 2015 Renske Smetsers-Weeda & Sjaak Smetsers Licensed under the Creative Commons Attribution 4.0 license,

More information

The first program: Little Crab

The first program: Little Crab Chapter 2 The first program: Little Crab topics: concepts: writing code: movement, turning, reacting to the screen edges source code, method call, parameter, sequence, if-statement In the previous chapter,

More information

Object Oriented Programming in C#

Object Oriented Programming in C# Introduction to Object Oriented Programming in C# Class and Object 1 You will be able to: Objectives 1. Write a simple class definition in C#. 2. Control access to the methods and data in a class. 3. Create

More information

Chapter 10 Recursion

Chapter 10 Recursion Chapter 10 Recursion Written by Dr. Mark Snyder [minor edits for this semester by Dr. Kinga Dobolyi] Recursion implies that something is defined in terms of itself. We will see in detail how code can be

More information

Introduction Basic elements of Java

Introduction Basic elements of Java Software and Programming I Introduction Basic elements of Java Roman Kontchakov / Carsten Fuhs Birkbeck, University of London Module Information Time: Thursdays in the Spring term Lectures: MAL B04: 2

More information

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos. Lecture 05: Methods AITI Nigeria Summer 2012 University of Lagos. Agenda What a method is Why we use methods How to declare a method The four parts of a method How to use (invoke) a method The purpose

More information

Principles of Software Construction: Concurrency, Part 2

Principles of Software Construction: Concurrency, Part 2 Principles of Software Construction: Concurrency, Part 2 Josh Bloch Charlie Garrod School of Computer Science 1 Administrivia Homework 5a due now Homework 5 framework goals: Functionally correct Well documented

More information

Chapter 2: Programming Concepts

Chapter 2: Programming Concepts Chapter 2: Programming Concepts Objectives Students should Know the steps required to create programs using a programming language and related terminology. Be familiar with the basic structure of a Java

More information

Hands-On Workshop: ARM mbed

Hands-On Workshop: ARM mbed Hands-On Workshop: ARM mbed FTF-DES-F1302 Sam Grove - ARM Michael Norman Freescale J U N. 2 0 1 5 External Use Agenda What is mbed mbed Hardware mbed Software mbed Tools mbed Support and Community Hands-On

More information

Prototyping & Engineering Electronics Kits Basic Kit Guide

Prototyping & Engineering Electronics Kits Basic Kit Guide Prototyping & Engineering Electronics Kits Basic Kit Guide odysseyboard.com Please refer to www.odysseyboard.com for a PDF updated version of this guide. Guide version 1.0, February, 2018. Copyright Odyssey

More information

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University Lecture 2 COMP1406/1006 (the Java course) Fall 2013 M. Jason Hinek Carleton University today s agenda a quick look back (last Thursday) assignment 0 is posted and is due this Friday at 2pm Java compiling

More information

Improving the Crab more sophisticated programming

Improving the Crab more sophisticated programming CHAPTER 3 Improving the Crab more sophisticated programming topics: concepts: random behavior, keyboard control, sound dot notation, random numbers, defining methods, comments In the previous chapter,

More information

Java Review Outline. basics exceptions variables arrays modulo operator if statements, booleans, comparisons loops: while and for

Java Review Outline. basics exceptions variables arrays modulo operator if statements, booleans, comparisons loops: while and for Java Review Outline basics exceptions variables arrays modulo operator if statements, booleans, comparisons loops: while and for Java basics write a simple program, e.g. hello world http://www2.hawaii.edu/~esb/2017fall.ics211/helloworl

More information

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016 COMP-202: Foundations of Programming Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016 Review What is the difference between a while loop and an if statement? What is an off-by-one

More information

Program #3 - Airport Simulation

Program #3 - Airport Simulation CSCI212 Program #3 - Airport Simulation Write a simulation for a small airport that has one runway. There will be a queue of planes waiting to land and a queue of planes waiting to take off. Only one plane

More information

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and Classes CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

Ticket Machine Project(s)

Ticket Machine Project(s) Ticket Machine Project(s) Understanding the basic contents of classes Produced by: Dr. Siobhán Drohan (based on Chapter 2, Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes,

More information

CS 251 Intermediate Programming Methods and More

CS 251 Intermediate Programming Methods and More CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

Computer Programming, I. Laboratory Manual. Final Exam Solution

Computer Programming, I. Laboratory Manual. Final Exam Solution Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Final Exam Solution

More information

BSc ( Hons) Computer Science with Network Security. Examinations for / Semester 2

BSc ( Hons) Computer Science with Network Security. Examinations for / Semester 2 BSc ( Hons) Computer Science with Network Security Cohort: BCNS/16B/FT Examinations for 2017 2018 / Semester 2 Resit Examinations for BCNS/15B/FT & BCNS/16A/FT MODULE: NETWORK PROGRAMMING MODULE CODE:

More information

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade; Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The

More information

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [http://www.programcreek.com/2009/03/thread-status/]

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [http://www.programcreek.com/2009/03/thread-status/] COMPSCI 230 Threading Week8 Figure 1 Thread status diagram [http://www.programcreek.com/2009/03/thread-status/] Synchronization Lock DeadLock Why do we need Synchronization in Java? If your code is executing

More information

Software and Programming 1

Software and Programming 1 Software and Programming 1 Lab 1: Introduction, HelloWorld Program and use of the Debugger 17 January 2019 SP1-Lab1-2018-19.pptx Tobi Brodie (tobi@dcs.bbk.ac.uk) 1 Module Information Lectures: Afternoon

More information

Threads Chate Patanothai

Threads Chate Patanothai Threads Chate Patanothai Objectives Knowing thread: 3W1H Create separate threads Control the execution of a thread Communicate between threads Protect shared data C. Patanothai Threads 2 What are threads?

More information

ICOM 4015: Advanced Programming

ICOM 4015: Advanced Programming ICOM 4015: Advanced Programming Lecture 1 Reading: Chapter One: Introduction Chapter 1 Introduction Chapter Goals To understand the activity of programming To learn about the architecture of computers

More information

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1 Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1 Motivation In the programs we have written thus far, statements are executed one after the other, in the order in which they appear. Programs often

More information

Programming in the Simple Raster Graphics Package (SRGP)

Programming in the Simple Raster Graphics Package (SRGP) Programming in the Simple Raster Graphics Package (SRGP) Chapter 2 This chapter focuses on a graphics package called SRGP. SRGP was written by the authors to demonstrate some of the basics of Raster Graphics

More information