Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.

Similar presentations


Presentation on theme: "Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java."— Presentation transcript:

1 Week 10 - Wednesday

2  What did we talk about last time?  Method example  Roulette simulation  Types in Java

3

4

5

6  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types:  String  arrays  An infinite number of others…

7

8  Picture a ham…  Imagine that this ham is actually a Java object  You may want a reference of type Ham to point at this ham  Let’s call it ham1 ham1

9  Now, what if we have another Ham reference called ham2  What happens if we set ham2 to have the same value as ham1 using the following code? ham1 Ham ham2 = ham1; ham2

10

11  If you declare a lot of references, you have not created any objects, just lots of arrows Eggplant aubergine; DumpTruck truck1; Idea thought; Eggplant aubergine; DumpTruck truck1; Idea thought; aubergine truck1thought

12  When you first declare a reference variable, those arrows point to null  null is a Java keyword that just means nothingness  If you try to do something with null, thinking it is a real object, you can break your program

13  To make those arrows point at a new object, you must call a constructor  A constructor is a kind of method that creates an object  Some constructors allow you to specify certain attributes of the object you are creating  The default constructor does not let you specify anything

14  To call a constructor, you use the new keyword with the name of the class followed by parentheses:  Perhaps there is a Ham constructor that lets you take a double that is the number of pounds that the ham weighs: Ham ham1 = new Ham(); //default constructor Ham ham2 = new Ham( 4.2 ); //weight constructor

15  The objects you are most familiar with by now are String s  They break a few rules:  It is possible to create them without explicitly using a constructor  They can be combined using the + operator  You can still create a String object using a constructor: String s = new String("Help me!");

16  You can't compare two references using ==  It will tell you if the point at the same object, not if those objects have the same value  That's why == doesn't work for String values  Always call the equals() method on objects Wombat bob1 = new Wombat("Bob"); Wombat bob2 = new Wombat("Bob"); if( bob1.equals(bob2) ) System.out.println("One Bob is much like another"); Wombat bob1 = new Wombat("Bob"); Wombat bob2 = new Wombat("Bob"); if( bob1.equals(bob2) ) System.out.println("One Bob is much like another");

17

18

19  A condition is either true or false  boolean variables can store such a condition  Conditions can also be found by using comparison operators:  == (Note: different from = ) <<  <= >>  >=  !=  Two conditions can be joined together using AND && or OR ||  Both conditions must be true to get true using AND, either one can be true for OR  A condition can be inverted using NOT !

20  Allow us to repeatedly execute code  Care must be taken to run exactly the right number of times  Not too many  Not too few  Not an infinite number  Not zero (unless that’s what should happen)  Loops come in three flavors:  while loops  for loops  do-while loops

21  Used when you don’t know how many times a loop will run  Runs as long as the condition is true  Syntax: while( condition ) { //statements //braces not needed for single //statement }

22  Used when you do know how many times a loop will run  Still runs as long as the condition is true  Syntax: for(initialize; condition; increment) { //statements //braces not needed for single //statement }

23  Used infrequently, mostly for input  Useful when you need to guarantee that the loop will run at least once  Runs as long as the condition is true  Syntax: do { //statements //braces not needed for single //statement } while( condition );

24  Infinite loops  Almost infinite loops (with overflow or underflow)  Fencepost errors (off by one)  Skipping loops entirely  Misplaced semicolon

25

26  An array is a homogeneous, static data structure  Homogeneous means that everything in the array is the same type: int, double, String, etc.  Static (in this case) means that the size of the array is fixed when you create it

27  To declare an array of a specified type with a given name :  Example with a list of type int :  Just like any variable declaration, but with [] type[] name; int[] list;

28  When you declare an array, you are only creating a variable that can hold an array  At first, it holds nothing, also know as null  To use it, you have to create an array, supplying a specific size:  This code creates an array of 100 int s int[] list; list = new int[100]; int[] list; list = new int[100];

29  You can access an element of an array by indexing into it, using square brackets and a number  Once you have indexed into an array, that variable behaves exactly like any other variable of that type  You can read values from it and store values into it  Indexing starts at 0 and stops at 1 less than the length list[9] = 142; System.out.println(list[9]); list[9] = 142; System.out.println(list[9]);

30  When you instantiate an array, you specify the length  You can use its length member to find out int[] list = new int[42]; int size = list.length; System.out.println("List has " + size + " elements"); //prints 42 int[] list = new int[42]; int size = list.length; System.out.println("List has " + size + " elements"); //prints 42

31  To declare a two dimensional array, we just use two sets of square brackets ( [][] ):  Doing so creates a variable that can hold a 2D array of int s  As before, we still need to instantiate the array to have a specific size: int [][] table; table = new int[5][10];

32

33 a) 3 4 7 8 13 14 23 24 41 42 b) 12 22 34 44 58 68 716 816 932 1032 c) 0 1 1 2 2 3 3 4 4 5 d) This code contains an infinite loop int a = 1, b = 2; for( int i = 0; i < 5; i++ ) { for( int j = 0; j < 2; j++ ) { System.out.print(a + b + " "); a++; } b *= 2; } int a = 1, b = 2; for( int i = 0; i < 5; i++ ) { for( int j = 0; j < 2; j++ ) { System.out.print(a + b + " "); a++; } b *= 2; }

34 a) A filled yellow square in the center of the screen b) A filled yellow square in the center of a large filled blue square c) A filled yellow square in the center of a larger filled red square in the center of a larger filled blue square d) A large filled blue square in the center of the screen StdDraw.setPenColor(StdDraw.RED); StdDraw.filledSquare(.5,.5,.2 ); StdDraw.setPenColor(StdDraw.BLUE); StdDraw.filledSquare(.5,.5,.3 ); StdDraw.setPenColor(StdDraw.YELLOW); StdDraw.filledSquare(.5,.5,.1 ); StdDraw.setPenColor(StdDraw.RED); StdDraw.filledSquare(.5,.5,.2 ); StdDraw.setPenColor(StdDraw.BLUE); StdDraw.filledSquare(.5,.5,.3 ); StdDraw.setPenColor(StdDraw.YELLOW); StdDraw.filledSquare(.5,.5,.1 );

35 a) An empty String b) A String containing a reversed version of s c) A String containing a copy of s d) Method peter() causes a run-time error public static String peter( String s ) { String temp = ""; while( s.length() > 0 ) { temp += piper( s ); s = s.substring( 0, s.length() - 1 ); } return temp; } public static char piper( String s ) { return s.charAt( s.length() - 1 ); } public static String peter( String s ) { String temp = ""; while( s.length() > 0 ) { temp += piper( s ); s = s.substring( 0, s.length() - 1 ); } return temp; } public static char piper( String s ) { return s.charAt( s.length() - 1 ); }

36 a) 0 b) 6 c) 24 d) 96 int[][][] thingy = new int[2][3][4];

37

38

39  Finish review for Exam 2  Lab 10

40  Exam 2 is next Monday  Start working on Project 4


Download ppt "Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java."

Similar presentations


Ads by Google