1 / 31

Review of ICS 102

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!. Example of a Java Program. Class name. Main method. Class body. Instruction. Example of a Java Program. Also notice:.

Download Presentation

Review of ICS 102

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Review of ICS 102

  2. 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!

  3. Example of a Java Program Class name Main method Class body Instruction

  4. Example of a Java Program Also notice: Curly braces { }

  5. Example of a Java Program Also notice: Curly braces { } Parentheses ( )

  6. Example of a Java Program Also notice: Square brackets [ ] Curly braces { } Parentheses ( )

  7. Example of a Java Program Also notice: A pair of braces { } define a block

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

  9. Primitive types

  10. A value of any type in the following list can be assigned to a variable of any type that appears to the right of it byteshortintlongfloatdouble Note that as your move down the list from left to right, the range of allowed values for the types becomes larger int x = 5.8; error double y = 6; ok short i = 14.3; error float s = 9.2; ok Assignment Compatibility For these, you need type cast. next slide October 26, 2014 ICS102: Expressions & Assignment 10

  11. Type Casting A type cast takes a value of one type and produces a value of another type with an "equivalent" value int x = (int) 2.9; When type casting from a floating-point to an integer type, the number is truncated, not rounded: (int) 2.9 evaluates to 2, not 3 October 26, 2014 ICS102: Expressions & Assignment 11

  12. Other Useful String Operators

  13. Java API

  14. System.out.println • To write of the screen: System.out.println(“Hello World"); • It is possible to print more than one item: • A plus sign is used to connect more than one item System.out.println("The answer is " + 42); • Every invocation of println generates a new line after it finishes System.out.println(“Hello World”); System.out.println(“Hello World”); Package name Class name Method name The item to be printed on the screen Hello World Hello World

  15. Console Input Import instruction Create Scanner object Read a first integer and assign it to variable a Read a second integer and assign it to variable b

  16. Console Input

  17. Exercises Write a Java program which computes the sum of all the odd numbers between 0 and 100. Write a Java program which reads 20 numbers using a scanner and computes their average. Write a Java program which reads unknown number of integers using a scanner and counts the number of odd numbers and the number of even numbers. Assume the input integers are all positive. Use a negative number as a sentinel. October 26, 2014 ICS102: while & do-while 17

  18. Exercises 1. Write a java program which gives the following output 1 22 333 4444 55555 • Write a java program which prints all the prime numbers less than 1000.

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

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

  21. Arrays (Cont’d) double[ ] salesAmt;salesAmt = new double[6]; salesAmt salesAmt [ 0 ] salesAmt [ 1 ] salesAmt [ 2 ] salesAmt [ 3 ] salesAmt [ 4 ] salesAmt [ 5 ]

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

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

  24. Array exercises • Write an application that inputs 10 numbers, each between 10 and 100. As each number is read, display it only if it is not a duplicate of a number already read. • Given an array of integers, write a java code that allows to check if the array is “palindromic”. A palindromic array is a symmetric one: • For example the arrays 1 6 4 6 1 and 258852 are both palindromic, but the array 3753 is not.

  25. Exercises • Write a program that creates a two dimensional array, fills it using Scanner, and then prints the sum of every column. • Given a two-dimensional array, dataTable, of type double such that the rows can have different lengths. Write a code fragment that computes the average of each row and saves it in a single-dimension array of size corresponding to the rows of dataTable.

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

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

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

  29. Copy Constructor • A copy constructor is a constructor with a single argument of the same type as the class. • It creates an object which is an exact copy of the argument object • Example: • How to invoke a copy constructor:

  30. Simple Example of Static members static field static method

  31. The end (or .. the beginning !)

More Related