Review of Java1 Quick Review of ICS 102 Primitive and Reference Types Initializing Class Variables Defining Constructors How to Create a String How to.

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.
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
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.
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.
Java Syntax Primitive data types Operators Control statements.
1 Strings and String Operations Overview l Creating String Objects l Substring methods l The Concatenation Operator l Strings are Immutable l Other Methods.
©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.
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.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
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.
Great Video Game Crash of Napoleon’s Russian Campaign,
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
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.
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.
Java Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[]
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.
Chapter 7: Characters, Strings, and the StringBuilder.
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.
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.
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.
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.
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.
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.
Strings, StringBuilder, and Character
Array, Strings and Vectors
Object Oriented Programming (OOP) LAB # 8
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.
Introduction To Programming Information Technology , 1’st Semester
Object-Oriented Programming
Java Programming Language
Visual Programming COMP-315
Outline Creating Objects The String Class The Random and Math Classes
Names of variables, functions, classes
Presentation transcript:

review of Java1 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 Inheritance in Java

review of Java2 Java Primitive Data Types primitive integralfloating point byte char short int long float double boolean

review of Java3 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

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

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

review of Java6 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.

review of Java7 The this Reference Instance methods receive an argument called this, which refers to the current object. 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 ” this

review of Java8 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?

review of Java9 title rating Class Variables l Class variables belong to a class and are common to all instances of that class. l Class variables are declared as static in class definitions. l Class variables belong to a class and are common to all instances of that class. l 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

review of Java10 Initializing Class Variables l Class variables can be initialized at declaration. l Initialization takes place when the class is loaded. l Class variables can be initialized at declaration. l 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;

review of Java11 Class Methods l Class methods are shared by all instances. l Useful for manipulating class variables: l Call a class method by using the class name or an object reference. l Class methods are shared by all instances. l Useful for manipulating class variables: l 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);

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

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

review of Java14 How to Concatenate Strings l Use the + operator to concatenate strings: l You can concatenate primitives and strings: String.concat() is another way to concatenate strings. l Use the + operator to concatenate strings: l 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);

review of Java15 How to Perform Operations on Strings l How to find the length of a string : l How to find the character at a specific index : l How to return a substring of a string : l How to find the length of a string : l How to find the character at a specific index : l How to return a substring of a string : int length(); 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);

review of Java16 How to Perform Operations on Strings l How to convert to uppercase or lowercase: l How to trim whitespace: l How to find the index of a substring : l How to convert to uppercase or lowercase: l How to trim whitespace: l 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");

review of Java17 How to Compare Two Strings Use equals() if you want case to count: Use equalsIgnoreCase() if you want to ignore case: Do not use ==. 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

review of Java18 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: 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);

review of Java19 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 )

review of Java20 What value is returned? // 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”

review of Java21 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.

review of Java22 Arrays l Declaring and instantiating an array l The length of an array l Manipulating the elements in an array l Using an array to count frequencies l Passing an array to a method

review of Java23 Arrays Arrays are data structures consisting of related data items all of the same type. l An array type is a reference type. Contiguous memory locations are allocated for the array, beginning at the base address of the array. l 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.

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

review of Java25 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

review of Java26 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]

review of Java27 Passing Arrays as Arguments l 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.

review of Java28 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

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

review of Java30 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

review of Java31 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

review of Java32 [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

review of Java33 [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 ]

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

review of Java35 [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

review of Java36 Inheritance in Java It is better to have a natural (hierarchical) way of organizing things. Important Note: Think in terms of “is a” relationships: An Employee is a Person, as is a Student. A Faculty is a(n) Employee. A Doctor is a Faculty. Person Employee Student Faculty Staff Doctor Lecturer Superclass Subclasses of Person Subclasses of Employee Subclasses of Faculty

review of Java37 More Classes and Subclasses Implement a super class, Person, and two subclasses for it, Student and Instructor. Person has two variables, name and year of birth. Student has an additional variable, major, while Instructor has an additional variable, salary. Write an appropriate constructor for each of the classes, making use of the constructor of the super class in defining those of the subclasses. Also write an appropriate toString method for each class, making use of the toString method of the super class in defining those of the subclasses. Write an application, TestInheritance, that creates an object of each of the classes in (a) above and prints it. You may use any data values of your choice in creating the objects. Person Student Instructor

review of Java38 Classes and Subclasses public class Problem6 { public static void main(String[] args) { Person person = new Person("Adil", 1959); Student student = new Student("Amr", 1979, "Computer Science"); Instructor instructor= new Instructor("Bashir", 1960, 65000); System.out.println(person); System.out.println(student); System.out.println(instructor); } class Person { private String name; private int birthYear; public Person(String name, int birthYear) { this.name = name; this.birthYear = birthYear ; } public String toString() { return "Name: " + name + ", BirthYear: " + birthYear; } class Student extends Person { private String major; public Student(String name, int birthYear, String major) { super(name, birthYear); this.major = major; } public String toString() { return super.toString() + ", Major: " + major; } class Instructor extends Person { private double salary; public Instructor(String name, int birthYear, double salary) { super(name, birthYear); this.salary = salary; } public String toString() { return super.toString() + ", Salary: " + salary; }