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

Post on 21-Jan-2016

215 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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

2

Review of Week 3

Readability

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;

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

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

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

Classes

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

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.

Constructors

Used to create new objects.Looks like:

public ClassName(parameters){

varName1 = myVarName;}

Called by: ClassName name = new

ClassName(parameters);

Methods

Does things:Looks like:

access static returnType name(parameters)

{//code goes here

}Called by:

objectName.methodName(parameters);

Accessor vs. Mutator Methods

Accessors access values, returning instance variables / other values.

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

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.

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.

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.

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

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

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

Questions?

New StuffTime to move on!

28

Inheritance

Interfaces

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

Example: Vehicle

Class VehicleWhat variables?

▪ weight▪ wheels▪ Miles▪ On or off

What methods?▪ drive(distance)

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.

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

Interfaces vs Abstract Classes

All of an interfaces methods are abstract

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

34

Example: Teleportable

Interface Teleportable

What method(s)? teleport(double

newX)

Practice: a teleporting vehicle

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

top related