Fundamentals of Object-Oriented Robot Programming in Java
Download
Report
Transcript Fundamentals of Object-Oriented Robot Programming in Java
Eastside Robotics Alliance / Newport Robotics Group
Day 1 · 10/2/14
T/Th, 6:30 – 8:30 PM
Big Picture School
1
to the Programming Training!
2
…Is new to programming?
…Has programmed in Java before?
…Has programmed in another language?
▪ Which?
…Has a laptop to bring to these meetings?
3
We will cover basic Java and assume that you
haven’t had much programming experience
The pace will be fairly vigorous since we have a
lot of material to cover in 8-10 weeks
You may want to review these PowerPoints
and sample code on your own if you fall behind
(resources may be available online)
We can go faster/slower if needed based on
how much you learn
4
We will be using the Java programming
language during this course
Java is advantageous because it is:
Portable – runs on any computer with Java installed
Simpler – a bit easier to learn than most other
similar languages
Safer – a lot harder to mess up your computer if
you make a mistake in code (remember that no
programming language is ever perfectly safe)
5
Turing
Turing would definitely be a kayak. It’s small.
It’s human powered. It’s often used as a
beginner “boat”. And it’s also very Canadian.
6
Java
Java is a cargo ship. It’s very bulky. It’s very
enterprisey. Though it can also carry a lot of
weight. Will carry a project, but not very fun
to drive.
7
Perl
Perl is a tugboat. Powerful enough to
tug Java around, in 80 characters or less.
8
Ruby
Ruby is difficult to describe. It’s sleek, sexy,
and very fun to drive. Here’s a picture. Very
trendy.
9
PHP
PHP is a bamboo raft. A series of hacks held
together by string. Still manages to keep
afloat though.
10
C
C is a nuclear submarine. The instructions
are probably in a foreign language, but all of
the hardware itself is optimized for
performance.
11
HTML
HTML isn’t really a programming
language boat.
12
Basic stuff you need to know to write code in Java
22
Java is a “high-level programming language,”
meaning we write our programs in a language
somewhat resembling English
However, computers understand only bits
(binary digits): 10010101 01101100 01001011
23
When we make a .java file on our computer, we
have to compile it before the program can run.
The Java compiler (to make a long story short)
takes the code and turns it into ones and zeros
so that the computer can understand it.
The Java compiler is built into the program we
will be using, so you don’t need to worry about
it too much
24
Code is a series of keywords and instructions
that tell the computer what to do
In Java, whitespace does not tell the computer
to stop doing something, although you need at
least one space between keywords
Generally, we put each statement on its own line to
make it easy to read
A semicolon at the end of a statement tells the
compiler where the statement ends;
Curly braces { } create blocks of related code
25
boolean – true or false (Is the cow awake?)
int – an integer (How many spots on a cow?)
double – a precise floating-point number (How
many pounds does the cow weigh?)
Others
float – less-precise floating-point number
char – a text character, such as: ‘a’ ‘$’ ‘6’
A few more we probably don’t need to know…
26
These data types are the most basic values,
and are often called “primitive types”
Task: Assign a primitive type to each part of
the Box class definition, where appropriate:
boolean
int
double
27
We use variables to store data in the
computer’s memory
Before we can use a variable, we must declare
it to the compiler
A declaration consists of a data type and a
variable name (plus a semicolon).
Examples:
boolean awake;
int spots;
28
When naming things in Java, we have to keep
things as one word
Generally, it is best if variable names are as
descriptive as possible
For variables, leave the 1st letter lowercase
Capitalize the 1st letter of each word
This system is called camelCase (for the
“hump” at the beginning of each word)
Example: robotPositionOnField
29
What data type would be appropriate for
storing whether you are an adult? What
would be the value?
What data type would we use for the amount
of gas left in a car? What would be a good
variable name?
Write declaration statements for these
variables.
30
Once you declare a variable with a primitive
type, it contains its default value
Default values:
boolean: false
int: 0
double: 0.0
float: 0.0f
▪ (f indicates it’s a float and not a double)
31
Once we have a variable, we can use it!
Use the equals sign (=) to assign a value on
the right to a variable on the left
int studentCount;
studentCount = 17;
Now studentCount holds the value 17!
32
Declaring a variable and then setting its value is
tedious. We can do this faster on one line:
int studentCount;
studentCount = 17;
int studentCount = 17;
These two methods are equivalent!
When you declare a variable, you should almost
always initialize it right away.
33
double My GPA =
int gallonsOfMilk = 14.5;
What’s wrong with the above code?
34
We can explain what code does or make notes about it
using comments
Code after two slashes (//) will be ignored by the Java
compiler if it is on the same line
Any code between /* and */ will be ignored, even if it
is on multiple lines.
Example:
x = 2; /* This is a multi-line
comment explaining my code. */
y = 3; // Here is a single-line comment.
b = true;
35
The colors signify special key words in your
code for readability. The colors can be
changed in settings.
36
So your code can do math and have logic!
37
Output boolean values – true or false
Equality: ==
Greater than: >
Less than: <
Greater than or equal: >=
Less than or equal: <=
Not equal: !=
38
5>2
2.0 != 2.0
true == false
6.0 < 6
(5 >= 5)
true != false
172 = 172
7>2>1
true
false
false
false
true
true
Don’t do these! Why?!
39
Binary operators take two boolean operands:
isMale && isAdult
// true if both left AND right
// operands are true
doorOpen || haveKey
// true if one OR the other
// operand is true (or both)
Unary operators take one boolean operand:
Not: !
// turns false true and vice versa
40
AND
&&
OR
||
true && true == true
true || true == true
true && false == false
true || false == true
false && true == false
false || true == true
false && false == false false || false == false
NOT
!
!true == false
!false == true
41
true && false =
false
true || false =
true
!(false && true) =
true
false || (false || true) =
true
(false || false) || (false && true) =
false
!false && false || false && (true || !false) = false
42
Addition +
Subtraction –
Multiplication *
Division /
Remainder of Division %
Increment (add 1) ++
Decrement (subtract 1) -43
2 + 7 ==
4 – 1.0 ==
2 * 8 ==
7 / 4 ==
3 + 5 / 2 ==
10 - 2 * 3.6 ==
52 % 9 ==
9
3.0
16
1
5
2.8
7
44
To add 6 to a number, we could write:
int x = 5;
x = x + 6;
Or we could use the += operator:
int x = 5;
x += 6;
+= adds a number at right to the value of x and
stores the result back in the variable x
A similar thing is true for -=, *=, /=, and %=
45
A group of characters (chars) that form text
Use quotation marks to make a String
Example: “The quick brown fox
jumps over the lazy dog.”
You can display a String on the screen using
System.out.println(“show this”);
46
…Before we try it out?
47
When you run a program, the first thing that
happens is the main method runs
Code between the curly braces will be executed
Don’t worry about the details of the first line —
it will make more sense later
public static void main(String[] args)
{
// ...Code you put here will run!
}
48
This is our first program, which will print
“hello world” to output.
How would you print “Hello World!”?.
System.out.println(“Hello World”);
49
This code will compute the sum of two
integers and print it on the screen.
int a = 5;
int b = 7;
int c = -1;
int sum = a + b + c;
System.out.println(sum);
50
Modify the sample code to display the average
of the two numbers instead of the sum
Declare a variable to hold the average
Assign the average of the integers to the average
variable
Print the result
Modify the sample code to take the sum of three
numbers instead of two
Declare a third integer
Add it to the sum variable
Both of the above: average of three variables!
52
int a = 1;
int b = 4;
double average = (a + b) / 2.0;
System.out.println(average);
int a = 5;
int b = 7;
int c = -1;
int sum = a + b + c;
System.out.println(sum);
int a = 5;
int b = 7;
int c = -1;
double average = (a + b + c) / 3.0;
System.out.println(average);
53