newport robotics group 1 tuesdays, 6:30 – 8:30 pm newport high school week 4 10/23/2014

29
Fundamentals of Object- Oriented Robot Programming in Java Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Upload: thomas-walsh

Post on 21-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Fundamentals of Object-Oriented Robot Programming in Java

Newport Robotics Group

1

Tuesdays, 6:30 – 8:30 PMNewport High School

Week 4 10/23/2014

Page 2: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

So we have the foundation needed to move on to new things!

2

Review of Week 3

Page 3: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Readability

Page 4: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Constants

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;

Page 5: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Repetitive Code

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);

Page 6: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Overloading

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

Page 7: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Overloading

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

Page 8: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Classes

Page 9: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Primitive Types vs. ClassesPrimitives store by value

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

int a

25

String s

83

Page 10: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Instance Fields

Stores variables in objectsLooks like:

access static type name;i.e. private double GPA;

Assigned values in constructor, mutator methods

Accessed by accessor methods, or is public.

Page 11: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Constructors

Used to create new objects.Looks like:

public ClassName(parameters){

varName1 = myVarName;}

Called by: ClassName name = new

ClassName(parameters);

Page 12: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Methods

Does things:Looks like:

access static returnType name(parameters)

{//code goes here

}Called by:

objectName.methodName(parameters);

Page 13: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Accessor vs. Mutator Methods

Accessors access values, returning instance variables / other values.

Mutators mutate values, modifying an instance variable in the object.

Page 14: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Static

static refers to whether the method or variable belongs to the class, and is the same, or static, throughout all instances of the class.

Page 15: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Testers

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.

Page 16: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Testers (cont.)

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.

Page 17: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Practice With Classes 1 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

Page 18: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Practice With Classes 2

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

Page 19: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Practice With Classes 3

Create a class coordinateShould have two values, x and y.Have a constructorA method that calculates the

distance from the originAccessor methods for x and y.Mutator methods addToX and addToY

that increase the value of X and Y by the parameter.

26

Page 20: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Questions?

Page 21: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

New StuffTime to move on!

28

Page 22: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Inheritance

Interfaces

Page 23: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

What is inheritance?

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

Page 24: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Example: Vehicle

Class VehicleWhat variables?

▪ weight▪ wheels▪ Miles▪ On or off

What methods?▪ drive(distance)

Page 25: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Practice: subclass of Vehicle

Create a subclass of Vehicle (maybe Car, Bike, Truck?) with its own methods/variables e.g. turnOn() refuel() miles per gallon yPos Etc.

Page 26: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

What are interfaces?

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

Page 27: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Interfaces vs Abstract Classes

All of an interfaces methods are abstract

An Abstract class has at least one method that is non abstract

34

Page 28: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Example: Teleportable

Interface Teleportable

What method(s)? teleport(double

newX)

Page 29: Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014

Practice: a teleporting vehicle

Create a new class that extends your vehicle subclass, and implements the teleportable interface.