Fundamentals of Object-Oriented Robot

advertisement
Newport Robotics Group
Week 4 10/23/2014
Tuesdays, 6:30 – 8:30 PM
Newport High School
1
Review of Week 3
So we have the foundation needed to move on to new things!
2

You can’t change the value.
Use the key-word final.
Uses a different naming convention.
Helps readability.

Instead of:



 int num2 = num1 + 7;
 final int NUM2MODIFIER = 7;
int num2 = num1 + NUM2MODIFIER;

To improve readability, you can often remove
repetitive code, for example:
 Instead of:
double quadratic1 = (-b + Math.sqrt(b*b – 4*a*c))/(2*a);
double quadratic2 = (-b - Math.sqrt(b*b – 4*a*c))/(2*a);
 Use:
double root = Math.sqrt(b*b – 4*a*c);
double quadratic1 = (-b + root)/(2*a);
double quadratic1 = (-b + root)/(2*a);



A method is overloaded if there are two or
more methods with the same name, but
different data types for the parameters
and/or number of parameters
Allows a method to perform similar tasks
with different inputs
Example: The System.out.println method is
overloaded so it can print different types of
data: Strings, ints, booleans, doubles, etc.
6

Say we have two methods:
public static double mean(int a, int b) { … }
public static double mean(int a, int b, int c) { … }

The compiler will match up a method call to the
correct method implementation based on the
count and types of the given parameters
 mean(2, 3) will call the first method
 mean(5, 7, 1) will call the second method
 mean(1) will result in an error, as there is no matching
method for mean(int)
 mean(1.0) will also be an error: no method mean(double)
7


Primitives store by value
int a
String s
25
83
Objects store by reference
 A reference says where the object is in memory:
the actual object is somewhere else

In this example, a stores the value of the int,
25, while s stores the address of where the
String object is in memory, 83. 83 is NOT the
value of the String!
9




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.


Used to create new objects.
Looks like:
public ClassName(parameters)
{
varName1 = myVarName;
}

Called by:

ClassName name = new ClassName(parameters);



Does things:
Looks like:
access static returnType name(parameters)
{
//code goes here
}
Called by:
objectName.methodName(parameters);


Accessors access values, returning instance
variables / other values.
Mutators mutate values, modifying an
instance variable in the object.

static refers to whether the method or
variable belongs to the class, and is the same,
or static, throughout all instances of the class.

Now that we know how to use classes, we’re
going to test them, without a main method
directly inside the class.

Is another class that is usually called
ClassNameTester.


Testers contain a main method, then
construct an object of the class it is testing.
It then uses the methods to test and see if it
works.






Create a class Tuple that does the following
A constructor with two parameters for first and
second.
Has 2 integer parameters, first and second that can
be accessed publically.
Has a method that returns the product of the two
numbers
Has a method that returns the larger of the two
numbers
Has a method that takes in another tuple and adds
that tuples’ first to its first and that tuples’ second to
its’ second.
24





Create a class dayIn2014
The class with have instance variables day
and month, and static variable year.
Create constructor that takes a month and
date as parameters
Create assessors and mutators methods for
all changeable variables
Create a static method that take in two dates
as parameters and returns true if the first
date is earlier than the second and vice versa
25






Create a class coordinate
Should have two values, x and y.
Have a constructor
A method that calculates the distance from
the origin
Accessor methods for x and y.
Mutator methods addToX and addToY that
increase the value of X and Y by the
parameter.
26
Time to move on!
28
Interfaces




Inheritance is when a class takes all the
methods and variables of another
class(superclass) for itself and adds methods
and variables that weren’t in the original.
Each class can have one superclass, and an
infinite amount of subclasses.
You implement a subclass by adding extends
ClassName in the class declaration
For Example:
 public class MountainBike extends Bicycle

Class Vehicle
What variables?
▪
▪
▪
▪
weight
wheels
Miles
On or off
 What methods?
▪ drive(distance)

Create a subclass of Vehicle (maybe Car, Bike,
Truck?) with its own methods/variables e.g.
 turnOn()
 refuel()
 miles per gallon
 yPos
 Etc.





Interfaces are a way to create a standard to how
different classes interact
Interfaces can only contain constants, method
signatures, and nested types
Interfaces have empty methods that are only
declared. These are called abstract methods.
Interfaces are implemented in classes or extended in
other interfaces
For example:
 public class MountainBike implements Wheeled


All of an interfaces methods are abstract
An Abstract class has at least one method
that is non abstract
34

Interface Teleportable

What method(s)?
 teleport(double newX)

Create a new class that extends your vehicle
subclass, and implements the teleportable
interface.
Download