Introduction To Java Programming With Boe-Bot Platform
Introduction To Java Programming With Boe-Bot Platform
ESE112
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
}
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 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
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’
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
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
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
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