Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Review of Java1 Quick Review of ICS 102 Primitive and Reference Types Initializing Class Variables Defining Constructors How to Create a String How to."— Presentation transcript:

1 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

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

3 review of Java3 Integral Types Type Size in Bits Minimum Value to Maximum Value byte 8 -128 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

4 review of Java4 Type Size in Bits Range of Values float32 +1.4E - 45 to +3.4028235E+38 double64 +4.9E - 324 to +1.7976931348623157E+308 Floating Point Types

5 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;

6 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.

7 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

8 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?

9 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

10 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;

11 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);

12 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

13 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");

14 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);

15 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);

16 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");

17 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

18 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);

19 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 )

20 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”

21 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.

22 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

23 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.

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

25 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 0.0 0.0 0.0 0.0 0.0

26 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] 40 13 20 19 36

27 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.

28 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

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

30 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

31 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

32 review of Java32 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 4 3 2 8 5 9 13 4 8 9 8 0 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

33 review of Java33 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 4 3 2 8 5 9 13 4 8 9 8 0 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 ]

34 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.

35 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

36 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

37 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

38 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; }


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

Similar presentations


Ads by Google