review of ics 102

Post on 04-Jan-2016

50 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

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

TRANSCRIPT

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 nameMain method

Instruction

Class body

Example of a Java Program

Also notice:

Curly braces } {

Example of a Java Program

Also notice:

Parentheses ) (

Curly braces } {

Example of a Java Program

Also notice:

Parentheses ) (

Curly braces } { Square brackets] [

Example of a Java Program

Also notice:

A pair of braces } { define a block

Java Primitive Data Types

primitive

integral floating point

byte char short int long float double

boolean

Primitive types

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

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

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

Java API

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

Console Input

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.

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.

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.

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

salesAmt [ 0 ]

salesAmt [ 1 ] salesAmt [ 2 ]

salesAmt [ 3 ]

salesAmt [ 4 ]

salesAmt [ 5 ]

salesAmt

Arrays (Cont’d)

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

[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

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

top related