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

9 Copyright © 2005, Oracle. All rights reserved. Using Strings, String Buffer, Wrapper, and Text-Formatting Classes.
8 Copyright © 2005, Oracle. All rights reserved. Object Life Cycle and Inner Classes.
Arrays.
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
Strings In Java, strings are contained in an object that is an instance of the String class. String in java is the name of a class. That’s why it starts.
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.
Fundamental Programming Structures in Java: Strings.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
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.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
From C++ to Java A whirlwind tour of Java for C++ programmers.
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.
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.
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.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter 7: Characters, Strings, and the StringBuilder.
CHAPTER 9 Text Processing and More about Wrapper Classes Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
CSC Programming I Lecture 9 September 11, 2002.
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.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
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.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
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.
Review of Java1 Quick Review of ICS 102 Primitive and Reference Types Initializing Class Variables Defining Constructors How to Create a String How to.
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.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
 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)
Java Programming Language Lecture27- An Introduction.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Strings A String is a sequence of letters
Strings, StringBuilder, and Character
Array, Strings and Vectors
JavaScript: Functions.
Primitive Types Vs. Reference Types, Strings, Enumerations
MSIS 655 Advanced Business Applications Programming
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.
Java Programming Language
Strings in Java.
Visual Programming COMP-315
Outline Creating Objects The String Class The Random and Math Classes
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 How to Create a String How to Perform Operations on Strings 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

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

How to Create a String Assign a double-quoted constant to a String variable : Concatenate other strings: Use a constructor: String category = "Action"; String empName = firstName + " " + lastName; String empName = new String("Joe Smith");

How to Concatenate Strings Use the + operator to concatenate strings: You can concatenate primitives and strings: String.concat() is another way to concatenate strings. System.out.println("Name = " + empName); int age = getAge(); System.out.println("Age = " + age);

How to Perform Operations on Strings How to find the length of a string : How to find the character at a specific index : How to return a substring of a string : String str = "Comedy"; int len = str.length(); char charAt(int index); String str = "Comedy"; char c = str.charAt(1); String substring(int beginIndex,int endIndex); String str = "Comedy"; String sub = str.substring(2,4); int length();

How to Perform Operations on Strings (Cont’d) How to convert to uppercase or lowercase: How to trim whitespace: How to find the index of a substring : String toUpperCase(); String toLowerCase(); String caps = str.toUpperCase(); String trim(); String nospaces = str.trim(); int indexOf (String str); int lastIndexOf(String str); int index = str.indexOf("me");

How to Compare Two Strings Use equals() if you want case to count: Use equalsIgnoreCase() if you want to ignore case: Do not use ==. String passwd = connection.getPassword(); if (passwd.equals("fgHPUw")) … // Case is important String cat = getCategory(); if (cat.equalsIgnoreCase("Drama")) … // We just want the word to match

How to Produce Strings from Other Objects Use Object.toString(). Your class can override toString() : System.out.println() automatically calls an object’s toString() method: public Class Movie { … public String toString { return name + " (" + Year + ")"; } … Movie mov = new Movie( … ); System.out.println("Title Rented: " + mov);

What value is returned? // Using methods length, indexOf, substring String stateName = “Mississippi” ; stateName.length( ) stateName.indexOf(“is”) stateName.substring( 0, 4 ) stateName.substring( 4, 6 ) stateName.substring( 9, 11 )

What value is returned? (Cont’d) // Using methods length, indexOf, substring String stateName = “Mississippi” ; stateName.length( ) value 11 stateName.indexOf(“is”) value 1 stateName.substring( 0, 4 ) value “Miss” stateName.substring( 4, 6 )value “is” stateName.substring( 9, 11 ) value “pi”

Comparing Strings Method Parameter Returns Operation Performed Name Type equalsString boolean compareToString int Tests for equality of string contents. Returns 0 if equal, a positive integer if the string in the parameter comes before the string associated with the method and a negative integer if the parameter comes after it.

Arrays Declaring and instantiating an array The length of an array Manipulating the elements in an array Using an array to count frequencies 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 ]

Two-Dimensional Array In Java, actually, a two-dimensional array is itself a one-dimensional array of references to one-dimensional arrays.

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] int [ ] [ ] data = new int [ 6 ] [ 12 ] ; [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] Java Array Implementation data