Fundamentals of Object-Oriented Robot Programming in Java: Newport Robotics Group
Fundamentals of Object-Oriented Robot Programming in Java: Newport Robotics Group
Object-Oriented Programming
(OOP)
Object
Primitive Types
boolean
awake?)
int an integer (How many spots on a
cow?)
double a precise floating-point number
(How many pounds does the cow weigh?)
Others
Variables
We
camelCase
When
Default Values
Once
Default
values:
boolean: false
int: 0
double: 0.0
float: 0.0f
(f indicates its a float and not a double)
Assignment
Once we have a variable, we can use it!
Use the equals sign (=) to assign a value
int studentCount;
studentCount = 17;
Now studentCount holds the value 17!
Initialization
Declaring
int studentCount;
studentCount = 17;
int studentCount = 17;
These two statements are equivalent!
When you declare a variable, you should almost
always initialize it right away.
Example:
x = 2; /* This is a multi-line
comment explaining my code. */
y = 3; // Here is a single-line comment.
b = true;
Comparison Operators
Output boolean
Equality: ==
equal: !=
>=
Boolean Operators
AND
OR
&& ||
true &&
true ==
true
true &&
false
==
false
false
&& true
==
false
true ||
true ==
true
true ||
false
== true
NOT
false
|| true
== true
!true == false
Math Operators
Addition +
Subtraction
Multiplication *
Division /
Remainder of Division %
Increment (add 1) ++
Decrement (subtract 1)
--
Assignment/Math Operators
To
int x = 5;
x = x + 6;
Or
int x = 5;
x += 6;
Strings
A group of characters (chars) that form
Use quotation marks to make a String
text
Example:
You
Sample Code
This
int a = 5;
int b = 7;
int c = -1;
int sum = a + b + c;
System.out.println(sum);
Your Turn
Write
average variable
Prints the result
A Possible Solution
int a = 1;
int b = 4;
double average = (a + b) / 2.0;
System.out.println(average);
Methods
What is a method?
method is a block of code that performs
some operation
Think of it as a machine that both inputs
and outputs data
A
Methods
Think
Method Name
A
sumOfThreeInts
Parameters
to a method are called parameters
or arguments
Just as before, we need to tell the
compiler what data types the parameters
are by declaring them
Separate parameters by commas
Inputs
Example:
int a, int b
Return Type
When
Access Modifiers
method should specify an access
modifier to control who can use the
method
The most common access modifiers are
public and private
Public methods can be used by anyone
Private methods can only be used
internally
Example: In an ATM machine, the method
enterPIN should be public (users can
press buttons), but the method
A
Method Signature
method signature is the first line of
any method and tells the compiler what
the method does
The method body, or code that runs when
the method is used, must follow the
contract set up by the method signature
Example: if the signature says that the
method outputs an int, the body cannot
output a boolean.
The
Method Signature
We
Example:
public int sumOfThreeInts(int a, int b, int c)
Access
Modifier
Return
Type
Method
Name
Parameter
s
Method Body
The
Implementation
When
Calling Methods
Now
To
Example:
Any Questions?
Before you try it?
Possible Solution
public double average(int a, int b){
double a = a+b;
a = a/2;
return a;
}
public static void main(String[] args){
System.out.println(average(6,9));
//prints out 7.5
}
Possible Solution
public double a(double a, double b, double c){
return (a+b+c)*2.0/3.0;
}
public static void main(String[] args){
System.out.println(a(5.5,7.5,9));
}
Possible Solution
public static int(int a, int b){
return(Math.abs(a-b)+a+b)/2;
}
Possible Solution
public static int max(int[] a, int i){
if(i == 1){
return(Math.abs(a[0]-a[1])+a[0]+a[1]);
}
int[] b = new int[2];
b[0] = a[i];
b[1] = max(a,i-1);
return max(b,1);
}