Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci...

47
Java Workshop

Transcript of Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci...

Page 1: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

Java Workshop

Page 2: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

Quick java review• Goal: We want the application to tell us

what the nth fibonacci number is.

• Components needed to achieve the goal:

• A Scanner to listen to inputs

• A function to calculate the fibonacci numbers

Page 3: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

How to instantiate an object

• School lowellHighSchool = new School(697);

Type of object

Name of object

MAGIC WORD TO MAKE NEW

THINGS!!

Constructor with argument

Page 4: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• Start a new java application in Netbeans

• Name it fibonacciNumberCalculator

Page 5: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• Java has a scanner class part of the utility package, so we can just take it for granted.

• Instantiate a new scanner and write some code to listen to the input and spit it back out.

Page 6: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• Java has a scanner class part of the utility package, so we can just take it for granted.

• Instantiate a new scanner and write some code to listen to the input and spit it back out.

Page 7: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• But we don't want the program to just quit after putting in one number, we want it to keep on listening to our inputs.

• So let’s put it in a while-true loop

Page 8: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.
Page 9: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• Well, how do we deal with the less intelligent people that can’t seem to enter whole numbers??

• Let’s use a try-catch

Page 10: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.
Page 11: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• Now what? Stuck in a unwanted feature! (infinite loop)

• So we need to make our own parsing logic

• First we need to check if there’s a NEW input

• Then we take the input and try to parse it as integer

Page 12: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.
Page 13: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• Components needed to achieve the goal:

• A Scanner to listen to inputs

• A function to calculate the fibonacci numbers

• Now we can move on to the second component of the application

Page 14: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• What is fibonacci number?

• It’s actually really easy to make a recursive function in computer language! So why don’t we calculate it with a recursive function!

Page 15: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• Complete this function

Page 16: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.
Page 17: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• Now let’s modify the program so we can interface the user input with our calculating function

Page 18: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

GREAT ARE WE DONE YET?!?!?

Page 19: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• Not quite. As you can see, recursive functions are one of the slowest functions in computer science. There are tricks to make it significantly faster by using more memory to store those values, but how can we make it faster when we don’t have a massive memory bank and very limited computing power like on a robot??

Page 20: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• Thanks to this cool guy

• Jacques Philippe Marie Binet

• He found this!

• Well let’s not put his hard work up for display!

Page 21: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• Now that we have 2 different methods that can calculate the fibonacci sequence, which one SHOULD we use?

• Why don’t we use both?

• Write a function that takes in the user input as argument, and automatically chooses which method to use.

Page 22: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.
Page 23: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

FRC Java• Difference:

• Written by WPI

• Slimmed down JVM

• Essentially Java ME (SE 1.2/3 ish)

• No generics, autoboxing

Page 24: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• FIRST provided us with 3 basic styles to control the robot

• Simple Robot

• Iterative Robot

• Command Based Robot

Page 25: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

Command Based• Very very detailed control of every

subsystem

• Requires A LOT of testing/fine tuning.

• Idea for teams with a lot of time at their hands.

Page 26: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.
Page 27: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

Iterative Robot• Much more automation and less

programmer control than command base robot.

• Still allows for modularization in different modes

• Good for teams that is trying to optimize resource consumption on the cRIO.

Page 28: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.
Page 29: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

Simple Robot• Used by most first year teams without a

strong programming team or is trying to switch over to Java

• Easy to use, best for testing prototypes!!!

• 4 Modes

• Autonomous, Teleop, Disabled, Testing

Page 30: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.
Page 31: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

Our version• It’s a 3 way hybrid that we wrote on our

own.

• It’s written from ground up, with modularization in mind.

• It’s automated like simple and iterative robot, but also allows for command based controls and it’s been multi-threaded to optimize for performance

Page 32: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.
Page 33: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

Simple Robot• Let’s start with simple robot

Page 34: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• To control anything, you must first register it with the system.

• For example, lets walkthrough how to setup a drivetrain.

• Physically we have:

Motor Speed Contoller PWM Signal

You Joystick cRIO Sidecar

Page 35: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

How to instantiate an object

• School lowellHighSchool = new School(697);

Type of object

Name of object

MAGIC WORD TO MAKE NEW

THINGS!!

Constructor with argument

Page 36: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

How to access class methods

• The Dot Operator!!!

• lowellHighSchool.getTeachersNames();

• access the function written in School class that will return the names of the teachers of lowellHighSchool

• lowellHighSchool.enrollStudent(“John Smith”);

• enrolls the student named John Smith into lowellHighSchool

Page 37: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• We are really just interfacing with the speed controllers via a joystick

• So we must register those two things with the program

• To do so, simply import the needed library and create an instant of the desired object

Page 38: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• Now that we have the physical connections setup, lets tell the computer what to do with those connections

• We want to use the RobotDrive class and tell that class about these two connections we created

Page 39: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• Our connection has been setup

• We told the cRIO where the two speed controllers are (port 1&2) and what we would like to use them for (RobotDrive)

• We told the robot that we have a joystick plugged into virtual usb hub 1

• Now let’s do something with the system in operatorControl

Page 40: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• Since it’s simple robot, we just need to dump code into the operatorControl function and the cRIO will run whatever it’s inside.

• We are accessing the arcadeDrive function written for RobotDrive class.

Page 41: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• If you don’t know what to put inside an argument, like

• PIDController drivePID = new PIDController(?????????);

Page 42: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• That takes you to the programming bible (API)

Page 43: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

Find the name of the class you have

questions about

Page 44: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

This is what we’re

interested in

Page 45: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• Now we know what to put into the question marks:

• PIDController drivePID = new PIDController(?????????);

• You have to pick one of the overloaded constructors then instantiate your drivePID system

Page 46: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• You can also find information about the class functions, that’s why it’s called the bible!

Page 47: Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci number is. Components needed to achieve the goal: A Scanner.

• If you have time, try writing this

• Use a gyro to drive straight in autonomous mode for 10 seconds.

• You need to create an instance of a gyroscope, and use the reading from the gyroscope to correct the heading

• hint: use arcadeDrive as driving method instead.