Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.

Slides:



Advertisements
Similar presentations
Chapter 21 Implementing lists: array implementation.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
8 Copyright © 2005, Oracle. All rights reserved. Object Life Cycle and Inner Classes.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
Arrays.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Lecture 6 b Last time: array declaration and instantiationarray declaration and instantiation array referencearray reference bounds checkingbounds checking.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
Introduction to arrays Data in economy size packages.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Java Classes Introduction and Chapter 1 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
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.
1 Chapter 11 One-Dimensional Arrays. 2 Chapter 11 Topics l Atomic and composite data types l Declaring and instantiating an array l The length of an array.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Chapter 8: Arrays.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Section 3 - Arrays and Methods. Arrays Array: collection of group of data variables of same type, sharing the same name for convenience - Easy to search.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
Java Classes Introduction. Contents Introduction Objects and Classes Using the Methods in a Java Class – References and Aliases Defining a Java Class.
Review of Java1 Quick Review of ICS 102 Primitive and Reference Types Initializing Class Variables Defining Constructors How to Create a String How to.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
 2005 Pearson Education, Inc. All rights reserved Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Programming in C++ Dale/Weems/Headington Chapter 11 One-Dimensional Arrays.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Objects as a programming concept
3 Introduction to Classes and Objects.
Intro To Classes Review
Chapter 3 Introduction to Classes, Objects Methods and Strings
Programs and Classes A program is made up from classes
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
Arrays in Java.
Java Programming Language
Presentation transcript:

Review of ICS 102

Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS 201!

Quick Review of ICS 102 Primitive and Reference Types Initializing Class Variables Defining Constructors Arrays

Java Primitive Data Types primitive integral floating point byte char short int long float double boolean

Integral Types Type Size in Bits Minimum Value to Maximum Value byte to 127 short16 -32,768 to 32,767 int32 -2,147,483,648 to 2,147,483,647 long64 -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

Type Size in Bits Range of Values float E - 45 to E+38 double E to E+308 Floating Point Types

Class Definition A class definition is composed of two parts:  Data members (Data part)  Methods (Operations part) Example: define a class Employee

- Object Creation DeclarationCreation Question: What is the name of Employee e1? How to change the name of Employee e1? … next slide..

- Accessor and Mutator Methods (Example) Accessor method for instance variable name Mutator method for instance variable name Modifying the name of e1 using a mutator method

Simple Initialization of Instance Variables Instance variables can be initialized at declaration. Initialization happens at object creation. More complex initialization should be placed in a constructor. public class Movie { private String title; private String rating = "G"; private int numOfOscars = 0;

Defining Constructors public class Movie { private String title; private String rating = "PG"; public Movie() { title = "Last Action … "; } public Movie(String newTitle) { title = newTitle; } Movie mov1 = new Movie(); Movie mov2 = new Movie("Gone … "); Movie mov3 = new Movie("The Good … "); The Movie class now provides two constructors.

The this Reference Instance methods receive an argument called this, which refers to the current object. public class Movie { public void setRating(String newRating) { this.rating = newRating; } void anyMethod() { Movie mov1 = new Movie(); Movie mov2 = new Movie(); mov1.setRating("PG"); … title: null rating: null mov2 mov1 title : null rating: “ PG ”

Sharing Code Between Constructors public class Movie { private String title; private String rating; public Movie() { this("G"); } public Movie(String newRating) { rating = newRating; } A constructor can call another constructor by using this(). Movie mov2 = new Movie(); What happens here?

title rating Class Variables Class variables belong to a class and are common to all instances of that class. Class variables are declared as static in class definitions. public class Movie { private static double minPrice; // class var private String title, rating; // inst vars title rating title rating minPrice Movie class Movie objects

Initializing Class Variables Class variables can be initialized at declaration. Initialization takes place when the class is loaded. public class Movie { private static double minPrice = 1.29; private String title, rating; private int length = 0;

Class Methods Class methods are shared by all instances. Useful for manipulating class variables: Call a class method by using the class name or an object reference. public static void increaseMinPrice(double inc) { minPrice += inc; } Movie.increaseMinPrice(.50); mov1.increaseMinPrice(.50);

Garbage Collection Memory management in Java is automatic When all references to an object are lost, it is marked for garbage collection. Garbage collection reclaims memory used by the object. Garbage collection is automatic. There is no need for the programmer to do anything

Arrays Declaring and instantiating an array The length of an array Manipulating the elements in an array Passing an array to a method

Arrays (Cont’d) Arrays are data structures consisting of related data items all of the same type. An array type is a reference type. Contiguous memory locations are allocated for the array, beginning at the base address of the array. A particular element in the array is accessed by using the array name together with the position of the desired element in square brackets. The position is called the index or subscript.

double[ ] salesAmt; salesAmt = new double[6]; salesAmt [ 0 ] salesAmt [ 1 ] salesAmt [ 2 ] salesAmt [ 3 ] salesAmt [ 4 ] salesAmt [ 5 ] salesAmt

Example Declare and instantiate an array called temps to hold 5 individual double values. double[ ] temps = new double[ 5 ] ; // declares and allocates memory temps[0] temps[1] temps[2] temps[3] temps[4] number of elements in the array indexes or subscripts

Using an initializer list in a declaration int[ ] ages = { 40, 13, 20, 19, 36 } ; for ( int i = 0; i < ages.length ; i++ ) System.out.println( “ages[ “ + i + “ ] = ” + ages[ i ] ) ; ages[0] ages[1] ages[2] ages[3] ages[4]

Passing Arrays as Arguments In Java an array is a reference type. What is passed to a method with an array parameter is the address of where the array object is stored. The name of the array is actually a reference to an object that contains the array elements and the public instance variable length.

public static double average ( int[ ] grades ) // Determines and returns the average grade in an array { int total = 0 ; for ( int i = 0 ; i < grades.length ; i++ ) total = total + grades[ i ] ; return (double) total / (double) grades.length ; ; } Passing an Array as Parameter

Declaration of Two-Dimensional Array EXAMPLES double[][] alpha; String[][] beta; int[][] data; DataType [ ] [ ] ArrayName; Array Declaration

Two-Dimensional Array Instantiation where each Expression has an integral value and specifies the number of components in that dimension TWO FORMS FOR DECLARATION AND INSTANTIATION int[][] data; data = new int[6][12]; OR int[][] data = new int[6][12]; ArrayName = new DataType [Expression1] [Expression2] ; Two-Dimensional Array Instantiation

Indexes in Two-Dimensional Arrays Individual array elements are accessed by a pair of indexes. The first index represents the element’s row, and the second index represents the element’s column. int[ ][ ] data; data = new int[6][12] ; data[2][7] = 4 ; // row 2, column 7

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] row 2, column 7 int [ ] [ ] data; data = new int [ 6 ] [ 12 ] ; data [ 2 ] [ 7 ] = 4 ; [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] data [2] [7] [ 5 ] Accessing an Individual Component

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] row 2 The length fields int [ ] [ ] data = new int [ 6 ] [ 12 ] ; data.length 6 gives the number of rows in array data data [ 2 ]. length 12 gives the number of columns in row 2 [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]