Computer Science 1026 Topic 5a.docx

4 Pages
Unlock Document

Computer Science
Computer Science 1026A/B
Brian Langen

Topic 5: Introduction to Pictures Picture  Picture: A grid or matrix of pixels o The grid has columns and rows shown with (x,y) position in the grid.  Creating a picture object o String fileName = FileChooser.pickAFile(); o Picture pictureObj = new Picture(fileName);  Properties o  A picture is a grid or matrix of pixels  The grid has columns and rows, each with an (x,y) position in the grid.  A Picture object can be created by: o String filename = FileChooser.pickAFile(); o Picture pictureObj = new Picture(filename);  A picture object has properties width and height (measured in ‘pixels’) o Picture Width: pictureObj.getWidth(); o Picture Height: pictureObj.getHeight();  The Picture class has a method that lets us explore a picture by moving the censor around in the picture o We can see the x and y values and the color of the pixel at the cursor. o To use on a picture object: pictureObj.explore(); Arrays  Array: Storage for a collection of items of the same type o It is possible to access individual items by using an index  Index: o It is possible to access individual items by using an index o The index starts at 0 o The last item is at index (length - 1)   In Java, arrays behave like objects o We need to declare a reference variable o We need to create the array object  We declare array reference variable by o type [] name; o Ex. int[] numbers; This creates a reference variable called numbers that can be used to refer to an array of int. But this does not actually ‘create’ the array  Creating Arrays o Use the Java keyword ‘new’ o Ex. numbers = new int[6];  This creates an array of 6 integers (of 48 bytes), and has reference variable numbers refers to it  The reference variable numbers refers to the entire collection of integers.  This does not store any data in the array. o We can declare and create in one statement  Int[] numbers = new int[6]; o Once we create an array, its size is fixed and we cannot change it  Array Indexing o Each item on an array can be accessed individually, using indexing. o Ex. numbers[0] refers to the first element of the array o Ex. numbers[i] refers to the (i+1th) element of the array  Initializing Arrays o It is possible to declare, create, and initialize an array in one statement. o However, this statement cannot be divided into two different statements, because the computer needs to be told how long the array is supposed to be o Ex. int[] numbers = {3,7,9,2,1,5}; This creates an array of 6 integers, and sets the initial values of the integers in the array according to the values specified. o The length of the array is determined by the number of items listed  Array size o Java remembers the size of arrays o Within a special attribute ‘length’ of the array object which stores the size of the array. o NOTE: length is a variable, not a method! Therefore, there are no parentheses after length.  Int arraySize = numbers.length; o Useful to get the last item in an array.  Int lastNumber = numbers[numbers.length - 1]; Two-Dimensional Arrays  Arrays have a dimension (the level or complexity of arrays).  The arrays described so far are of a single level, and can be described as ‘1-dimensional array.’  Grouping a number of Arrays together would add another level of co
More Less

Related notes for Computer Science 1026A/B

Log In


Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.