0% found this document useful (0 votes)
37 views

Lecture 01

Uploaded by

Sami Saad
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)
37 views

Lecture 01

Uploaded by

Sami Saad
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/ 48

CSCI133 – Programming II

(Object Oriented Programming)

Lecture 01:
Review of basic programming in Java.

Rabe Abdalkareem, PhD

Slides have originally been prepared by Rose Williams, Binghamton University and Kenrick
Mock, University of Alaska Anchorage
Object Oriented Programming
• Object Oriented Programming treats everything in
the world as objects (e.g., automobiles, universities,
people, computers).

• Each object has the ability to perform actions, and


those actions can have an impact on other objects.

• It is a programming methodology that views a


program as consisting of objects that interact with
one another by means of actions (called methods).

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 2


Object Oriented Programming
• Java is an object oriented programming (OOP)
language.
• Examples: String, Scanner classes, …

• All programming constructs in Java, including


methods, are part of a class.

• Objects of the same kind are said to have the


same type or be in the same class.

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 3


Java Program Structure
• A Java program:

• is made up of one or more classes (collection of


actions).

• a class contains one or more methods (action).

• a method contains program statements/instructions.

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 4


Problem Solving
■ The purpose of writing a program is to solve
a problem!

■ The general steps in problem solving are:


■ Understand the problem
■ Design a solution (find an algorithm)
■ Implement the solution (write the program)
■ Test the program and fix any problems

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 5


Java Program Structure
// comments about the class
Public class MyProgram
{
Class header
// comments about the method
Public static void main (String[] args)
{ Class body

System.out.println("This is my first program!");


}
}
MyProgram.java
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 6
Java Program Structure
// comments about the class
Public class MyProgram
{
// comments about the method
Public static void main (String[] args)
{
method header
method body
System.out.println("This is my first program!");
}
}
MyProgram.java
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 7
Java Program Structure
// comments about the class
Public class MyProgram
{
// comments about the method
Public static void main (String[] args)
{
System.out.println("This is my first program!");
}
}

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ MyProgram.java 8


‫‪Editing, compiling, and executing.‬‬

‫اﺳﺗﺎذ اﻟﻣﺎدة‪ :‬د‪ .‬رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬ ‫‪9‬‬


Syntax and Semantics
■ Syntax rules
■ define how we can put together symbols, reserved
words, and identifiers to make a valid program

■ Semantics
■ define what a statement means

■ A program that is syntactically correct is not necessarily


logically (semantically) correct

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 10


Three types of errors
■ Compile-time (syntax) errors

• The compiler will find syntax errors and


other basic problems
• An executable version of the program is not
created

■ Run-time errors
■A problem can occur during program
execution
■Causes the program to terminate abnormally
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 11
Three types of errors

■ Logical (semantic) errors


• A mistake in the algorithm
• Compiler cannot catch them
• A program may run, but produce incorrect
results

Who is responsible for discovering and fixing


these types of errors?
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 12
Identifiers in Java
■ are the words a programmer uses in a program
to name variables, classes, methods, …

■ Rules to create an identifier:


• can be made up of: letters, digits, the underscore
character (_), and the dollar sign ($)
• no limit on length
• cannot begin with a digit
• cannot be a reserved word
• Examples: class, const, continue, default, do, else,
float, for, if, implements, import

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 13


Built-in data types in Java

Assignment Compatibility:

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 14


Output & Input in Java
public class OutputExample {

public static void main(String[] args) {


System.out.print("hello");
System.out.print("you");

System.out.println("hello");
System.out.println("you");

System.out.println();

int price = 50; helloyouhello


System.out.print(price);
you
char initial = 'L’;
System.out.println(initial);
50L
}
‫}ﻲ‬
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 15
Console Input
Since Java 5.0, use the Scanner class

The keyboard is represented by the System.in object


import java.util.Scanner;
public class MyProgram
{
public static void main (String[] args)
{ Create an object of class Scanner
Scanner myKeyboard = new Scanner(System.in);
//. . .
Reads one word from the keyboard
String name = myKeyboard.next();
int age = myKeyboard.nextInt(); Reads an integer from the keyboard
//. . .
16
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬
To read from a Scanner
§ To read tokens, use a nextSomething() method
➦ nextBoolean(),
➦ nextByte,
➦ nextInt(),
➦ nextFloat(),
➦ nextDouble(),
➦ next(),
➦ nextLine()
➦ . . .

§ tokens are delimited by whitespaces (i.e., blank


spaces, tabs, and line breaks)
§ Note: no nextChar()
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 17
To read from a Scanner
import java.util.Scanner;

public class Id {

public static void main(String[] args) {

Scanner myKeyboard = new Scanner(System.in);


System.out.println("Your name:");
String name = myKeyboard.next();

System.out.println("Welcome " + name + " Enter your age:");


int age = myKeyboard.nextInt();

System.out.print("\nYour name is " + name);


System.out.print(" and Your age is " + name);
Your name:
} Jalal
Welcome Jalal Enter your age:
} 30

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ Your name is Jalal and Your age is Jalal 18
Example: Scanner Demo (1/2)
//********************************************************
// This program demonstrates how to read tokens from
// the console with the Scanner class
//********************************************************

import java.util.Scanner; // we need to import this class

public class ScannerDemo


{
public static void main(String[] args)
{
// let's declare our scanner
Scanner keyboard = new Scanner(System.in);
// let's ask the user for some input
System.out.println("Enter the number of pods followed by");
System.out.println("the number of peas in a pod:");
19
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬
Example: Scanner Demo (2/2)
// let's read the user input
int numberOfPods = keyboard.nextInt();
int peasPerPod = keyboard.nextInt();

// let’s do some calculations


int totalNumberOfPeas = numberOfPods*peasPerPod;

// let's display some output


System.out.print(numberOfPods + " pods and ");
System.out.println(peasPerPod + " peas per pod.");
System.out.println("The total number of peas = " +
totalNumberOfPeas);
}
} Enter the number of pods followed by
the number of peas in a pod:
3 2
3 pods and 2 peas per pod.
The total number of peas = 6
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 20
String Class
■ So far we have seen only primitive types

■ A variable can be either:


■ a primitive type (ex: int, float, boolean, …)
■ or a reference to an object (ex: String, Array, …)

■ A string of character :
■ is an object defined by the String class
■ delimited by double quotation marks ex: "hello", "a"

•System.out.print("hello"); // string of characters

•System.out.print('a'); // single character


‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 21
Declaring Strings
■ Declare a reference to a String object

String title;

■ Declare the object itself (the String itself)

title = new String("content of the string");

This calls the String constructor, which is


a special method that sets up the object

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 22


Declaring Strings
■ Because strings are so common, we don't have to use
the new operator to create a String object
String title;
title = new String("content of the string");

String title = new String("content of the string");

String title;
title = "content of the string";

String title = "content of the string";

■ These special syntax works only for strings


‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 23
Strings
■ Once a string is created, its value cannot be
modified (i.e., the object is immutable)
• cannot lengthen/shorten it
• cannot modify its content

■ The String class offers:


• the + operator (string concatenation)
•ex: String solution = "The answer is " + "yes";

• many methods to manipulate strings


•length() // returns the number of characters in a string
•concat(String str) // returns the concatenation of the string and str
•toUpperCase() // returns the string all in uppercase
•replace(char oldChar, char newChar) // returns a new string
// where all occurrences of oldChar have been replaced by newChar
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 24
‫‪String indexes start at zero‬‬

‫اﺳﺗﺎذ اﻟﻣﺎدة‪ :‬د‪ .‬رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬ ‫‪25‬‬


Example: Iterate through each character of a string

public class StringDemo {


public static void main(String[] args) {
String joke; J
a
joke = new String("Java is fun!");
v
a
for (int i = 0; i < joke.length(); i++) {
i
System.out.println(joke.charAt(i)); s
}
f
} u
n
}
!
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 26
An Example of using String Class
public class StringTest {
public static void main (String[] args) {
String string1 = new String ("A string");
String string2 = "";
String string3, string4, string5;

System.out.println("Content of string1: \"" + string1 + "\"");


System.out.println("Length of string1: " + string1.length());
System.out.println("Content of string2: \"" + string2 + "\"");
System.out.println("Length of string2: " + string2.length());

string2 = string1.concat(" abc");


string3 = string2.toUpperCase();
string4 = string3.replace('A', 'X');
string5 = string4.substring(3, 10);

System.out.println("Content of string2: \"" + string2+ "\"");


System.out.println("Content of string3: \"" + string3+ "\"");
System.out.println("Content of string4: \"" + string4+ "\"");
System.out.println("Content of string5: \"" + string5+ "\"");
}
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 27
}
An Example of using String Class
public class StringTest {
public static void main (String[] args) {
String string1 = new String ("A string");
String string2 = "";
String string3, string4, string5;

System.out.println("Content of string1: \"" + string1 + "\"");


System.out.println("Length of string1: " + string1.length());
System.out.println("Content of string2: \"" + string2 + "\"");
System.out.println("Length of string2: " + string2.length());
Content of string1: "A string"
Length ofabc");
string2 = string1.concat(" string1: 8
string3 = string2.toUpperCase();
Content of string2: ""
string4 = string3.replace('A', 'X');
Length of string2: 0
string5 = string4.substring(3, 10);
Content of string2: "A string abc"
Content of
System.out.println("Content of string3: "A+ STRING
string2: \"" string2+ ABC"
"\"");
System.out.println("Content
Content of of string4:
string3: \""
"X+ STRING
string3+ XBC"
"\"");
System.out.println("Content of string4: \"" + string4+ "\"");
Content of string5: "TRING X"
System.out.println("Content of string5: \"" + string5+ "\"");
}
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 28
}
Example of String Class
public class StringProcessingDemo {

public static void main(String[] args) {


String sentence = "I hate text processing!";
int position = sentence.indexOf("hate");
String ending = sentence.substring(position + "hate".length());

System.out.println("01234567890123456789012");

System.out.println(sentence);
System.out.println("The word \"hate\" starts at index " + position);
sentence = sentence.substring(0, position) + "adore" + ending;
System.out.println("The changed string is:");
System.out.println(sentence);

}
}

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 29


Example of String Class
public class StringProcessingDemo {

public static void main(String[] args) {


String sentence = "I hate text processing!";
int position = sentence.indexOf("hate");
String ending = sentence.substring(position + "hate".length());

System.out.println("01234567890123456789012");

System.out.println(sentence);
System.out.println("The word \"hate\" starts at index " + position);
01234567890123456789012
sentence = sentence.substring(0, position) + "adore" + ending;
System.out.println("The Ichanged
hate string is:");
text processing!
System.out.println(sentence);
The word "hate" starts at index 2
} The changed string is:
I adore text processing!
}
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 30
A note on comparing strings
• We cannot use the relational operators to compare
strings (<, ==, …)

• use the equals() method


• to determine if two strings have the same content

• ex: firstString.equals(secondString)

• returns a boolean:
• true if firstString has the same content as secondString
• false otherwise

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬


31
Just checking...
■ What does the following code produce, given the input
String: This is a test

//. . .
String inputLine;

Scanner myKeyboard = new Scanner (System.in);


System.out.println("Enter a line of text: ");
inputLine = myKeyboard.next();
myKeyboard.close();

System.out.println("The text you entered is: " + inputLine);


//. . .

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 32


Arithmetic Expressions
■ An expression is a combination of one or more operands
and their operators
■ Arithmetic operators:

Addition +
Subtraction -
Multiplication *
Division /
Remainder %

result = total + count / max - offset;

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 33


Associativity Rules
Precedence and

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 34


Just checking...
n What is the output of this program?
//. . .
int n, sum = 0;

for (n = 1; n < 10; n++)


{

System.out.println("n == " + n + " sum == " + sum);


//Above line is a trace.

sum = sum + n;

}
System.out.println("After loop");//trace
System.out.println("n == " + n + " sum == " + sum);//trace
System.out.println("1 + 2 + ...+ 9 + 10 == " + sum);
//. . .
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 35
Just checking...
n What is the output produced by the following?
//. . .
int number = 10;
while (number > 0) {

number = number - 2;

if (number == 4)
continue;

System.out.println(number);
}

System.out.println("The end.");
//. . .
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 36
Just checking...
n What is the output produced by the following?
import java.util.Scanner;

public class PowersOfTwo {


public static void main(String[] args) {

Scanner myKeyboard = new Scanner (System.in);


System.out.println("Enter a number: ");
int n = myKeyboard.nextInt();

int i = 0; // count from 0 to N


int powerOfTwo = 1; // the ith power of two

// repeat until i equals n


while (i <= n) {
System.out.println(i + " " + powerOfTwo);
powerOfTwo = 2 * powerOfTwo;
i = i + 1;
}
}
‫}ﻲ‬
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 37
Introduction to Arrays
• An array is an ordered list of elements of the same type.
Examples:
• 52 playing cards in a deck.
• 100 thousand students in an online class.
• 1 billion pixels in a digital image.

Array indices always start with zero


The entire array
has a single name Each value has a numeric index

a [0] a [1] a [2] a [3] a [4] a [5] a [6] a [7] a [8]

score 80.0 99.0 75.5 100 85.0 20.5 12.0 30.5 77.0

• This array holds 9 values that are indexed from 0 to 8.


• An array of size n is indexed from 0 to n-1
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 38
Declaring and Creating Arrays
• Declaring the reference:
type_of_elements[] name_of_array;

double[] scores; int[] marks;


char[] vowels; Date[] sentence;

• Creating the elements:


name_of_array = new type_of_elements[size];
scores = new double[4];

• declaration + creation:
double[] scores = new double[4];

• An array is declared and created in almost the same way that objects
are declared and created.
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 39
Initializing Arrays
• An array can be initialized when it is declared
double[] score = {80, 99.9, 75, 100, 85.5}

• The array length (size) is automatically set to the number


of values in the braces.
• Initializing array using for loop.
double[] mark = new int[20];
for(int i = 0; i < mark.length; i++)
mark[i] = 100;

• If you do not initialize the elements of an array, they will


automatically be initialized to the default value for their base
type.
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 40
Using the score Array in a Program
import java.util.Scanner;

public class ArrayOfScores {


/* Enter 5 scores:
80 99.9 75 100 85.5
* Reads in 5 scores and shows how much each
The highest score is 100.0
* score differs from the highest score. The scores are:
*/ 80.0 differs from max by 20.0
public static void main(String[] args){ 99.9 differs from max by 0.09999999999999432
Scanner keyboard = new Scanner(System.in); 75.0 differs from max by 25.0
double[] score = new double[5]; 100.0 differs from max by 0.0
int index; 85.5 differs from max by 14.5
double max;

System.out.println("Enter 5 scores:"); Every array has one


score[0] = keyboard.nextDouble();
max = score[0]; instance variable named
for (index = 1; index < score.length; index ++) { length
score[index] = keyboard.nextDouble();
if(score[index] > max) variable
max = score[index];
}
System.out.println("The highest score is " + max);
System.out.println("The scores are:");
for(index = 0; index < score.length; index++)
System.out.println(score[index] +
" differs from max by " + (max - score[index]));
}
}
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 41
Common Programming Error Made When Using Arrays

• Array Index Out of Bounds:


What is wrong?
double[] a = new double[10];
for (int i = 1; i <= 10; i++) No a[10] (and a[0] unused)
a[i] = 20.4;

• An out of bounds index will cause a program to


terminate with a run-time error message.
• ArrayIndexOutOfBounds exception is thrown.

• Uninitialized array:
What is wrong?
double[] a;
for (int i = 0; i < 9; i++)
Never created the array
a[i] = 2.5;
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 42
Multidimensional Arrays
• A one-dimensional array: stores a list of elements of simple type
(primitive or reference).
char[] a = new char[4]; v i e w

• A two-dimensional array: stores a list of elements, where each element


is a 1-D array of simple type.
char[][] aa = new char[4][9];
a

a[0] o n c e u p o n
aa[1][5]
a[1] a t i m e

a[2]
t h e r e w a s
a[3] a l i t t l e
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 43
Using the length Instance Variable

char[][] page = new char[30][100];

• The instance variable length does not give the total number of indexed
variables in a two-dimensional array.

• page.length is equal to 30
• page[0].length is equal to 100

char[][] page = new char[30][100];


int row, column;
page.length = 30
for(row = 0; row < page.length; row++)
for(column = 0; column < page[row].length; column++)
page[row][column] = 'Z';

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 44


Classes and Objects
• A class is a type and you can declare variables of a
class type.

• A value of a class is called an objects. An object


has 2 components:
• Data (instance variables) - descriptive
characteristics

• Actions (methods) - what it can do (or what


can be done to it)

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬


45
Declaration of Classes

• A class contains: int x, y;


char ch;
Data
declarations

• 1. data declarations
(instance variables)
Method
declarations
• 2. action declarations
(methods)

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬


46
Declaring Objects

• The new operator is used to create an object of


a class and associate it with a variable

• Example:
nameOfClass nameOfObject = new nameOfClass();

• nameOfClass nameOfObject ;

• nameOfObject = new nameOfClass();

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬


47
Next Class
Classes and object in Java

‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬


48

You might also like