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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Week 10: Objects and Classes 1.  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types: 
Week 6: Arrays 1.  Loops are great  But, without a way to talk about a group of values, we can’t get the full potential out of a loop  Enter: the array.
Computer Science 1620 Loops.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
CS0007: Introduction to Computer Programming Introduction to Arrays.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
Week 7 - Wednesday.  What did we talk about last time?  Introduction to arrays  Lab 6.
Week71 APCS-AB: Java Control Structures October 17, 2005.
1 CS 177 Week 5 Recitation Slides Loops. 2 Announcements Project 2 due next Thursday at 9PM. Exam 1 this evening (switch and loop not covered) Old exams.
Arrays After a while. Remember for loops? for(int i = 0; i < 10; i++){ System.out.println(i); } -But what if we are unsure of how many cycles we need?
Lec 19 Array Intro. Agenda Arrays—what are they Declaring Arrays Constructing Arrays Accessing Array cells.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Chapter 8: Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
CS107 References and Arrays By Chris Pable Spring 2009.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Chapter 3- Flow Control. Overview n Why flow control n Branches vs. Loops n Branch statements n Loop Statements n Complex loops n Boolean variables n.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Variables, Primitives, and Objects A Visual Learner’s Guide.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
CSC 212 Object-Oriented Programming and Java Part 2.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Week 6 - Monday.  What did we talk about last time?  while loop examples  Lab 5.
Week 9 - Wednesday.  What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life.
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Comp1004: Loops and Arrays I Whiles, For and Arrays[]
CSC 211 Java I for loops and arrays.
Week 15 – Wednesday CS 121.
Arrays, For loop While loop Do while loop
Looping and Repetition
Loops.
Review for Midterm 3.
Looping and Repetition
Week 7 - Monday CS 121.
Presentation transcript:

Week 10 - Wednesday

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

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

 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

 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

 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

 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

 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

 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

 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!");

 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");

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

 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

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

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

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

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

 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

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

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

 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]);

 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

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

a) b) c) 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; }

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 );

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 ); }

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

 Finish review for Exam 2  Lab 10

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