Transcript lect04

Today's Topics




Java
Robocode
leJOS
Reading
 Bagnall, Chapters 1-3
Mindstorms
4.1
Java!



Java is a buzzword-enabled language
From Sun (the developers of Java),
“Java is a simple, object-oriented, distributed, interpreted,
robust, secure, architecture-neutral, portable, high
performance, multi-threaded, and dynamic language.”
What do all of those terms mean?
Mindstorms
4.2
Definitions






Algorithm: ordered set of unambiguous executable steps, defining a
terminating process
Program: instructions executed by a computer
Applet: Java program that is executed in a program such as
appletviewer or a Java-enabled web browser
Class: family of components sharing common characteristics
consisting of:
 Data: information
 Method: functionality
Object: instance of a class
Variable: represent value stored in computer memory. A variable
must be defined or declared before being used
 Somewhat like containers
Mindstorms
4.3
Why Le







OS ???
Relatively Small – 16kb
Cross Platform
Powerful - full ability of a professional programming
language
More transparent
Useful for teaching Java
Limitations?
More info:
 LeJOS home page: http://lejos.sourceforge.net
 Bagnall text
Mindstorms
4.4
Getting started


Duke tools
 Ambient Eclipse http://www.cs.duke.edu/csed/ambient
Configuring Eclipse for leJOS
 Install LDT from http://lejos.sourceforge.net/tools/eclipse/ldt/
 Download, unzip, and copy the directories
• Features
• Plugins


Put in Eclipse installation directory
Can now create projects
• leJOS RCX
• leJOS Client
Mindstorms
4.5
Writing and Understanding Java

Language independent skills in programming
 What is a loop, how do you design a program?
 What is an array, how do you access files?

However, writing programs in any language requires
understanding the syntax and semantics of the programming
language
 Syntax is similar to rules of spelling and grammar:
• i before e except after c
• Two spaces after a period, then use a capital letter
Mindstorms
4.6
Syntax and Semantics

Semantics is what a program (or English sentence) means
 You ain’t nothing but a hound dog.
 La chienne de ma tante est sur votre tete.

At first it seems like the syntax is hard to master, but the
semantics are much harder
 Natural languages are more forgiving than programming
languages.
Mindstorms
4.7
Toward an Understanding of Java

Traditional first program, doesn’t convey power of computing
but it illustrates basic components of a simple program
public class SayHello {
// traditional first program
}

public static void main(String[] args) {
System.out.println("Hello World!");
}
This program must be edited/typed, compiled and executed
Mindstorms
4.8
Methods/Functions can return values


What does the square root function do?
 When called with parameters of 4, 6.2, -1
What does the method getGcount() return?
public class DNAstuff {
public int getGcount(String dna) {
int total = 0;
for(int k=0; k < dna.length(); k++){
if (dna.charAt(k) == 'g'){
total = total + 1;
}
}
return total;
}
}
Mindstorms
4.9
Specifying language constructs
public Type Name(parameter-list) {
statement-list
}
parameter-list:: empty-list || non-empty-list
non-empty-list:: Type Name
non-empty-list:: Type Name, non-empty-list
if ( boolean-expression ) {
statement-list
}
Mindstorms
4.10
zero-one, true-false, boolean
if ( a == b ) {
statement-list
}
if ( a relational-operator b ) {
statement-list
}
if ( bool-exper conditional bool-expr ) {…}
if ( boolean-function ) {…}
==, !=, <, >, <=, >=
&& ||, !
Mindstorms
4.11
Evaluating expressions

Order of precedence

Operators Associativity Type
()
*
+
<
==
=
/ %
<= >
!=

left to right
left to right
left to right
>= left to right
left to right
right to left
Parentheses
Multiplicative
Additive
Relationals
Equalities
Assignment
Automatic type conversion
 Values of one type are
promoted to another
compatible type as part of
the computation process
Mindstorms

You can convert Tf degrees
Fahrenheit to Tc degrees Celsius
using the formula:
Tc = (5/9)*(Tf-32)
Given the following expression:
double Tc = (Tf – 40.0) * (5/9)
If Tf is –40.0 what is Tc?
1. -40.0
2. 0.0
3. 40.0
4. error
5. unknown
4.12
From Selection to Repetition

The if statement and if/else statement allow a block of statements to be
executed selectively: based on a guard/test
if (area > 20.0)
{
System.out.print(area);
System.out.println(" is large");
}

The while statement repeatedly executes a block of statements while the
guard/test is true
int month = 0;
while (month < 12)
{
PrintCalendar(month, 1999);
month += 1;
// month = month + 1;
}
Mindstorms
4.13
Semantics of while loop
if (test)
{
statements;
statements;
}
test
false
true
test
Statement list
Next statement
Mindstorms
while (test)
{
statements;
statements;
}
false
true
Statement list
Next statement
4.14
Objects and values


Primitive variables are boxes
 think memory location with value
Object variables are labels that are put on boxes
String s = new String(robot);
String t = new String(”robot");
if (s == t) {they label the same box}
if (s.equals(t)) {contents of boxes the same}
s
t
What's in the boxes? ”robot" is in the boxes
Mindstorms
4.15
Objects, values, classes

For primitive types: int, char, double, boolean
 Variables have names and are themselves boxes (metaphorically)
 Two int variables assigned 17 are equal with ==

For object types: String, Sequence, others
 Variables have names and are labels for boxes
 If no box assigned, created, then label applied to null
 Can assign label to existing box (via another label)
 Can create new box using new

Object types are references or pointers or labels to storage
Mindstorms
4.16