Fundamentals of Object-Oriented Robot Programming in Java: Newport Robotics Group
Fundamentals of Object-Oriented Robot Programming in Java: Newport Robotics Group
Review of Methods
What is a method?
A
Methods
Think
Method Name
A
sumOfThreeInts
Parameters
Inputs
int a, int b
6
Return Type
When
Access Modifiers
A
Method Signature
The
Method Signature
We
Example:
public int sumOfThreeInts(int a, int b, int c)
Access Modifier Return Type
Method Name
Parameters
10
Method Body
The
Implementation
When
Calling Methods
Now
13
Example:
16
Possible Solution
public double average(int a, int b, int c,
double d){
double a = a+b+c+d;
a = a/2;
return a;
}
public static void main(String[] args){
System.out.println(average(6,9,17,9.5)+);
//prints out 10.375
}
17
a method to
convert radians to degrees
convert degrees to radians
Remember:
360 = 2 radians
18
19
Conditionals
if
else
else
if
Allow
20
Using Conditionals
boolean adult = false;
boolean under16 = false;
if (age < 16)
{
adult = false;
under16 = true;
}
Loops
While
loops
Do-while loops
For loops
For-each loops (will be covered later)
22
While Loops
Runs
while (condition)
{
// Do this!
}
23
Do-While Loops
Runs
do
{
// Do this!
} while (condition);
24
For Loops
Combines
update
for (declaration/initialization; condition; update)
{
// Do this!
}
Example:
for (int i = 1; i < 10; i++)
{
// Do thishow many times?
}
25
Any Questions?
Before we move on?
26
27
Practice: FizzBuzz
Is
Make
29
FizzBuzz Solution
public static void main(String[] args){
for(int i = 1; i <= 50; i++){
if(i%5==0 && i%7==0){
System.out.println(FizzBuzz!);
}else if(i%5 == 0){
System.out.println(Fizz!);
}else if(i%7 == 0){
System.out.println(Buzz!);
}else{
System.out.println(i);
}
}
}
30
New Stuf
Time to move on!
31
Readabil
ity
Constants
You
of:
Repetitive Code
To
Practice: BetterFizzBuzz
Make
Overloading
A
36
Overloading Methods
Say
37
Questions?
Reviewing Object
Oriented Programming
Ready? Lets go!
39
What is Object-Oriented
Programming?
Object
41
information it has?
What
it does?
Volume
Surface Area
Open/Close
Collapse
42
Class Examples a
Robot
What
information it has?
weight
motors
position
What
it does?
Move
Speak
Develop intelligence (ex: Skynet)
43
Classes
Instance Fields
Stores
variables in objects
Looks like:
access static type name;
i.e. private double GPA;
Assigned values in constructor,
mutator methods
Accessed by accessor methods, or is
public.
Constructors
Used
Called
by:
Methods
Does
things:
Looks like:
access static returnTypename(parameters)
{
//code goes here
}
Called
by:
objectName.methodName(parameters);
What is Static?
static
Testers
Now
Is
Testers (cont.)
Testers
Questions?
55
Practice: Robot
What
What
Fields:
// how much the robot weighs in pounds
private double mass;
//true if the robot is on; false if the robot is
off
private
boolean isAwake;
Constructor:
public (double themass, boolean awake)
{
mass = themass;
isAwake = awake;
}
58
HW/Practice: Box
Do