31
Review of ICS 102

Review of ICS 102

Embed Size (px)

DESCRIPTION

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:. - PowerPoint PPT Presentation

Citation preview

Page 1: Review of ICS 102

Review of ICS 102

Page 2: 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!

Page 3: Review of ICS 102

Example of a Java Program

Class nameMain method

Instruction

Class body

Page 4: Review of ICS 102

Example of a Java Program

Also notice:

Curly braces } {

Page 5: Review of ICS 102

Example of a Java Program

Also notice:

Parentheses ) (

Curly braces } {

Page 6: Review of ICS 102

Example of a Java Program

Also notice:

Parentheses ) (

Curly braces } { Square brackets] [

Page 7: Review of ICS 102

Example of a Java Program

Also notice:

A pair of braces } { define a block

Page 8: Review of ICS 102

Java Primitive Data Types

primitive

integral floating point

byte char short int long float double

boolean

Page 9: Review of ICS 102

Primitive types

Page 10: Review of ICS 102

April 20, 2023 ICS102: Expressions & Assignment 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 itbyteshortintlongfloatdouble

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; errordouble y = 6; okshort i = 14.3; errorfloat s = 9.2; ok

Assignment Compatibility

For these, you need type cast. next

slide

Page 11: Review of ICS 102

April 20, 2023 ICS102: Expressions & Assignment 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

Page 12: Review of ICS 102

Other Useful String Operators

Method Meaning

equals Checks if two strings are equal. (Use equalsIgnoreCase for case insensitive) str1.equals( str2 )

compareTo Compares the two strings. str1.compareTo( str2 )

substringExtracts the a substring from a string.

str1.substring( 1, 4 ) str1.substring( 5 )

trim Removes the leading and trailing spaces. str1.trim( )

toUpperCaseConverts a string to all caps string. (Use toLowerCase for all small)

str1.toUpperCase( )

Page 13: Review of ICS 102

Java API

Page 14: Review of ICS 102

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

Page 15: Review of ICS 102

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

Page 16: Review of ICS 102

Console Input

Page 17: Review of ICS 102

April 20, 2023 ICS102: while & do-while 17

Exercises

1. Write a Java program which computes the sum of all the odd numbers between 0 and 100.

2. Write a Java program which reads 20 numbers using a scanner and computes their average.

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

Page 18: Review of ICS 102

Exercises

1. Write a java program which gives the following output122333444455555

2. Write a java program which prints all the prime numbers less than 1000.

Page 19: Review of ICS 102

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

Page 20: Review of ICS 102

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.

Page 21: Review of ICS 102

double[ ] salesAmt;salesAmt = new double[6];

salesAmt [ 0 ]

salesAmt [ 1 ] salesAmt [ 2 ]

salesAmt [ 3 ]

salesAmt [ 4 ]

salesAmt [ 5 ]

salesAmt

Arrays (Cont’d)

Page 22: Review of ICS 102

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

Page 23: Review of ICS 102

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]

4 3 2 8 5 9 13 4 8 9 8 0row 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

Page 24: Review of ICS 102

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.

Page 25: Review of ICS 102

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.

Page 26: Review of ICS 102

Class Definition

A class definition is composed of two parts: Data members (Data part) Methods (Operations part)

Example: define a class Employee

Page 27: Review of ICS 102

- Object Creation

Declaration Creation

Question: What is the name of Employee e1?

How to change the name of Employee e1? … next slide ..

Page 28: Review of ICS 102

- 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

Page 29: Review of ICS 102

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:

Page 30: Review of ICS 102

Simple Example of Static members

static field

static method

Page 31: Review of ICS 102

The end(or .. the beginning !)