Download LECT#01

Document related concepts

Time series wikipedia , lookup

Transcript
ICS 201 Summer 2001
Instructor: Mustafa Ahmed AbuOsba.
Room No: 22- 124/6, Phone No: 1141
mailto:[email protected]
http://www.ccse.kfupm.edu.sa/~abuosba/
Home Page:
Office Hours: Saturday Monday, Wednesday :10:30-11:30
an appointment outside these hours.)
(Make
Text Book:
“Computing Concepts with Java 2 Essentials”,
Cay Horstmann, John Wiley, 2nd Edition, 2000.
Catalog Course Description:
Advanced Java programming concepts. Introduction to computer
graphics, Simple graphical user interfaces. Basic data
structures. Searching and sorting techniques. Survey of
computer science areas. Case studies and practice in developing
small scale programs.
review of Java
1
Grade Distribution:
Activity
Weight
Laboratory(total weight 25%)
Lab work(12 x 0.75%)
9%
Lab quizzes(3 x 3%)
9%
Homeworks(2 x 1%)
2%
Lab Project
5%
Lecture(total weight 75%)
Lecture Home Works
Lecture Quizzes (5 x 3%)
15%
Major EXAM 1 (9:10 am, Wednesday July 11, 2001, 22/135)
15%
Major EXAM 2 (9:10 am Saturday July 28, 2001, 22/135)
15%
Final (comprehensive)
30%
review of Java
2
Important Notes:
•Lectures and labs are integrated and they complement each other.
•Students must dedicate a reasonable amount of time for studying this course on
regular basis. Manage your time well and keep a record of all your programs and
notes for further use.
•To pass this course, the student must pass the lab-component of the course.
•Cheating in whatever form will not be taken lightly and could lead to damaging
consequences
•Lateness to class will not be tolerated. Attendance will be taken within the first
five minutes of the class.
•Each unexcused absence will attract a deduction of 0.5% of total grade. An official
excuse must be shown in one week following return to classes.
•Absence for more than 9 hours (Lectures & Labs) will result in a DN grade.
•Home works must be turned in within the stated time.
•No make-up quizzes & Lab Tests.
•If you work hard to understand the material in this course well, it will help you in
many other courses.
•Hard work and dedication are necessary ingredients for success in this course.
review of Java
3
Time table Summer session
Week #
SAT
Jun
Jun
Jul
SUN
Jun
MON
Jun
Jul
Jul
Jul
Jul
TUE
Jun
Jul
WED
Jun
Jul
Thursday
Jun
Jul
Friday
Jun
Jul
Jul
Jul
Jul
Jul
Jul
Jul
Jul
Jul
Jul
Jul
Jul
Jul
Jul
Jul
Jul
Jul
Jul
Jul
Jul
Jul
Jul
Jul
Aug
Aug
Aug
Aug
Aug
Aug
Aug
Aug
Aug
Aug
Aug
Aug
Aug
Aug
Aug
review of Java
Aug
Aug
Aug
Aug
Aug
Aug
Aug
Aug
4
Aug
Week 1 & 2
ICS 201 SUMMER SESSION (003-2001 )Lecture & Lab Schedule
Week
Lec #
Date
Topic
1
1
24/June
Review of ICS102 Programming Fundamentals
2
25/June
Java Packages & Program
3
26/June
Literate Programming using Documentation /Javadoc
4
27/June
Further OO Concepts I (Inheritance/ Polymorphism)
5
28/June
Further OO Concepts II
6
30/June
Abstract Classes
7
1/July
Interfaces
8
2/July
Inner Classes /API and Interfaces
9
3/July
GUI-Graphics Programming I
2
Event Handling
*
10
4/July
GUI-Graphics Programming II
Examples using Swing GUI Components Quiz1
11
7/July
GUI-Graphics Programming III
reviewManagers
of Java
More Swing GUI Components& Layout
5
Week 3 & 4
3
12
8/July
GUI-Graphics Programming IV
Creating Menu
4
13
9/July
Applets and
Advanced Graphics
14
10/July
Applets and
Advanced Graphics
15
11/July
16
14/July
Review of Major Examination 1
17
15/July
Introduction to Multithreading
Basic concept/ thread Life cycle, creating thread
*
18
16/July
Multithreading Application (Synchronization)
19
17/July
Developing a Multithreading Application
20
18/July
Introduction to Software Engineering
Quiz 2
21
21/July
Searching: Linear & Binary Search
review of Java
6
Week 5 & 6
5
*
22
22/July
Sorting: Insertion & Selection Sort
23
23/July
Sorting: Merge & Quick Sort
24
24/July
25
25/July
Basic Data Structures
Design Alternatives for Implementing Stacks, Queues, Lists.
Quiz 3
6
*
26
28/July
27
29/July
Review of Major Examination II
28
30/July
Stacks, Queues & Lists: Implementation & Assessment
29
31/July
Introduction to Databases
30
1/Aug
Database Design, SQL Quiz 4
31
4/Aug
Introduction to Data Communication Systems
review of Java
7
Week 7 & 8
7 32 5/Aug
Introduction to the OSI Model & Standards
33 6/Aug
Introduction to IP Addressing and Routing
34 7/Aug
Introduction to Artificial Intelligence (AI)
35 8/Aug
Introduction to Knowledge Representation & Expert systems.
Quiz 5
36 11/Aug
Introduction to Operating Systems I
8 37 12/Aug Introduction to Operating Systems II
38 13/Aug Review for Final Examination
*
39 14/
14-16 August Final examinations Begin xxxxxxxxx
40
review of Java
8
Quick Review of ICS 102
•Primitive and Reference Types
•Initializing Class Variables
•Defining Constructors
•Defining Constructors
•How to Create a String
•How to Perform Operations on Strings
•Arrays
•Inheritance in Java
review of Java
9
Java Primitive Data Types
primitive
integral
byte char
short
int
boolean
long
floating point
float
review of Java
double
10
Java Data Types
primitive
integral
boolean
byte char short int long
reference
floating point
float
array
interface
class
double
review of Java
11
Integral Types
Type
Size in Bits
Minimum Value to Maximum Value
byte
8
short
16
-32,768
int
32
-2,147,483,648 to 2,147,483,647
long
64
-9,223,372,036,854,775,808 to
-128
to
127
to
32,767
+9,223,372,036,854,775,807
review of Java
12
Floating Point Types
Type
Size in Bits
float
32
Range of Values
+1.4E- 45 to
+3.4028235E+38
double
64
+4.9E - 324 to
+1.7976931348623157E+308
review of Java
13
Simple Initialization of
Instance Variables


Instance variables can be initialized at declaration.
Initialization happens at object creation.
public class Movie {
private String title;
private String rating = "G";
private int numOfOscars = 0;

More complex initialization should be placed in a
constructor.
review of Java
14
Defining Constructors
public class Movie {
private String title;
private String rating = "PG";
public Movie() {
title = "Last Action …";
The Movie class now
provides two
constructors.
}
public Movie(String newTitle) {
title = newTitle;
}
Movie mov1 = new Movie();
}
Movie mov2 = new Movie("Gone …");
Movie mov3 = new Movie("The Good …");
review of Java
15
The this Reference
Instance methods receive an argument called this,
which refers to the current object.
public class Movie {
public void setRating(String newRating)
{
this
this.rating = newRating;
}
void anyMethod() {
Movie mov1 = new Movie();
Movie mov2 = new Movie();
mov1.setRating("PG"); …
mov1
title : null
rating: “PG”
mov2
title: null
rating: null
review of Java
16
Sharing Code Between
Constructors
public class Movie {
private String title;
private String rating;
A constructor
can call another
constructor by
using this().
public Movie() {
this("G");
}
What happens
public Movie(String
here?
newRating) {
rating = newRating;Movie mov2 = new Movie();
}
}
review of Java
17
Class Variables
Class variables belong to a class and
are common to all instances of that
class.
class
Movie {
public
Class
variables
are declared as static
private static double minPrice;
// class var
in class
definitions.
private
String
title, rating;
// inst vars

minPrice
Movie class
title
rating
title
rating
title
rating
Movie objects
review of Java
18
Initializing Class Variables


Class variables can be initialized at
declaration.
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;
review of Java
19
Class Methods


Class methods are shared by all instances.
Useful for manipulating class variables:
public static void increaseMinPrice(double inc) {
minPrice += inc;
}

Call a class method by using the class name
or an object reference.
Movie.increaseMinPrice(.50);
mov1.increaseMinPrice(.50);
review of Java
20
Garbage Collection



Memory management in Java is automatic
When all references to an object are lost, it is marked
for garbage collection.
Garbage collection reclaims memory used by the
object.
Garbage collection is automatic.
There is no need for the programmer
to do anything
review of Java
21
How to Create a String
Assign a double-quoted constant to a
String variable:

String category = "Action";
String empName = firstName + " " + lastName;

Concatenate other strings:
String empName = new String("Joe Smith");

Use a constructor:
review of Java
22
How to Concatenate Strings

Use the + operator to concatenate strings:
System.out.println("Name = " + empName);

You can concatenate primitives and
strings:
int age = getAge();
System.out.println("Age = " + age);

String.concat() is another way to
concatenate strings.
review of Java
23
How to Perform Operations on
Strings

How to find the length of a string:
int length();
char charAt(int index);

String str = "Comedy";
char c = str.charAt(1);
How to find the character at a specific index:
String substring
(int beginIndex,
int endIndex);

String str = "Comedy";
int len = str.length();
String str = "Comedy";
String sub =
str.substring(2,4);
How to return a substring of a string:review of Java
24
How to Perform Operations on
Strings

How to convert to uppercase or lowercase:
String toUpperCase();
String toLowerCase();

How to trim whitespace:
String trim();

String caps =
str.toUpperCase();
String nospaces =
str.trim();
How to find the index of a substring:
int indexOf (String str);
int lastIndexOf
(String str);
int index =
str.indexOf("me");
review of Java
25
How to Compare Two Strings

Use equals()if you want case to
count:
String passwd = connection.getPassword();
if (passwd.equals("fgHPUw"))… // Case is important

Use equalsIgnoreCase()if you want
to ignore case:
String cat = getCategory();
if (cat.equalsIgnoreCase("Drama"))…
// We just want the word to match

Do not use ==.
review of Java
26
How to Produce Strings from
Other Objects


Use Object.toString().
Your class can override toString():
public Class Movie {…
public String toString {
return name + " (" + Year + ")";
}…

System.out.println() automatically calls
an object’s toString() method:
Movie mov = new Movie(…);
System.out.println("Title Rented: " + mov);
review of Java
27
length method


Method length returns an int value
that equals the number of characters in
the string.
You must use dot notation and
parentheses in the call to method
length.
review of Java
28
indexOf method

Method indexOf searches a string to find a
particular substring, and returns an int value
that is the beginning position for the first
occurrence of that substring within the string.

The substring argument can be a literal String,
a String expression, or a char value.

If the substring could not be not found, method
indexOf returns value -1.
review of Java
29
substring method




Method substring returns a particular
substring of a string, but does not change the
string itself.
The first parameter is an int that specifies a
starting position within the string.
The second parameter is an int that is 1 more
than the ending position of the substring.
Positions of characters within a string are
numbered starting from 0, not from 1.
review of Java
30
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 )
review of Java
31
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”
review of Java
32
Comparing Strings
Method
Name
Parameter
Type
Returns
Operation Performed
equals
String
boolean
Tests for equality of string
contents.
compareTo
String
int
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.
review of Java
33
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
review of Java
34
Arrays
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.
review of Java
35
double[ ] salesAmt;
salesAmt = new double[6];
salesAmt
review of Java
36
double[ ] salesAmt;
salesAmt = new double[6];
salesAmt
salesAmt [ 0 ]
salesAmt [ 1 ]
salesAmt [ 2 ]
salesAmt [ 3 ]
salesAmt [ 4 ]
salesAmt [ 5 ]
review of Java
37
Example

Declare and instantiate an array called
temps to hold 5 individual double values.
number of elements in the array
double[ ] temps = new double[ 5 ] ;
// declares and allocates memory
0.0
temps[0]
0.0
0.0
temps[1]
temps[2]
indexes or subscripts
0.0
0.0
temps[3]
temps[4]
review of Java
38
Declaring and Allocating an Array

Operator new is used to allocate the specified number
of memory locations of the size needed for DataType.
SYNTAX FORMS
DataType[ ] ArrayName ;
// declares array
ArrayName = new DataType [ IntExpression ] ;
// allocates array
DataType[ ] ArrayName = new DataType [ IntExpression ] ;
review of Java
39
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 ] ) ;
40
ages[0]
13
ages[1]
20
19
ages[2]
ages[3]
36
ages[4]
review of Java
40
Passing Arrays as Arguments

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.
review of Java
41
Passing an Array as Parameter
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 ; ;
}
review of Java
42
More about array index

Array index can be an integral expression of type
char, short, byte, or int.

It is programmer’s responsibility to make sure that
an array index does not go out of bounds. The index
must be within the range 0 through the array’s length
minus 1.

Using an index value outside this range throws an
ArrayIndexOutOfBoundsException. Prevent this
error by using public instance method length.
review of Java
43
String[ ] groceryItems = new String[10];
groceryItems
[0]
“cat food”
[1]
“rice”
.
.
.
.
.
.
[8]
“spinach”
[9]
“butter”
review of Java
44
Declaration of Two-Dimensional Array
Array Declaration
DataType [ ] [ ] ArrayName;
EXAMPLES
double[][] alpha;
String[][] beta;
int[][] data;
review of Java
45
Two-Dimensional Array Instantiation
Two-Dimensional Array Instantiation
ArrayName = new DataType [Expression1] [Expression2] ;
where each Expression has an integral value
and specifies the number of components in that
dimension.
review of Java
46
Two-Dimensional Array Instantiation
Two-Dimensional Array Instantiation
ArrayName = new DataType [Expression1] [Expression2] ;
TWO FORMS FOR DECLARATION AND INSTANTIATION
int[][] data;
data = new int[6][12];
OR
int[][] data = new int[6][12];
review of Java
47
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
review of Java
48
Accessing an Individual Component
int [ ] [ ] data;
data = new int [ 6 ] [ 12 ] ;
data [ 2 ] [ 7 ] = 4 ;
[0]
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
[1]
[2]
row 2,
column 7
[3]
[4]
4
3
2
8
5
9 13
4
8
9
8
0
data [2] [7]
[5]
review of Java
49
The length fields
int [ ] [ ] data = new int [ 6 ] [ 12 ] ;
data.length
data [ 2 ]. length
[0]
6
gives the number of rows in array data
12
gives the number of columns in row 2
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
[1]
[2]
row 2
[3]
4
3
2
8
5
9
13
4
8
9
8
0
[4]
[5]
review of Java
50
Using the length field
int [ ] [ ] data = new int [ 6 ] [ 12 ] ;
for ( int i = 0; i < data [ 2 ]. length ; i++ ) // prints contents of row 2
System.out.println( data[ 2 ] [ i ] ) ;
[0]
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
[1]
[2]
row 2
[3]
4
3
2
8
5
9 13
4
8
9
8
0
[4]
[5]
review of Java
51
Two-Dimensional Array
In Java, actually, a two-dimensional array
is itself a one-dimensional array of
references to one-dimensional arrays.
review of Java
52
Using Initializer Lists
int [ ] [ ] hits = { {
{
{
{
2,
1,
1,
0,
1,
1,
0,
1,
0,
2,
0,
2,
3,
3,
0,
1,
2},
4},
0},
1} };
hits
[0]
[0]
2
[1] [2] [3] [4]
1
0
3
2
[1]
1
1
2
3
4
[2]
1
0
0
0
0
[3]
0
1
2
1
1
review of Java
53
Java Array Implementation
int [ ] [ ] data = new int [ 6 ] [ 12 ] ;
data
[0]
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
[1]
[2]
[3]
[4]
[5]
review of Java
54
Write a method using the two-dimensional stateHighs array
to fill a one-dimensional stateAverages array
final int NUM_STATES = 50 ;
int [ ] [ ] stateHighs = new int [ NUM_STATES ] [ 12 ] ;
int stateAverages [ NUM_STATES ] ;
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
Alaska
Arizona
62
85
[0]
[1]
[2]
.
.
.
[ 48 ]
[ 49 ]
43 42 50 55 60 78 80 85 81 72 63 40
66 64 72 78 85 90 99 105 98 90 88 80
review of Java
55
Inheritance in Java
It is better to have a natural (hierarchical) way of organizing things.
Superclass
Person
Employee
Faculty
Doctor
Student
Staff
Subclasses of Person
Subclasses of Employee
Lecturer
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.
review of Java
56
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
review of Java
57
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 Instructor extends Person {
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;
}
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;
}
}
}
review of Java
58