Lecture 2
Lecture 2
Fall 2020/21
Object-Oriented Programming
CS-201, CS201
Lec. (2)
Objects and Classes
– A class is code that describes a particular type of object. It specifies the
data that an object can hold (the object's fields), and the actions that an
object can perform (the object's methods).
– You can think of a class as a code "blueprint" that can be used to create
a particular type of object.
Objects and Classes
• When a program is running, it can use the class to create, in
memory, as many objects of a specific type as needed.
keyboard Scanner
variable object
Writing a Class, Step by Step
• A Room object will have the following fields:
Room
length
width
height
setLength()
setWidth()
setHeight()
getLength()
getWidth()
getHeight()
getArea()
Writing the Code
public class Room Room
{
length
private double length; width
height
private double width; setLength()
} setWidth()
setHeight()
getLength()
getWidth()
getHeight()
getArea()
Access Modifiers
• An access modifier is a Java keyword that indicates how a field or method can
be accessed.
• public
– When the public access modifier is applied to a class member, the member can be
accessed by code inside the class or outside.
• private
– When the private access modifier is applied to a class member, the member cannot be
accessed by code outside the class. The member can be accessed only by methods that
are members of the same class.
Data Hiding
Data Hiding
• An object hides its internal, private fields from code that is outside the class
that the object is an instance of.
• Only the class's methods may directly access and change the object’s internal
data.
• Code outside the class must use the class's public methods to operate on an
object's private fields.
• Data hiding is important because classes are typically used as components in
large software systems, involving a team of programmers.
• Data hiding helps enforce the integrity of an object's internal data.
Return Room
Type
Access Method - width : double
Name - length : double
specifier
- height : double
+ setWidth(w : double) : void
+ setLength(len : double): void
+ getWidth() : double
public void setLength(double len) + getLength() : double
+ getArea() : double
}
Creating a Room object
Room r = new Room ();
r.setLength(10.0);
The r A Room object
variable holds width:
the address of address
the Room 0.0
object.
6-32
Writing the getLength Method
public class Room
{
private double length;
private double width;
public void setLength(double len)
{
length = len;
}
public double getLength()
{
return length;
}
}
6-33
public class Room
{
private double width;
private double length;
Height 10.0
Accessors and Mutators
public class Rectangle
{
private double width;
private double length;
Input Scanner
variable object
Uninitialized Local Reference Variables
• Reference variables can be declared without being initialized.
Room r1;
• This statement does not create a Room object, so it is an uninitialized local
reference variable.
• A local reference variable must reference an object before it can be used, otherwise a
compiler error will occur.
r1 = new Room();
Box Rectangle
variable object
More Examples
Your First Java Program
Processing a Java Program
Create/Modify Source Code
Result
• Runtime Errors
– Causes the program to abort
• Logic Errors
– Produces incorrect result
Comments
• /*
Mulitple Lines
• */ to comment multiple lines
import java.util.Scanner;
public class Addition {
public static void main (String[] args)
{
//II create a Scanner to obtain i nput from the command window
Scanner input = new Scanner (System.in);
System.out.print ("Enter first integer:");
int numberl = input.nextInt();
System.out.print("Enter second integer: ");
int number2 = input.nextInt();
int sum = numberl + number2;
System.out.printf("Sum= is %d%n", sum);
}
}
Java Data Types
char Character or small 1byte signed: -128 to 127
integer. unsigned: 0 to 255
int Integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
Input :
Rectangle Length , Rectangle Width.
Processing :
Area = Rect Length * Rect Width.
Output :
Print Out The area.
Working With Variable
5
Int length ; Length
Int width; 10
Int area; Width
50
Scanner input = new Scanner(System.in); area
Length = input.nextInt();
Width = input.nextInt();
The perimeter and area of the square are given by the following formulas:
perimeter = Side Length * 4
area = Side Length * Side Length
Input:
Square Side Length
Processing:
perimeter = Side Length * 4
area = Side Length * Side Length
Output:
Print Out The Perimeter and Area.
Arithmetic Operations
% Remainder 20 % 3 2
34
Precedence of arithmetic operations
• )4 + 3( * 2 + 1 = ?
• Examples :
++K , K++ k= K+1
--K , K-- K= K-1
Increment and Decrement Operators
• If the value produced by ++ or – – is not used in an expression, it does not matter whether it is a
pre or a post increment (or decrement).
• When ++ (or – –) is used before the variable name, the computer first increments (or decrements)
the value of the variable and then uses its new value to evaluate the expression.
• When ++ (or – –) is used after the variable name, the computer uses the current value of the
variable to evaluate the expression, and then it increments (or decrements) the value of the
variable.
x = 5;
Print(++x);
x = 5;
Print (x++);
special assignment statements
+= , -= , *= , /= , %=
• Example:
X +=5 ; means x = x + 5;
x *=10; means x = x * 10;
x /=5; means x = x / 5;
For Example : write a program that ask the user to Enter 3 integer numbers and print out their sum
and Average.
int main ()
{
int n1 , n2 , n3 ;
System.out.println("Please Enter 3 integer numbers “ );
n1= input.nextInt();
n2= input.nextInt();
n3= input.nextInt();
System.out.println(" The sum of the 3 number is “ || sum (n1, n2,n3) );
System.out.println(" The average of the 3 number is " || average (n1, n2,n3) );
}
Public static int sum (int num1 , int num2, int num3)
{
return num1+num2+num3;
}
Public static double average (int num1, int num2, int num3 )
{
return sum (num1 , num2 , num3)/3 ;
}
Scope of a variable
scope is the context within a program in which a variable is valid and can be used.
result = x + y;
return result; }
} } 128
Scope of a variable
Global variable: declared outside of every function definition.
– Can be accessed from any function that has no local variables with the same name. In case the function
has a local variable with the same name as the global variable ,
static Int z ;
int main ( )
{
{
}
}
int sum (int x , int y )
{
}
Scope of a variable
Static int x = 100 ;
int main ( )
{
int x= 10;
{
int z , x ;
z=100;
y=100;
x= 250;
System.out.println(" inner block " << x ;
}
}
Method Overloading:
Defining several methods within a class with the same name. As long as every Method has a
different signature
Method Signature:
The signature of a method consists of the following:
Method name
Formal parameter list
Examples:
public int Sum(int x, int y)
public int Sum(int x, int y , int z)
public double Sum(double x, double y, double z)
public int Sum(int x, double y)
public int Sum(double y, int x) 133
The following methods are incorrectly overloaded; the compiler generates an error:
Example (1): The following methods are incorrectly overloaded because they have the same
method name and same formal parameter lists:
Example (2): Changing the names of the formal parameters, does not allow overloading of the
previous counter-example:
Note that the method type and modifiers are not part of the overloading rules