ESE112. The brain : Javelin Stamp. Boe-Bot Platform. Introduction to Java Programming with Boe-Bot Platform

Similar documents
Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Chapter 2: Elements of Java

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

CS 106 Introduction to Computer Science I

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Variables, Constants, and Data Types

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Chapter 2 Introduction to Java programming

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Java Basics: Data Types, Variables, and Loops

Moving from CS 61A Scheme to CS 61B Java

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Chapter One Introduction to Programming

Informatica e Sistemi in Tempo Reale

Chapter 1 Java Program Design and Development

Java Crash Course Part I

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Introduction to Java

Programming in Java Course Technology, a part of Cengage Learning.

Sources: On the Web: Slides will be available on:

Introduction to Java. CS 3: Computer Programming in Java

Object Oriented Software Design

C++ Language Tutorial

Number Representation

Object Oriented Software Design

VB.NET Programming Fundamentals

Introduction to Python

Pemrograman Dasar. Basic Elements Of Java

J a v a Quiz (Unit 3, Test 0 Practice)

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Java Interview Questions and Answers

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Example of a Java program

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

Programming Languages CIS 443

Computer Programming Tutorial

JavaScript: Control Statements I

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

The programming language C. sws1 1

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Introduction to Visual C++.NET Programming. Using.NET Environment

Programming and Data Structures with Java and JUnit. Rick Mercer

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

CSC4510 AUTOMATA 2.1 Finite Automata: Examples and D efinitions Definitions

Chapter 2 Elementary Programming

Lecture 5: Java Fundamentals III

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Install Java Development Kit (JDK) 1.8

Java CPD (I) Frans Coenen Department of Computer Science

13 File Output and Input

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Lecture 1 Introduction to Java

Chapter 3. Input and output. 3.1 The System class

Crash Course in Java

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Computer Programming I & II*

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

Semantic Analysis: Types and Type Checking

X86-64 Architecture Guide

arrays C Programming Language - Arrays

Chapter 7D The Java Virtual Machine

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Computer Programming I

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Java Programming Fundamentals

Object-Oriented Programming in Java

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

PL / SQL Basics. Chapter 3

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

#820 Computer Programming 1A

Beyond the Mouse A Short Course on Programming

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Simple Java Applications

1001ICT Introduction To Programming Lecture Notes

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

6.170 Tutorial 3 - Ruby Basics

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

ECE 122. Engineering Problem Solving with Java

Introduction to Python

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Using Files as Input/Output in Java 5.0 Applications

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

A Grammar for the C- Programming Language (Version S16) March 12, 2016

Outline Basic concepts of Python language

Introduction to Object-Oriented Programming

C++ INTERVIEW QUESTIONS

Computers. Hardware. The Central Processing Unit (CPU) CMPT 125: Lecture 1: Understanding the Computer

An Introduction to Computer Science

AP Computer Science Java Subset

Introduction to Java Lecture Notes. Ryan Dougherty

Memory is implemented as an array of electronic switches

SOME EXCEL FORMULAS AND FUNCTIONS

Transcription:

Boe-Bot Platform ESE112 Introduction to Java Programming with Boe-Bot Platform The brain : Javelin Stamp Programmable Microcontroller 32K of non-volatile EEPROM Electrically Erasable Programmable Read-Only Memory To store program instructions Can be written 1 million times 32K of RAM To store data during program execution 16 Input/Output (I/O) pins: To control motors/sensors Interprets a subset of Java Javelin Stamp Integrated Development Environment (JSIDE) Software that edits, compiles, and download program to Boe-Bot board Runs on Microsoft Windows Need a serial port/usb version of board Is not a very good syntax highlighter Better IDE just for editing is Dr Java Javelin Stamp ESE112 2 ESE112 3 1

Java Program Structure Consist of one of more file ending.java Each file has the following structure public class Classname { } Filename and Classname must exactly match The curly braces { } define start and end of class description Syntax error if the brace pair is missing Classname Must start with alphabet Java Rule The first letter must capital Style Rule Can be made up of alphanumeric characters and underscore ESE112 4 Java Compiler and Virtual Machine The Java Compiler Reads file with extension.java Checks syntax / grammar Creates a.class file which contains byte( ( or machine) code independent of any machine JVM(Java Virtual Machine) Translates byte code in to instructions (actual machine code) for a particular processor The actual machine code then is executed on the computer ESE112 5 Process of Programming Special Syntax Programming Cycle Run-time or Semantic Error Edit (Syntax + Semantics) Compile Run (observe your output) Philosophy: program in increments Compile-time or Syntax Error Within any one class description if we have the special syntax public static ti void main () { statement(s) } This known as a program s (computational solution) entry point i.e. where it starts getting executed Called the main method A method is a named group of statements Difference: In a regular Java program An additional argument is used i.e. public static void main (String [] args) {... } ESE112 6 ESE112 7 2

Example Hello.java public class Hello{ public static void main(){ // A statement t t that t prints to output t screen System.out.println("Hello World"); }// end of main } Comments Literals Data type Variables Operators Expressions String and Printing Java Syntax ESE112 8 ESE112 9 Comments Comments are used to make code more understandable to humans Java Compiler ignores comments // this is a single line comment /* this is * a multi-line * comment */ Literals Literals are the values we write in a conventional form whose value is obvious 3 //An integer has no decimal point a // a character has single quotes true // The boolean literals are of two types: true, false hello world // A string literal ESE112 10 ESE112 11 3

Arithmetic Operators + to indicate addition - to indicate subtraction * to indicate multiplication / to indicate division % to indicate remainder of a division (integers only) parentheses ( ) to indicate the order in which to do things Relational Operators == equal to!= not equal to < less than > greater than <= less than equal to >= greater than equal to Note: Arithmetic comparisons result in a Boolean value of true or false ESE112 12 ESE112 13 Boolean or Logical Operators Like In English - conditional statements formed using "and", "or", and "not" In Java -> OR operator true if either operand* is true && -> AND operator true only if both operands are true! -> NOT operator Is a unary operator applied to only one operand Reverses the truth value of its operand * Operand: a quantity upon which a operation is performed Expression An expression is combination of literals and operators An expression has a value 3 -> 3 3 + 5 -> 8 a == A -> false // == Equality operator true && false -> false //using the logical AND Later we ll see that an expression may contain other things Such as variables, method calls ESE112 14 ESE112 15 4

Value: Piece of data 23, true, a Value & Type Type: Kind of data integer, boolean (true/false), character Expression Value Type 23 23 integer 3 + 5 * 6 33 integer (3 * 4)/15?? true && false?? Note: With Boe-Bot platform there is no support of floating point numbers integer division truncates ESE112 16 Operator Precedence Source: Javelin Reference Manual ESE112 17 Types: a very important concept! All data values in Java have a type The type of a value determines: How the value is stored in computer s memory Max/min value that data type can take on What operations make sense for the value How the value can be converted (cast) to related values Note: Types are very helpful in catching programming errors Primitive types Values that Java knows how to operate on directly Primitives with Javelin stamp: int Integer (also short and byte for less representation) -1 42 char - Character 'J' '*' boolean - Truth value true false Regular Java has other types double, long, float ESE112 18 ESE112 19 5

Type Storage Space for Numeric Type Numeric types in Java are characterized by their size: how much you can store? computers have finite memory Integer and Character types char (8 bits) int (16 bits) short(16bits) Value Range 0 : 255 Note: Each char is assigned a unique numeric value & numeric value is stored (ASCII code) -32768 : 32767 byte(8 bit) -128 : 127 Variables A variable is a name associated with a value Value is stored in computer s memory Instead of knowing the location, we access the value by the name it is associated with Variable must always be associated with type It tells the computer how much space to reserve for the variable The value stored can vary over time ESE112 20 ESE112 21 Identifiers Identifiers are names that you as a coder make up Variable names Also class and method names more later! Java Rule for Variable names May consist of alphanumeric characters and the underscore (_) Cannot start with a number Cannot use keywords such as int, double etc. If the rules are not followed then compiler will complain (syntax error) Identifiers (contd..) Style Rule for Variable names Should be a noun that starts with an lowercase letter E.g. sum, average If the name has multiple words, capitalize the start of every word except the first (style rule) E.g. firstname, lastname Note: Style rule are for consistency and readability of programs Compiler will not complain if the rule is not followed If you do not follow the rule you get penalized in grading! ESE112 22 ESE112 23 6

Declaring variables All variables must be declared before being used Done with a declaration statement <type> <identifie>; Declaration statement Specifies the type of the variable, followed by descriptive variable name, followed by semicolon(;) Examples: int seats; boolean isfriday; char initial; Storing value into Variables To store values into variable we use the assignment operator i.e. = Variable = Expression; -> assignment statement Right hand side value is assigned to left hand side Important Assignment statement must end with a semicolon(;) When a variable is assigned a value, the old value is discarded and totally forgotten Examples seats = 150; isfriday = true; ESE112 24 ESE112 25 Variable value and type Assume variable x is an integer The value of a variable may be changed x = 57; However its type may not x = true; // this causes an syntax error, // i.e.compiler will complain Caveat You can assign do x = c Why? However when you view the value of x, a numeric value is printed Initializing Variables It s good idea to declare and initialize a variable in one statement double milesperhour = 60.5; //No support in Javelin boolean istall = true; int age = 17; Note: If a variable is not initialized before using it, you may or may not get a compiler error This will depend where in the program your variable is declared More on this later ESE112 26 ESE112 27 7

Constants Variables that don t change Initialize a value and never change it Program s computation might be affected if a variable is not consistent throughout Rules Java Rule: Must have the keyword final before the type Style Rule: Should have all caps for variable name If multiple words use underscore between words final double PI = 3.14; //No support in javelin final int MILES_PER_GALLON = 32; Sequential Instructions/Programming Computer executes statements in the order the statements are written Example: int time = 123; //The time, in seconds /* Convert time into hours, minutes, seconds*/ int hours = time / 3600; // 3600 seconds in an hour int minutes = (time % 3600) / 60; // 60 seconds in a minute int seconds = ((time % 3600) % 60); // remainder is seconds ESE112 28 ESE112 29 Another Type: String A String is an Object, not a primitive type Java also has objects - cover objects later String is composed of zero or more chars A String is a sequence of characters enclosed by double quotes "Java" "3 Stooges" " 富士山 + means concatenation for strings "3" + " " + "Stooges" 3 Stooges Automatic conversion of numbers to strings 3 + " " + "Stooges" 3 Stooges ESE112 30 System.out.println(String) Command that prints string to the output screen Can also print literals, and expression values The answer is automatically converted to string Prints every time on a new line Useful in finding semantic errors in a program System.out.println( hello world ); System.out.println(5) System.out.println( x = + x); To not print on new newline use: System.out.print(String) ESE112 31 8

Memory and Strings Due to on board memory limitations Do not use a lot of concatenation ti (+) operations To declare to work with string data on Boe-Bot use StringBuffer instead of String object more on this later Putting it all together public class TimeConversion { public static void main() { int time = 2000; /* The time, in seconds */ /* Convert time into hours, minutes, seconds */ int hours = time / 3600; // 3600 seconds in an hour int minutes = (time % 3600) / 60; // 60 seconds in a minute int seconds = ((time % 3600) % 60); // remainder is seconds /* Output results */ System.out.println( println("time :" + hours+ "h " + minutes + "m " + seconds + "s "); }//end of main }//end of TimeConversion class ESE112 32 ESE112 33 9