0% found this document useful (0 votes)
4 views31 pages

1st Lecture

The document outlines the objectives and structure of a CS 1312 course focused on procedural programming and object-oriented concepts. It details course topics including arrays, recursion, and file processing, along with assessment methods such as quizzes and exams. Additionally, it provides insights into array creation, access, and manipulation in Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views31 pages

1st Lecture

The document outlines the objectives and structure of a CS 1312 course focused on procedural programming and object-oriented concepts. It details course topics including arrays, recursion, and file processing, along with assessment methods such as quizzes and exams. Additionally, it provides insights into array creation, access, and manipulation in Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

%

Y
& —all ol Lo

:
uuuuuuuuuuuuuuuuuuuu

CS 1312

Lecture 1
Course Main Objective

To equip students with the core knowledge required to


develop a procedural program using a high-level
programming language alongside the basic knowledge and
understanding of structured programming paradigm.
Course Main Objective
*+ Recognize object-oriented concepts
* Demonstrate basic knowledge and understanding of structured
programming paradigm.
+ Use standard programming design and development principles on some
moderately complex problems.
* Use the command line and relevant IDEs for writing, formatting,
compiling, running, and debugging code.
Course Topics

T 1
2
Arrays and Vectors
Recursion
12
3
3 Files processing 4
4 Introduction to object-oriented concept 5
5 Classes and objects: the building blocks 6,7
6 Encapsulation and data hiding 8
7 Inheritance and code reusability 9,10
Assessment

Theoretical Practical
* Quizzes (15%) * Quizzes (15%)
= 3 quizzes throughout the semester. = 2 quizzes throughout the semester.

+ Mid-term Exam (20%) * Final practical exam (10%)


» Week 6 = Week 710

* Theoretical Final Exam (40%)


= Assigned by university
Assessment Timeline (Theoretical)

Day and Date


Quiz 1 Week 3 Sunday 16/9/1446 (16/3/2025)
Quiz 2 Week 5 Thursday 17/10/1446
(19/4/2025)
Midterm exam Week 6 (Lecture time)
Quiz 3 Week 8 Thursday 10/11/1446
(8/5/2025)
Final exam Week 11 or 12 Assigned by university
Resources

* Textbooks
* W. Savitch, JAVA: an introduction to problem solving and
programming, global edition. Philadelphia, PA: Pearson
Education, 2018.

* Blackboard
* Lecture slides
* Checking your marks
* Announcements
* Quizzes will be released in the theoretical part of the module in the blackboard. Don't
be mixed out.
Arrays and Vectors
Objectives

* Nature and purpose of an array


* Using arrays in Java programs
* Methods with array parameter
* Methods that return an array
* Array as an instance variable
Array Basics: Outline

* Creating and Accessing Arrays


* Array Details
* The Instance Variable length
* More About Array Indices
* Analyzing Arrays
Creating and Accessing Arrays

* An array is a special kind of object


* Think of as collection of variables of same type
* Creating an array with 7 variables of type double
double[] temperature = new double[7]; |

* To access an element use


* The name of the array
« An index number enclosed in braces
* Array indices begin at zero
Creating and Accessing Arrays

* Figure 7.1 A common way to visualize an array

Indices

il 1 2 3 4 5 6 -

P 32 30 25.7 26 34 31.5 29

————— The array temperature


temperature[5]

* Note sample program, listing 7.1


class ArrayOf Temperatures
Creating and Accessing Arrays
Enter 7 temperatures:
32
30
25.7
26
34
31.5
29
The average temperature is 29.7428
The temperatures are Sample
above average screen
above average output
below average
below average
above average
above average
below average
a nice week.
Array Details

* Syntax for declaring an array with new


Base_Tvpe[] Arrav_Name = new Base_Tvpe[Length] ;

* The number of elements in an array is its length


* The type of the array elements is the array's base type
Square Brackets with Arrays

* With a data type when declaring an array


int [] pressure;
* To enclose an integer expression to declare the length of the
array
pressure = new int [100];
* To name an indexed value of the array
pressure[3] = keyboard.nextint();
Array Details

* Figure 7.2 Array terminology


————— Array name
/"

temperature[n + 2]
Index (also cailed a subscript)

Indexed variable (also


temperaturefn + 2] called an array element, an
— element, or a subscripted
rd variable)
temeratur'el n+ 2 I Value of the indexed
variable {also calledan
,/_—— element of the array)
temperature(n + 2] = 32;
The Instance Variable length

* As an object an array has only one public instance variable


- Variable length
+ Contains number of elements in the array
* It is final, value cannot be changed
* Note revised code, listing 7.2
class ArrayOfTemperatures2
The Instance Variable length

How many temperatures do you have?


3
Enter 3 temperatures:
32
26.5
27
The average temperature is 28.5 Sample
The temperatures are screen
32,0 above average
output
26.5 below average
27.0 below average
Have a nice week.
More About Array Indices
* Index of first array element is 0
* Last valid Index is arrayName.length — 1
* Array indices must be within bounds to be valid
* When program tries to access outside bounds, run
time error occurs
* OK to "waste" element 0
* Program easier to manage and understand
* Yet, get used to using index 0
Initializing Arrays

* Possible to initialize at declaration time


double[] reading = {3.3, 15.8, 9.7};

* Also may use normal assignment statements


* One at atime
* Inaloop

int[] count = new int[100];


for (int i = 0; i < 100; d++)
count[i] = 0;
Indexed Variables as Method Arguments

* Indexed variable of an array


* Example ... a[i]
+ Can be used anywhere variable of array base type can be used
* View program using indexed variable as an argument, listing 7.5
class ArgumentDemo
Entire Arrays as Arguments

* Declaration of array parameter similar to how an array is


declared
* Example:

public class SampleClass


{
public stavic void incrementArragBy2 (double[] anArray)
{
for (int i = 0; i < anArray.length; i++)
anArray[i] = anarray[i] + 2;
}
<The rest of the class definition goes hare.>
1
Entire Arrays as Arguments

* Note — array parameter in a method heading does not specify


the length
+ An array of any length can be passed to the method
* Inside the method, elements of the array can be changed
* When you pass the entire array, do not use square brackets in
the actual parameter
Arguments for Method main

* Recall heading of method main


public static void main (String[] args)
* This declares an array
* Formal parameter named args
+ Its base type is String
* Thus possible to pass to the run of a program multiple strings
* These can then be used by the program
Array Assignment and Equality

* Arrays are objects


+ Assignment and equality operators behave (misbehave) as specified in
previous chapter
* Variable for the array object contains memory address of the
object
+ Assignment operator = copies this address
+ Equality operator == tests whether two arrays are stored in same
place in memory
Array Assignment and Equality

* Two kinds of equality


* View example program, listing 7.6
class TestEquals

Sample
screen

Not equal by ==. output

Equal by the equals method.


Array Assignment and Equality

* Note results of ==
* Note definition and use of method equals
* Receives two array parameters
+ Checks length and each individual pair of array elements
* Remember array types are reference types
Summary

* An array is a collection of variables all of the same type


* Arrays are objects, created with operator new
* Elements numbered starting with 0, ending with 1 less than
length
* Indexed variable can be used as a parameter — treated like
variable of base type
Summary

* Entire array can be passed as parameterto a method


* Method return value can be an array
Questions

30
Z'S

Sl n||p[r| cola
UMM AL-QURA UNIVERSITY

Thank You

You might also like