Top Banner
Java Programming
43

Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Dec 17, 2015

Download

Documents

Gavin Ellis
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Java Programming

Page 2: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Objectives

Introduce Software Engineering ConceptsIntroduction to Object Oriented and Java

ProgrammingProvide Context for Software in FRC RoboticsProvide Foundation Interacting with

Programming Team

Page 3: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Agenda

IntroductionPhilosophyObject OrientedJava SyntaxFRC Robotics

Libraries Tele-operated Autonomous

Page 4: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Introduction

Mr. Rich Vitkus BS Computer Science at Rensselaer Polytechnic

Institute Manager of Enterprise Architect, Lowe’s Home

Improvement 3 years FLL, 4th year FTC, 6th year FRC

Evan Vitkus VP Programming 2 years FLL, 2nd year FTC, 2nd year FRC

Page 5: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Philosophy

Computers do what you tell them to do, not what you want them to do.

Different results mean either the software, the system, or the data has changed

Reuse ideas, libraries, and code.Test and debug systematically with as much

information as possible.Programming is not art it is engineering. The

standards and process are essential to success.

Page 6: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Object Oriented

Consider an airport system in the real world. It consists of many different parts which work together to get work done Control tower, airplane, runways, fuel trucks, security

agent, …When we build a software systems, some of

the software objects have corresponding names Class ControlTower, class Airplane, class Runway, …

Reduces “representation gap” between software model and solution we are building Improves comprehension, communication, and

predictability

Page 7: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Object Oriented – Real World Object

Consider an airplane in the airport system. What is important?

Real world objects have information Airplanes have a model number, serial number, speed,

altitudeReal world objects also do things

Airplanes can land, take off, change speed, turn, …

Page 8: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Object Oriented – Software Object

Have names corresponding to the real world system Class Airplane

Store information – attributes modelNumber, serialNumber, speed, altitude

Perform actions – methods land(), takeOff(), changeSpeed(), turn()

Page 9: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Object Oriented – Software Object

An object bundles data (attributes) and code to perform actions (methods) into one, cohesive unitAirplan

espeedmodel

land()takeOff()

//Java source codeclass airplane{ private int speed; private int model; public void land() { … }

public void takeOff() { … }

Page 10: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Object Oriented – Project

Simple dice game Each player rolls two dice ten times The player rolling seven or more the most times is the

winner

Page 11: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Object Oriented – Project

Identify the real world objects in the game Game Player Die

Page 12: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Object Oriented – Project

Identify the attributes of each object in the game

Game dice players

Player name score

Die value

Page 13: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Object Oriented – Project

Identify the methods of each object in the game

Game play

Player takeTurn incrementScore getScore

Die roll

Page 14: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Object Oriented – Class and Instance

Class Definer: A blueprint

that stores the definition for instances

Creator: A factory for creating software objects of one kind

Instance Instantiated (created)

by a class Occupies space in

computer memory Retains values of

attributes Has behavior

An object bundles data (attributes) and code to perform actions (methods) into one, cohesive unit

Die

value

roll()die1:Dievalue=3

Page 15: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Java Syntax - Attributes

Java is strongly typed

Two basic types in Java Primitive – int, double, boolean, char, etc

private int value; Object – created from classes

private Die die1;

Variables– attributes which can change valuesConstants – attributes which do not change values

private final int SIDES = 6;

Page 16: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Java Syntax - Methods

Methods can perform actions, have parameters, and return values

Public void setName(String playerName){ name = playerName;}

public int roll(){ value = (int)(Math.random()*SIDES) + 1; return value;}

Page 17: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Java Syntax – Control Structures - Conditional

If statements handle boolean (true or false) conditions

if (i == 3) doSomething();

if (i == 2) doSomething(); else doSomethingElse();

if (i == 3) { doSomething(); } else if (i == 2) { doSomethingElse(); } else { doSomethingDifferent();}

Page 18: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Java Syntax – Control Structures - loops

Loops repeat a segment of code

// validate a boolean condition then executewhile (i < 10) { doSomething(); }

// execute then validate a boolean condition (always // run at least oncedo { doSomething(); } while (i < 10);

// execute a specific number of timesfor (int i = 0; i < 10; i++) { doSomething();}

Page 19: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Java Syntax – Project

public class Game {

private Player player1; private Player player2; private Die die1; private Die die2; private int score;

public void play() { for (int i = 0; i < 10; i++) { score = die1.roll() + die2.roll(); if (score > 7) { player1.incrementScore(); } }

for (int i = 0; i < 10; i++) { score = die1.roll() + die2.roll(); if (score > 7) { player2.incrementScore(); } }

if (player1.getScore() > player2.getScore()) { System.out.print("Player 1 wins"); } else { System.out.print("Player 2 wins"); } }}

Page 20: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Agenda

IntroductionPhilosophyObject OrientedJava SyntaxFRC Robotics

Robot Libraries Tele-operated Autonomous

Page 21: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

FRC Robotics – Object Oriented

Software Objects correspond to Real World Objects

Reduces “representation gap” between software model and solution we are building Improves comprehension, communication, and

predictabilityAn object bundles data (attributes) and code

to perform actions (methods) into one, cohesive unit

Page 22: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

FRC Robotics

InputProcessOutput

Page 24: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Robot – Photo 1

Page 25: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Robot – Photo 2

Page 26: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Robot – Photo 3

Page 27: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

FRC Robotics - Libraries

FRC Javadoc http://wbrobotics.com/javadoc/index.html import edu.wpi.first.wpilibj.Joystick; import edu.wpi.first.wpilibj.RobotDrive; import edu.wpi.first.wpilibj.Victor; import edu.wpi.first.wpilibj.Servo; import edu.wpi.first.wpilibj.Jaguar; import edu.wpi.first.wpilibj.Compressor; import edu.wpi.first.wpilibj.camera.AxisCamera;

Getting Started with Java for FRChttp://www.youtube.com/user/thenewboston

Page 28: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

FRC Robotics – Teleoperated

Human Players control the robotSoftware controls the robotEvent LoopInput

JoySticks Sensors Timer Camera

Process Calculations

Output Speed controllers Solenoid

Page 29: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

FRC Robotics – Teleoperated

Human Driver and Human Robot Eyes open Eyes closed

Page 30: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

FRC Robotics – Teleoperated

Pseudo Code

Page 31: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

FRC Robotics – Autonomous

No Human PlayersSoftware controls the robotInput

Sensors Timer Camera

Process Calculations

Output Speed controllers Solenoid

Page 32: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

FRC Robotics – Autonomous

Human Robot Vision Touch Time

Page 33: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

FRC Robotics – Autonomous

Pseudo Code

Page 34: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

FRC Robotics – Questions

Page 35: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

FRC Robotics – Appendix

Slides removed to reduce to 75 minutes

Page 36: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Java Syntax - Methods

Setters and Getters Setters are a public method to set the value of private

attributes Getters are a public method to get the value of private

attributes

PlayerPublic void setName(String playerName)Public String getName()Public void setScore(int playerScore)Public int getScore()

Page 37: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Object Oriented – Concepts

Cohesion – strongly related responsibilities of a software component

Encapsulation – hide information or details Attributes are private (only exposed to the class) Methods are public (exposed to the world)

Polymorphism – multiple methods with the same name which operate on different data print(file) print(integer)

Inheritance – a generalization, specialization hierarchy. Extend or override functionality and data from a parent (super) class boy is a human is a mammal

Page 38: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Object Oriented – Project

Game

Player Die

Plays Has

Rolls

Page 39: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Dice Game Code - Die

public class Die { private final int SIDES = 6; int value;

public int getValue() { return value; }

public int roll() { value = (int)(Math.random()*SIDES) + 1; return value; }}

Page 40: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Dice Game Code - Player

public class Player { private String name; private int score = 0;

public void setName(String playerName) { name = playerName; } public String getName() { return name; } public int getScore() { return score; }

public void takeTurn(Die die1, Die die2) { int roll; score = 0; for (int i = 0; i < 10; i++) { roll = die1.roll() + die2.roll(); if (roll > 7) { incrementScore(); } } } public void incrementScore() { score += 1; }}

Page 41: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Dice Game Code - Game

public class Game {

private static Player player1 = new Player(); private static Player player2 = new Player(); private static Die die1 = new Die(); private static Die die2 = new Die(); public static void play() { player1.takeTurn(die1, die2); player2.takeTurn(die1, die2);

if (player1.getScore() > player2.getScore()) { System.out.print("Player 1 wins " + player1.getScore() + " to " + player2.getScore()); } else if (player1.getScore() < player2.getScore()){ System.out.print("Player 2 wins " + player2.getScore() + " to " + player1.getScore()); } else { System.out.print("It's a tie " + player2.getScore() + " to " + player1.getScore()); } }}

Page 42: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Java Syntax – Control Structures - Conditional

Switch statements handle multiple cases

switch (ch) { case 'A': doSomething(); // Triggered if ch == 'A' break; case 'B': case 'C': doSomethingElse(); // Triggered if ch == 'B' or ch ==

'C' break; default: doSomethingDifferent(); // Triggered in any other case break; }

Page 43: Java Programming. Objectives Introduce Software Engineering Concepts Introduction to Object Oriented and Java Programming Provide Context for Software.

Java Syntax – Arrays

Arrays are an ordered collection A dozen eggs The passengers on an airplane

// one dimensional arrayint[] numbers = new int[5]; numbers[0] = 2; numbers[1] = 5; int x = numbers[0];

// multi dimensional arrayint[][] numbers = new int[3][3]; number[1][2] = 2;