COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Interfaces A Java interface is a collection
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Chapter 4: Writing Classes
Chapter 3, More on Classes & Methods Clark Savage Turner, J.D., Ph.D. Copyright 2003 CSTurner, from notes and text.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 14, 2005.
COMP 10 Introduction to Programming Mr. Joshua Stough October 29, 2007.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Programming with Collections Collections in Java Using Arrays Week 9.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP Introduction to Programming Adrian Ilie July 13, 2005.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
COMP 110 Introduction to Programming Mr. Joshua Stough October 1, 2007.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 18, 2005.
Class design. int month; int year class Month Defining Classes A class contains data declarations (state) and method declarations (behaviors) Data declarations.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004.
COMP 14 Introduction to Programming Miguel A. Otaduy June 1, 2004.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
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.
Chapter 8: Arrays.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
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.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
CS 106 Introduction to Computer Science I 10 / 29 / 2007 Instructor: Michael Eckmann.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
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.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
COMP 14 Introduction to Programming Mr. Joshua Stough March 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Chapter VII: Arrays.
Chapter 4: Writing Classes
Java Programming: Guided Learning with Early Objects
Method Mark and Lyubo.
CSCI 1100/1202 April 10,
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Classes, Encapsulation, Methods and Constructors (Continued)
MSIS 655 Advanced Business Applications Programming
Corresponds with Chapter 5
Presentation transcript:

COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007

Today Review Reserved word this Arrays

Announcements HW6

Review Scope public class Rectangle { // variables declared here are class-level // available in all methods in Rectangle class public int computeArea() { // variables declared here are method-level // only available in computeArea() } public void print() { // variables declared here are method-level // only available in print() }

The Reference this Reserved word Refers to instance variables and methods of a class Allows you to distinguish between member variables and local variables with the same name

Rectangle.java public class Rectangle { private int length; private int width; public Rectangle (int length, int width) { this.length = length; this.width = width; }

Reference Variables as Parameters If a formal parameter is a reference variable: –copies value of corresponding actual parameter –value of actual parameter is address of object where actual data is stored –both formal and actual parameter refer to same object

Review Overloading Methods Overloading - the process of using the same method name for multiple methods The signature of each overloaded method must be unique –number of parameters –type of the parameters –not the return type of the method, though The compiler determines which version of the method is being invoked by analyzing the parameters

Review public Rectangle (int l, int w) { length = l; width = w; } public class Rectangle { private int length; private int width; Rectangle r2 = new Rectangle (5, 10); public Rectangle () { length = 0; width = 0; }

Review A method should be relatively small –it should be able to be understood as a single entity –its name should fully describe its function A potentially large method should be decomposed into several smaller methods as needed for clarity A service method of an object may call one or more support methods to accomplish its goal

Thought Exercise Write a method for the Rectangle class called printBox that will print the rectangle as a box made of % example: length = 3, width = 5 %%% % %%%

public void printBox () { for (int i = 1; i <= width; i++) System.out.print("%"); System.out.println( ); for (int i = 1; i <= length - 2; i++) { System.out.print("%"); for (int j = 1; j <= width - 2; j++) System.out.print(" "); System.out.println("%"); } for (int i = 1; i <= width; i++) System.out.print("%"); System.out.println( ); } %%%% % % %%%% length = 3, width = 8

The toString Method Special method in Java classes Produces a String object based on the current object, suitable for printing Mapped to the '+' operator Also called when the object is a parameter in a print() or println() method There is a default toString method, but it's better if we write our own

Rectangle.java public String toString() { String result = ""; result += "length: " + length + "\n"; result += "width: " + width; return (result); } Rectangle r = new Rectangle (2,3); System.out.println (r); length: 2 width: 3

The Modifier static In the method heading, specifies that the method can be invoked by using the name of the class –no object has to be created in order to use the method –can't call a non-static method from a static method –can't access non-static variables from a static method If used to declare data member, data member invoked by using the class name –no object has to be created in order to use the variables

static Variables Shared among all objects of the class Memory created for static variables when class is loaded –memory created for instance variables (non-static) when an object is instantiated (using new) If one object changes the value of the static variable, it is changed for all objects of that class

Illustrate Class public class Illustrate { private int x; public static int y; private static int count; public Illustrate() { x = 0; } public Illustrate (int a) { x = a; } public static void incrementCount() { count++; } Illustrate obj1 = new Illustrate(3); x 3 x 5 0 y count 0 obj1 obj2 Illustrate.incrementCount(); 1 1 Illustrate obj2 = new Illustrate(5); Illustrate.y++;

Arrays An array is a list of values that can be represented by one variable Members of an array must all have the same data type Each value is stored at a specific, numbered position in the array –the number corresponding to each position is called an index or subscript All arrays have a length –number of elements the array can hold 01 23

Declaring Arrays type[] name; The array (element) data type Empty square brackets The array (variable) name Creates a reference variable called name that can point to an array of type elements

Declaring Arrays Examples // array of characters char[] characterSet; // array of counters (integers) int[] counter; // array of grades (doubles) double[] grade; counter characterSet grade 01 23

Instantiating Arrays You must instantiate (create) arrays –the size of an array is typically not known before run time name = new type[size]; The array (element) data type The new operator The array (variable) name The assignment operator The number of elements 01 23

Instantiating Arrays Examples // instantiate an array of counters counter = new int[5]; // instantiate the array of grades numStudents = 10; grade = new double[numStudents]; counter <= index < size 01 23

Declaration and Instantiation type[] name = new type[size]; DeclarationInstantiation 01 23

Arrays of Objects Can use arrays to manipulate objects Create array of objects Must instantiate each object in array classname[] array = new classname[size]; for(int j=0; j <array.length; j++) { array[j] = new classname(); }

Example int[] num = new int[5]; 01 23

Array Access Examples averageScore = (score[0]+score[1]+score[2])/3; numStudents = 3; totalScore = 0; for (int i = 0; i < numStudents; i++) { totalScore += score[i]; } averageScore = totalScore/numStudents; double score[] = new double[3]; score[0] = 98.3; score[1] = 57.8; score[2] = 93.4; often use loops for access 01 23

Array Length Arrays have length –an internal variable called length –number of elements in array –access the length variable using the “dot’ notation (arrayname. length) // loop through the array of test scores sumOfScores = 0; for (int i=0; i<scores.length; i++) { sumOfScores += scores[i]; } 01 23

int counter[] = {0, 0, 0, 0, 0}; char[] characterSet = {‘a’,’b’,’c’}; // etc. Initializing Arrays Array elements are variables too! –if you don’t initialize, the contents are undefined When and how? –if you don’t yet know the size initialize at run time, typically with a loop –if you know how many elements perhaps use an initializer list 01 23

Lists the initial value for the elements of an array Items are separated by commas and the list is in braces {} The size of the array is determined by the number of items in the list int[] scores = {87, 98, 45}; Can only be used in the same statement as declaring the array NOTint[] scores; scores = {87, 98, 45}; Initializer Lists 01 23

Next Time in COMP 110 Arrays Reading Assignment: Ch 9