310 likes | 417 Views
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:.
E N D
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: Curly braces { }
Example of a Java Program Also notice: Curly braces { } Parentheses ( )
Example of a Java Program Also notice: Square brackets [ ] Curly braces { } Parentheses ( )
Example of a Java Program Also notice: A pair of braces { } define a block
primitive integral boolean floating point byte char short int long float double Java Primitive Data Types
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 byteshortintlongfloatdouble 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
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
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
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
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
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.
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.
Arrays (Cont’d) double[ ] salesAmt;salesAmt = new double[6]; salesAmt salesAmt [ 0 ] salesAmt [ 1 ] salesAmt [ 2 ] salesAmt [ 3 ] salesAmt [ 4 ] salesAmt [ 5 ]
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
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
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.
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.
Class Definition • A class definition is composed of two parts: • Data members (Data part) • Methods (Operations part) • Example: define a class Employee
- Object Creation Declaration Creation 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
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:
Simple Example of Static members static field static method
The end (or .. the beginning !)