Boe-Bot Platform
ESE112
Introduction to Java Programming
with Boe-Bot Platform
The “brain”: Javelin Stamp Javelin Stamp Integrated Development Environment
(JSIDE)
Programmable Microcontroller
Software that edits, compiles, and download program to
32K of non-volatile EEPROM Boe-Bot board
Electrically Erasable Programmable Read-Only Runs on Microsoft Windows
Memory N d a serial
Need i l port/USB
t/USB version
i off board
b d
To store program instructions Is not a very good syntax highlighter
Can be written 1 million times Better IDE just for editing is Dr Java
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
ESE112 2 ESE112 3
1
Java Compiler and Virtual Machine
Java Program Structure
The Java Compiler
Consist of one of more file ending .java
Reads file with extension .java
Each file has the following structure Checks syntax / grammar
public class Classname { Creates a .class file which
… contains
t i byte(
b t ( or machine)
hi ) code
d
independent of any machine
}
Filename and Classname must exactly match
JVM(Java Virtual Machine)
The curly braces { } define start and end of class Translates byte code in to instructions
(actual machine code) for a particular
description processor
Syntax error if the brace pair is missing Th actuall machine
The hi coded then
h iis
executed on the computer
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 ESE112 5
Process of Programming Special Syntax
Programming Cycle Within any one class description if we have the special
syntax
Edit public
bli static
t ti void
id main
i () {
(Syntax + Semantics) statement(s)
Compile-time
Run-time }
or or Syntax Error
Compile
Semantic This known as a program’s (computational solution)
Error entry point i.e. where it starts getting executed
Run
Called
C ll d th
the main
i method
th d
(observe your output) 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 []
Philosophy: program in increments args) {... }
ESE112 6 ESE112 7
2
Example Hello.java Java Syntax
public class Hello{ Comments
public static void main(){ Literals
// A statement
t t t that
th t prints
i t to
t output
t t screen D t type
Data t
System.out.println("Hello World"); Variables
Operators
}// end of main Expressions
} String
g and Printing
g
ESE112 8 ESE112 9
Comments Literals
Literals are the values we write in a
Comments are used to make code more
understandable to humans conventional form whose value is obvious
Java Compiler ignores comments 3 // An integer has no decimal point
// this is a single line comment ‘a’ // a character has single quotes
/* this is true // The boolean literals are of two types: true, false
* a multi-line
“hello
hello world”
world // A string literal
* comment
*/
ESE112 10 ESE112 11
3
Arithmetic Operators Relational Operators
+ to indicate addition == equal to
- to indicate subtraction != not equal to
* to indicate multiplication < less than
/ to indicate division > greater than
% to indicate remainder of a division (integers <= less than equal to
only) >= greater than equal to
parentheses ( ) to indicate the order in which to
do things Note: Arithmetic comparisons result in a
Boolean value of true or false
ESE112 12 ESE112 13
Boolean or Logical Operators Expression
Like In English - conditional statements formed An expression is combination of literals and operators
using "and", "or", and "not" An expression has a value
In Java 3 ->
> 3
|| -> OR operator
¾ true if either operand* is true 3 + 5 -> 8
&& -> AND operator
¾ true only if both operands are true ‘a’ == ‘A’ -> false // == Equality operator
! -> NOT operator
true && false -> false //using the logical AND
¾Is a unary operator – applied to only one operand
¾ Reverses the truth value of its operand
Later we’ll see that an expression may contain other things
* Operand: a quantity upon which a operation is
Such as variables, method calls …
performed
ESE112 14 ESE112 15
4
Value & Type Operator Precedence
Value: Piece of data
23, true, ‘a’
Type: Kind of data
integer, boolean (true/false), character
Expression Value Type
23 23 integer
3+5*6 33 integer
g
(3 * 4)/15 ? ? integer division truncates
true && false ? ?
Note: With Boe-Bot platform
there is no support of floating Source: Javelin Reference Manual
ESE112 point numbers 16 ESE112 17
Types: a very important concept! Primitive types
All data values in Java have a type Values that Java knows how to operate on directly
Primitives with Javelin stamp:
The type of a value determines: int – Integer (also short and byte for less representation)
How the value is stored in computer’s memory -1 42
Max/min value that data type can take on char - Character
What operations make sense for the value 'J' '*'
How the value can be converted (cast) to related boolean - Truth value
values true false
Note: Types are very helpful in catching Regular Java has other types
programming errors double, long, float
ESE112 18 ESE112 19
5
Storage Space for Numeric Type Variables
Numeric types in Java are characterized by their size: A variable is a name associated with a value
how much you can store ? – computers have finite memory
Value is stored in computer’s memory
Integer
g and Character types
yp Instead of knowingg the location,, we access the value
by the name it is associated with
Type Value Range
0 : 255
char (8 bits) Note: Each char is assigned a unique numeric value & numeric Variable must always be associated with type
value is stored (ASCII code)
It tells the computer how much space to reserve for
int (16 bits) -32768 : 32767
short(16bits)
the variable
byte(8 bit) -128 : 127 The value stored can vary over time
ESE112 20 ESE112 21
Identifiers Identifiers (contd..)
Identifiers are names that you as a coder make up Style Rule for Variable names
Variable names Should be a noun that starts with an lowercase letter
Also class and method names – more later! ¾ E.g. sum, average
If the name has multiple words, capitalize the start of
Java Rule for Variable names every word except the first (style rule)
May consist of alphanumeric characters and the ¾ E.g. firstName, lastName
underscore (_)
Cannot start with a number Note: Style rule are for consistency and
Cannot use keywords such as int int, double etc
etc. readability of programs
If the rules are not followed then compiler will complain Compiler will not complain if the rule is not followed
(syntax error) If you do not follow the rule you get penalized in
grading!
ESE112 22 ESE112 23
6
Declaring variables Storing value into Variables
All variables must be declared before being used To store values into variable we use the assignment
operator i.e. “=“
Done with a declaration statement
Variable = Expression; -> assignment statement
<type> <identifie>;
Right hand side value is assigned to left hand side
Declaration statement
Specifies the type of the variable, followed by Important
descriptive variable name, followed by semicolon(;) Assignment statement must end with a semicolon(;)
When a variable is assigned a value, the old value is
Examples:
p discarded and totally forgotten
int seats;
boolean isFriday; Examples
char initial; seats = 150;
isFriday = true;
ESE112 24 ESE112 25
Variable value and type Initializing Variables
Assume variable x is an integer It’s good idea to declare and initialize a variable in one
statement
The value of a variable may
y be changed
g
double milesPerHour = 60.5; //No support in Javelin
x = 57;
boolean isTall = true;
int age = 17;
However its type may not
x = true; // this causes an syntax error, Note:
// i.e.compiler will complain If a variable is not initialized before using it, you may or
may not get a compiler error
Caveat This will depend where in the program your variable is declared
¾ You can assign do x = ‘c’ – Why? More on this later
¾ However when you view the value of x, a numeric value is
printed
ESE112 26 ESE112 27
7
Constants Sequential Instructions/Programming
Variables that don’t change Computer executes statements in the order the
Initialize a value and never change it statements are written
Program’s computation might be affected if a variable is Example:
not consistent throughout
int time = 123; //The time, in seconds
Rules
Java Rule: Must have the keyword final before the type /* Convert time into hours, minutes, seconds*/
Style Rule: Should have all caps for variable name int hours = time / 3600; // 3600 seconds in an hour
¾ If multiple words use underscore between words int minutes = (time % 3600) / 60; // 60 seconds in a minute
int seconds = ((time % 3600) % 60); // remainder is seconds
final double PI = 3.14; //No support in javelin
final int MILES_PER_GALLON = 32;
ESE112 28 ESE112 29
Another Type: String System.out.println(String)
A String is an Object, not a primitive type Command that prints string to the output screen
Java also has objects - cover objects later Can also print literals, and expression values
The answer is automatically converted to string
String is composed of zero or more chars
Prints every time on a new line
A String is a sequence of characters enclosed Useful in finding semantic errors in a program
by double quotes
"Java" "3 Stooges" "富士山“ System.out.println(“hello world”);
System.out.println(5)
+ means concatenation for strings System out println(“x = “ + x);
System.out.println(“x
"3" + " " + "Stooges" ⇒ “3 Stooges”
To not print on new newline use:
Automatic conversion of numbers to strings
System.out.print(String)
3 + " " + "Stooges" ⇒ “3 Stooges”
ESE112 30 ESE112 31
8
Memory and Strings Putting it all together
Due to on board memory limitations public class TimeConversion {
public static void main() {
int time = 2000; /* The time, in seconds */
D nott use a lot
Do l t off concatenation
t ti (+) operations
ti /* C
Convertt ti
time iinto
t hhours, minutes,
i t seconds
d */
int hours = time / 3600; // 3600 seconds in an hour
int minutes = (time % 3600) / 60; // 60 seconds in a minute
To declare to work with string data on Boe-Bot int seconds = ((time % 3600) % 60); // remainder is seconds
use StringBuffer instead of String object – more
on this later /* Output results */
System out println("Time
System.out.println( Time ::" + hours+ "h
h " + minutes + "m
m"+
seconds + "s ");
}//end of main
}//end of TimeConversion class
ESE112 32 ESE112 33