Presentation is loading. Please wait.

Presentation is loading. Please wait.

Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.

Similar presentations


Presentation on theme: "Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014."— Presentation transcript:

1 Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014

2  Display the sum of 4 numbers  Display the remainder when the sum is divided by 3 2

3  We will be using the Java programming language during this course  Java is advantageous because it is:  Portable – runs on any computer with Java installed  Simpler – a bit easier to learn than most other similar languages  Safer – a lot harder to mess up your computer if you make a mistake in code (remember that no programming language is ever perfectly safe) 3

4  Object Oriented Programming (OOP) is a methodology that breaks programs into discrete, reusable units of code called ‘objects’ which attempt to model things in the real world  Class – “Blueprint” that defines properties and behaviors applying to a set of objects  Object – A specific instance of a class 4

5 A class defines 3 things about a type of object:  Instance Fields - Information the object contains (data)  Methods - What the object does  Constructors - How the object is made 5

6  Code is a series of keywords and instructions that tell the computer what to do  In Java, whitespace does not tell the computer to stop doing something, although you need at least one space between keywords  Generally, we put each statement on its own line to make it easy to read  A semicolon at the end of a statement tells the compiler where the statement ends;  Curly braces { } create blocks of related code 6

7  boolean – true or false (Is the cow awake?)  int – an integer (How many spots on a cow?)  double – a precise floating-point number (How many pounds does the cow weigh?)  Others  float – less-precise floating-point number  char – a text character, such as: ‘a’ ‘$’ ‘6’  A few more we probably don’t need to know… 7

8  We use variables to store data in the computer’s memory  Before we can use a variable, we must declare it to the compiler  A declaration consists of a data type and a variable name (plus a semicolon).  Examples:  boolean awake;  int spots; 8

9  When naming things in Java, we have to keep things as one word  Generally, it is best if variable names are as descriptive as possible  For variables, leave the 1 st letter lowercase  Capitalize the 1 st letter of each word  This system is called camelCase (for the “hump” at the beginning of each word)  Example: robotPositionOnField 9

10  Once you declare a variable with a primitive type, it contains its default value  Default values:  boolean: false  int: 0  double: 0.0  float: 0.0f ▪ (f indicates it’s a float and not a double) 10

11  Once we have a variable, we can use it!  Use the equals sign (=) to assign a value on the right to a variable on the left int studentCount; studentCount = 17; Now studentCount holds the value 17! 11

12  Declaring a variable and then setting its value is tedious. We can do this faster on one line: int studentCount; studentCount = 17; int studentCount = 17; These two statements are equivalent! When you declare a variable, you should almost always initialize it right away. 12

13  We can explain what code does or make notes about it using comments  Code after two slashes ( // ) will be ignored by the Java compiler if it is on the same line  Any code between /* and */ will be ignored, even if it is on multiple lines.  Example: x = 2; /* This is a multi-line comment explaining my code. */ y = 3; // Here is a single-line comment. b = true; 13

14  Output boolean values – true or false  Equality: ==  Greater than: >  Less than: <  Greater than or equal: >=  Less than or equal: < =  Not equal: != 14

15 AND && OR || true && true == truetrue || true == true true && false == falsetrue || false == true false && true == falsefalse || true == true false && false == falsefalse || false == false 15 NOT ! !true == false !false == true

16  Addition +  Subtraction –  Multiplication *  Division /  Remainder of Division %  Increment (add 1) ++  Decrement (subtract 1) -- 16

17  To add 6 to a number, we could write: int x = 5; x = x + 6;  Or we could use the += operator: int x = 5; x += 6 ;  += adds a number at right to the value of x and stores the result back in the variable x  A similar thing is true for -=, *=, /=, and %= 17

18  A group of characters (chars) that form text  Use quotation marks to make a String  Example: “The quick brown fox jumps over the lazy dog.”  You can display a String on the screen using System.out.println(“show this”); 18

19  When you run a program, the first thing that happens is the main method runs  Code between the curly braces will be executed  Don’t worry about the details of the first line — it will make more sense later (and Eclipse will automatically generate it for you) public static void main(String[] args) { //...Code you put here will run! } 19

20  This code will compute the sum of three integers and print it on the screen. int a = 5; int b = 7; int c = -1; int sum = a + b + c; System.out.println(sum); 20

21 21

22  Write code that does the following:  Declares two integer variables  Initializes the two variables  Declares a variable to hold an average  Assigns the average of the two integers to the average variable  Prints the result 22

23 int a = 1; int b = 4; double average = (a + b) / 2.0; System.out.println(average); 23

24 Organize our code’s functions 24

25  A method is a block of code that performs some operation  Think of it as a machine that both inputs and outputs data  An accessor method provides access to data  A mutator method changes data 25

26  Think of a method from our earlier example: We need to find the average of two integers.  What are the inputs of the method?  What is the output of the method?  What does the method do?  What is the method called? 26

27  A method name must describe what the method does accurately and succinctly  Use camelCase starting with a lowercase letter  Example: sumOfThreeInts 27

28  Inputs to a method are called parameters or arguments  Just as before, we need to tell the compiler what data types the parameters are by declaring them  Separate parameters by commas  Example: int a, int b 28

29  When a method outputs a value, we say that it returns that value  We must specify to the compiler what data type the method outputs  This is called the return type  If a method has no output, its return type is called void 29

30  A method should specify an access modifier to control who can use the method  The most common access modifiers are public and private  Public methods can be used by anyone  Private methods can only be used internally  Example: In an ATM machine, the method enterPIN should be public (users can press buttons), but the method releaseMoney should be private and only used internally 30

31  The method signature is the first line of any method and tells the compiler what the method does  The method body, or code that runs when the method is used, must follow the “contract” set up by the method signature  Example: if the signature says that the method outputs an int, the body cannot output a boolean. 31

32  We can make a method signature by putting the following parts together:  Access Modifier  Return Type  Method Name  Parameters (in parentheses) If there are no parameters, use empty parentheses (). Do not put void. Example: public int sumOfThreeInts(int a, int b, int c) Access ModifierReturn TypeMethod NameParameters 32

33  The body of the method is a block of code between two curly braces  The method body uses the inputs and generates the output  The return statement indicates the output public int sumOfThreeInts(int a, int b, int c) { //...(Method body goes here) return something; //something is an int } Method Signature 33

34  When we write the code for a method, we say that we implement that method public int sumOfThreeInts(int a, int b, int c) { int sum = a + b + c; return sum; } Implementation 34

35  Now we have created our method, or machine. However, just because it knows what to do doesn’t mean it gets used  Example: A washing machine may have all the code it needs to run, but nothing will happen until the user puts clothes in and turns it on  To make a method run, we call it. 35

36  To call a method, put the method name and values for any parameters in parentheses  Example: sumOfThreeInts(5, 2, 1) In this case, the method will run with a having a value of 5, b with a value of 2, and c with a value of 1. However, we don’t do anything with the value the method returns in this case 36

37  We can make use of method calls by storing their return values in a variable.  Example: int x = sumOfThreeInts(1, 4, -3);  In this example, after the line of code runs, x will have a value of 2. 37

38 Before you try it? 38

39  Write the method for the average of two integers complete with a method signature and implementation.  Call your method from the main method.  Store the return value from your method in a variable you declare in main  Print out the return value from your method at the end of the main method. 39

40 public double average(int a, int b){ double a = a+b; a = a/2; return a; } public static void main(String[] args){ System.out.println(average(6,9)); //prints out 7.5 } 40

41  Write a program that takes in 3 doubles and returns in the difference between the mean and the sum.  Call your method from main(String[] args) 41

42 public double a(double a, double b, double c){ return (a+b+c)*2.0/3.0; } public static void main(String[] args){ System.out.println(a(5.5,7.5,9)); } 42

43  Write a program that takes in 2 integers and returns the larger.  No if statements.  Can use Math in java’s library 43

44 public static int(int a, int b){ return(Math.abs(a-b)+a+b)/2; } 44

45  Extend the problem to a method that takes in an array of integers and returns the largest one. 45

46 public static int max(int[] a, int i){ if(i == 1){ return(Math.abs(a[0]-a[1])+a[0]+a[1]); } int[] b = new int[2]; b[0] = a[i]; b[1] = max(a,i-1); return max(b,1); } 46

47  Write a method that takes in an integer n, and outputs all the reduced fractions from 0 to 1 in increasing order with denominator less than or equal to n. 47


Download ppt "Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014."

Similar presentations


Ads by Google