140 likes | 259 Views
beginning Programming. What is programming?. Literally – giving instructions to a computer so that it does what you want Practically – using a programming language (such as Java) to solve a problem Ex: Controlling a robot autonomously, mathematical computations, vision processing.
E N D
What is programming? • Literally – giving instructions to a computer so that it does what you want • Practically – using a programming language (such as Java) to solve a problem • Ex: Controlling a robot autonomously, mathematical computations, vision processing
What is java? • Cross-platform write-once run anywhere high level object-oriented programming language • The Java Virtual Machine • Software that runs on most operating systems • Runs your java code • Handles all OS specific programming problems • Write a program one time and run it on every major OS
Object oriented Java is based on Objects An Object is an instance of any class, e.g. a String variable Objects provide different functionalities depending on what it’s class was designed to do Makes programming easier because Objects do most of the work
The Structure of a Basic Program public static void main(String[] args){…} Primitive types (variables) boolean (true or false) byte (0-255) char (a single letter/character) short (small integer) int (an integer) float (decimal number) double (decimal with double precision of float) long (very large integer)
Simple Objects An object is an instance of any class (e.g. an instance of the String class) String Not a primitive type PrintStream (e.g. System.out) Allows you to output data
The “Hello World Program” • The most basic program in any language – just prints out “Hello World” • In Java
Conditionals • “Modifies” your program at runtime • Changes what/when the program does something based on a test • Ex: • If a is greater than b, the program does one thing, and does another if a is not greater than • Else – the code inside the else block executes if none of the if statements are true
Iteration • Used when you want to do something multiple times or while a condition is true • For loop • While loop • Do-while loop
Methods • Public void name(){…} • Most basic definition: a block of code that does something useful • Use methods to simplify code and make programming easier • Ex: arm.setAngle(90) is much easier than directly controlling the motors and stopping it at 90 degrees
Method Arguments • Argument – any data that you pass to a method • Ex: • Where a and b are the numbers that you want to add • Arguments give data to a method so that it can do what it was designed to do
Method Modifiers • Public/Private – controls who can access this method • Private - only accessible within the defining class • Public - accessible by any class or object • Static – no instance of object required to run the method
Returning Data From a Method • Void – doesn’t return anything • Ex: • Object – returns an Object of the specified class • Ex: • Return data using the return command • Ex: