Presentation is loading. Please wait.

Presentation is loading. Please wait.

Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/4/2012.

Similar presentations


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

1 Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/4/2012

2  Write a method to…  …convert radians to degrees  …convert degrees to radians  Remember: 360° = 2π radians 2

3  if  else if  else  Allow us to create logic in our programs – do things based on whether statements are true or false 3

4 boolean adult = false; boolean under16 = false; if (age < 16) { adult = false; under16 = true; } else if (age < 18) { adult = false; under16 = false; } else { adult = true; under16 = false; } 4

5  While loops  Do-while loops  For loops  For-each loops (will be covered later) 5

6  Runs while condition is true while (condition) { // Do this! } 6

7  Runs while condition is true, at least once! do { // Do this! } while (condition); 7

8  Combines declaration, initialization, condition, and update for (declaration/initialization; condition; update) { // Do this! } Example: for (int i = 1; i < 10; i++) { // Do this…how many times? } 8

9 Before we move on? 9

10  Is a “game” where you count up by 1, starting at 1.  Whenever the number is a multiple of:  5, you say “Fizz!”  7, you say “Buzz!”  5 and 7, you say “FizzBuzz!”  Else, say the number  Make a method fizzBuzz() that plays the above game to 50.

11 public static void main(String[] args){ for(int i = 1; i <= 50; i++){ if(i%5==0 && i%7==0){ System.out.println(“FizzBuzz!”); }else if(i%5 == 0){ System.out.println(“Fizz!”); }else if(i%7 == 0){ System.out.println(“Buzz!”); }else{ System.out.println(i); } 11

12 Time to move on! 12

13 Readability

14  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;

15  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 quadratic2 = (-b - root)/(2*a);

16  Make a method betterFizzBuzz() that uses constants and gets rid of repetitive code.

17  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. 17

18  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) 18

19  Create another fizzBuzz() method that takes a max, a first, and a second parameter that counts up to the max, says “Fizz!” when divisible by first, “Buzz!” when divisible by the second, and “FizzBuzz!” when divisible by both.

20

21 Ready? Let’s go! 21

22  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  An object usually contains both data and a set of operations that can be performed on that data  An object-oriented program is just a collection of cooperating objects that communicate to accomplish a task 22

23  Class – “Blueprint” that defines properties and behaviors applying to a set of objects  Object – A specific instance of a class Example: Let’s say that “Human” is a class. You are an object—an instance of the Human class. Note that there are many different objects (people) with different characteristics that are all instances of ONE class (Human) 23

24  A key concept/ideal in OOP  In encapsulation, programming is simpler because we don’t need to understand how existing code works, only what it does  Each class only knows its own functions and how it connects with other classes  A class doesn’t care how other classes work internally, making code more flexible and reducing interdepencies 24

25  Example: Say we are given two classes, Washer and Dryer. If we want to make a Laundry Room class, we can use Washer and Dryer without knowing how they work internally  Picture a “Black Box”—see what goes in and comes out, but don’t know what happens inside (More on this later.) 25

26 A class defines 3 things about a type of object:  Information the object contains (data)  What the object does  How the object is made (We’ll touch on this later) 26

27  What information it has?  Weight  Date it was born  Color of spots  What it does?  Moo  Make milk  Eat grass 27

28  What information it has?  Length, Width, Height  Type of Material  Whether it is open or closed  What it does?  Volume  Surface Area  Open/Close  Collapse 28

29  Since a Box is an easy concept to visualize, we will be using it as one of our examples.  We will continue to define our Box class as we learn new concepts, adding to it each time. Any Questions so far? 29

30

31  Primitives 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! 31 int a 25 String s 83

32  Stores variables in objects  Looks like: access static type name; i.e. private double GPA;  Assigned values in constructor, mutator methods  Accessed by accessor methods, or is public.

33  Used to create new objects.  Looks like: public ClassName(parameters) { varName1 = myVarName; }  Called by:  ClassName name = new ClassName(parameters);

34  Does things:  Looks like: access static returnType name(parameters) { //code goes here }  Called by: objectName.methodName(parameters);

35  Accessors access values, returning instance variables / other values.  Mutators mutate values, modifying an instance variable in the object.

36  static refers to whether the method or variable belongs to the class, and is the same, or static, throughout all instances of the class.  For Example, the class Math has static methods such as.abs() (Absolute Value). In order to use this you do not need to create a new instance of Math, you just do Math.abs();

37  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.

38  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.

39

40  What variables would our cow need to store?  weight – a double  isAwake – a boolean  What methods would our cow need to do?  getWeight  getAwake  eat  wakeUp  goToSleep

41  Instance Fields: // how much the cow weighs in pounds private double weight; //true if the cow is awake; false if the cow is sleeping private boolean isAwake;  Constructor: public Cow(double theWeight, boolean awake) { weight = theWeight; isAwake = awake; }

42  Methods: public double getWeight() { return weight; } public boolean isAwake() { return isAwake; }

43 public void wakeUp() { isAwake = true; System.out.println("streeeeetch..."); } public void goToSleep() { isAwake = false; System.out.println("zzzzzz..."); }

44 //This method milks the cow for a given number of seconds. We assume that each //gallon of milk weighs 7 pounds, and that a cow will give one gallon of milk per minute of //milking. It is not terribly realistic in that it lets you convert the entire weight of the cow //into milk, but we're dealing with virtual cows here, so whatever... public double getMilk(int seconds) { if (!isAwake) return 0.0; double gallonsToGive = weight / 7.0; double gallonsWeWant = seconds / 60.0; double gallonsWeGet = Math.min(gallonsToGive, gallonsWeWant); weight -= gallonsWeGet * 7.0;// adjust the cow's weight return gallonsWeGet;// return the milk }

45  Tester: public class CowTester { public static void main(String[] args) { Cow bubba = new Cow(445.0, false); bubba.wakeUp(); bubba.eat(35.0); double gals = bubba.getMilk(240); System.out.println("Bubba gave us " + gals + " gallons of milk!"); bubba.goToSleep(); System.out.println("Bubba now weighs " + bubba.getWeight() + " pounds."); }

46  Do a class box with instance variables length, width, height, isOpen, and material(String).  It should contain accessors/mutators for all of the above, and the methods getVolume, getSurfaceArea, and toString, which returns a String representation of the box.  Also, make a tester.

47  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. 47

48  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 48

49  Create a class coordinate  Should have two values, x and y.  Have a constructor  A method that calculates the distance from the origin  Accessor methods for x and y.  Mutator methods addToX and addToY that increase the value of X and Y by the parameter. 49


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

Similar presentations


Ads by Google