0% found this document useful (0 votes)
3 views54 pages

Lecture 02

The lecture covers the fundamentals of Object Oriented Programming (OOP) in Java, focusing on the concepts of classes and objects. It explains how classes define data (instance variables) and actions (methods), and illustrates these concepts with examples such as a Car class and a Pizza ordering system. The session also emphasizes the importance of creating and using custom classes to manage complexity in programming.

Uploaded by

zogaintersta
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)
3 views54 pages

Lecture 02

The lecture covers the fundamentals of Object Oriented Programming (OOP) in Java, focusing on the concepts of classes and objects. It explains how classes define data (instance variables) and actions (methods), and illustrates these concepts with examples such as a Car class and a Pizza ordering system. The session also emphasizes the importance of creating and using custom classes to manage complexity in programming.

Uploaded by

zogaintersta
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/ 54

CSCI133 – Programming II

(Object Oriented Programming)

Lecture 02:
Classes and Objects

Rabe Abdalkareem, PhD

Slides have originally been prepared by Rose Williams, Binghamton University and Kenrick
Mock, University of Alaska Anchorage
Last class: 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


Last class: 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


Last class: Predefined classes
• Programmers can:
• use pre-defined classes
• develop their own classes

• a class library is a collection of pre-defined classes

• the Java standard class library (or Java API -


Application Programming Interface) defines many
classes
• ex:
• the Scanner class
• the String class
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 4
Last class: Writing our own classes…
• So far, our program had only 1 class with 1 method (main)

public class MyProgram {


public static void main(String[] args)
{
// . . .
}
}

• For large problems…


• the program becomes large and difficult to understand
• e.g., repetition of instructions, variables are dispersed…
• Solution:
• Decompose the problem into sub-problems.
• Use methods to implement the sub-problems
• Group related variables and methods into classes
5
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬
Last class: 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);

}
}
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 6
Last class: 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);
01234567890123456789012
}
I hate text processing!
} The word "hate" starts at index 2
The changed string is:
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 7
I adore text processing!
In this lecture, we will see:
1. Writing our own classes
1.1 Classes and Objects
1.2 Instance Variables
1.3 Methods
2. Some notions of OOP
3. Passing and returning objects
4. Recap

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


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 object.

• An object has 2 components:


• Data (instance variables) - descriptive
characteristics.
• Actions (methods) - what it can do (or
what can be done to it).
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 9
Objects
• Once a new class is defined, objects, or
instances, can be created from this class.

• An object is often referred to as an instance


of the class.

• Each object can have different data, but all


objects have the same actions.

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


Declaration of Classes

• A class contains: int x, y;


Data
char ch;
declarations
• 1. data declarations
(instance variables)
Method
• 2. action declarations
declarations
(methods)

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


Example: A class definition
public class Date
{
public String month;
data declarations
public int day;
(instance variables)
public int year;

public boolean isWeekEnd()


{
// . . .
}
action declarations
public void printDate() (methods)
{
// . . .
}
} 12
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬
Exercise 1: Class Car
• Write a class, Car, that can be used to create
car objects.

• Each car has a make, model, color and year.

• Also create a printCar() that prints each of


the car’s instance variables.

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


Exercise 1: Class Car
public class Car
{
public String make;
public String model;
public String color;
public int year;

public void printCar()


{
System.out.println(make + " " + model + " "
+ color + " " + year);
}
}

But how can we use this class?


You need A driver file
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 14
Declaring Objects
1. An object of a class is named or declared by a
variable of the class type:
ClassName objectName;

2. The new operator is used to create an object of a


class and associate it with a variable

• Example:
objectName = new ClassName();
3. Or simple:
ClassName objectName = new ClassName();

Important Note: objectName is a pointer/reference to the object.


‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 15
Example: A driver file Toyota Corola Black 2020
public class CarFirstTryDemo { Kia Forta Red 1999
public static void main(String[] args) {
Car car1;
car1 = new Car(); declaration of 2 objects
Car car2 = new Car();

car1.make = "Toyota";
car1.model = "Corola";
car1.color = "Black";
car1.year = 2020;

car2.make = "Kia";
car2.model = "Forta"; usage of the object
car2.color = "Red";
car2.year = 1999;

car1.printCar();
car2.printCar();
} Q: How many instance variable and
} ‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ methods does our object have? 16
Class Definitions (Date example)
public class Date {

public String month;


public int day; Instance Variables
public int year;

public void printDate()


{
System.out.println(month + ""
+ day + ", " + year);
}

public void printMonth()


Methods
{
System.out.println(month + " "
+ day + ", " + year);
}
}

But how can we use this class? 17


Declaring Objects (Date example)
public class DateDemo {

public static void main(String[] args)


{
Class name Date dateOne, dateTwo;
dateOne = new Date(); declaration
Object name dateTwo = new Date(); of 2 objects
dateOne.month = "December";
dateOne.day = 20;
dateOne.year = 2019;

dateTwo.month = "July"; usage of the


dateTwo.day = 03;
dateTwo.year = 2020;
object

dateOne.printDate();
dateTwo.printDate();
}

} Important Note: dateOne and dateTwo are pointers/references to the objects. 18


Classes and Files
• In general, we store each class in its own file.

• Our example program has 2 files:

1. The class definition file:


• defines the data and methods of a class.

2. The driver file:


• contains the main method.
• declares and uses objects of the class.
• controls the use of other parts of a program.
• often used to test other parts of the program.
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 19
Just checking …
The new operator:

A. allocates memory
B. is used to create an object of a class
C. associates an object with a variable that names it.
D. all of the above.

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


Example: Pizza place
Example: write a program to manage a pizzeria place.
• Small pizza = $10 + $2 per topping
• Medium pizza = $14 + $2 per topping
• Large pizza = $17 + $2 per topping
Output

Which pizza you want to order: Large


How many topping you want to order: 3

_________________________
Your order is:

Size: Large
Topping: 3
Total Price: 23.0$

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


Example: Pizza place
public class Pizza {
public String size;
public int topping;
public double price;

public void getOrderInfo() {


if (size.equals("Small")) {
price = 10 + (2 * topping);
} else if (size.equals("Medium")) {
price = 14 + (2 * topping);
} else if (size.equals("Large")) {
price = 17 + (2 * topping);
}

System.out.println("\nSize: " + size + "\n" +


"Topping: " + topping + "\nTotal Price: " + price + "$");

}
}
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 22
Example: Pizza place
import java.util.Scanner;
public class PizzeDemo {

public static void main(String[] args) {


Pizza pizza = new Pizza();

Scanner input = new Scanner(System.in);


System.out.print("Which pizza you want to order: ");
pizza.size = input.next();

System.out.print("How many topping you want to order: ");


pizza.topping = input.nextInt();

System.out.println("_________________________");
System.out.println("Your order is:");
pizza.getOrderInfo();

}
}
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 23
In this lecture, we will see:
1. Writing our own classes
1.1 Classes and Objects
1.2 Instance Variables
1.3 Methods
2. Some notions of OOP
3. Passing and returning objects
4. Recap

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


Instance Variables
• Instance variables can be defined as in the
following examples.

public String month;


Note the public public int day;
modifier (for now):
public int year;

• variables/constants declared inside the class but


not inside a specific method.

• also called attributes.


25
Instance Variables
• In order to refer to a particular instance variable,
preface it with its object name as follows:

dateTwo.month = "July";
dateOne and dateTwo dateOne.day = 03;
are objects from the class
Date dateTwo.year = 2020;

• can be used by any method of the class.


• initialized to 0 (false for booleans).

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


Instance Variables
• Each object (instance) of the class has its own instance data.
public class Date { Class Date
public String month;
public int day;
public int year; month
Day
public void printDate(){//.} year
} Instance of
Instance of
Date dateOne, dateTwo; printDate()
dateOne = new Date();
dateTwo = new Date();

dateOne.month = "December"; Date : dateOne


dateOne.day = 20; Date : dateTwo
dateOne.year = 2019; December
month
July
month Day 20
dateTwo.month = "July";
Day 03 year
dateTwo.day = 03; 2019
dateTwo.year = 2020; year
2020 Objects
printDate()
dateOne.printDate();
printDate()
dateTwo.printDate();
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 27
In this lecture, we will see:
1. Writing our own classes
1.1 Classes and Objects
1.2 Instance Variables
1.3 Methods
2. Some notions of OOP
3. Passing and returning objects
4. Recap

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


Methods
• Group of statements that are given a name.

• Implement the behavior/actions of all objects of a


particular class.

• All objects of a class share the method definitions

• Some methods are a bit special…


• (ex: constructor)

• A method is defined once, but can be used


(called/invoked) several times.
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 29
Definition of a method
• Method header and method body
optional

visibility static returnType methodName(listOfParameters)


{
\\ statements of the method
}
method header
method body

public void sayHi()


{
System.out.print("Hi");
}

public static void main(String[] args)


{
// . . .
}

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


Methods continued…
• There are two kinds of methods:

• Methods that compute and return a value

• Methods that perform an action


• does not return a value
• is called a void method

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


Methods that return a result
• Must specify the type of that value in its heading:
public typeReturned methodName(paramList)

• Examples:
• description: determines if the coin is a tail (1==tail, 0==heads)
• name: isTail
• result: boolean
public boolean isTail()
{
if (face == 1)
return true;
else
return false;
}

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


Methods that return no result
• They perform an action performed, but do not “evaluate” to
a value
• ex: they display something, change the value of an attribute, …

• They officially return void


• They use no return expression;
• or: return;
public void flip()
{
face = (int)(Math.random()*2);
}
public static void main(String[] args)
{
// . . .
}
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 33
Class Definitions (Date example)
public class Date {

public String month;


public int day;
public int year;

method
public void printDate()
{
System.out.println(month + "
" + day + ", " + year);
}

public void printMonth()


method
{
System.out.println(month + "
" + day + ", " + year);
}

34
Class Definitions (Date example)
public void setDate(String monthStr, int dayInt, int yearInt){
if (dateOk(monthStr, dayInt, yearInt)){
day = dayInt;
month = monthStr; method
year = yearInt;
}
}

public boolean dateOk(String monthStr, int dayInt, int yearInt){


return (monthOk(monthStr) && (dayInt >= 1) && (dayInt <= 31) &&
(yearInt >= 2000) && (yearInt <= 2019));
} method
public boolean monthOk(String monthStr) {
return (monthStr.equals("January") || monthStr.equals("February") ||
monthStr.equals("March") || monthStr.equals("April") ||
monthStr.equals("May") || monthStr.equals("June") ||
method monthStr.equals("July") || monthStr.equals("August") ||
monthStr.equals("September") || monthStr.equals("October") ||
monthStr.equals("November") || monthStr.equals("December"));
}
} 35
Declaring Objects (Date example)
public class DateFirstTryDemo {

public static void main(String[] args) {


Date dateOne;
dateOne = new Date();

dateOne.setDate("December", 20, 2019);


dateOne.printDate();

if (dateOne.dateOk("December", 20, 2019)) {


System.out.println("The date is Ok");
} else {
System.out.println("The date is not correct");
}

}
} December 20, 2019
The date is Ok 36
Just checking …
• A class is a type and you can declare
variables of a class type.
• A value/instance 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)

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


Accessing Class Members
• To access any members (data or method)
• within the class
• nameOfData
• nameOfMethod(actualParameters)

• from outside the class


• non-static:
• nameOfObject.nameOfData
• nameOfObject.nameOfMethod(actualParameters)

• Static:
• We will cover this late…

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


Example (Coin class)
public class Coin {
public final int HEADS = 0;
public final int TAILS = 1;
public int face;

public void flip() {


face = (int) (Math.random() * 2);
}

public boolean isHeads() {


return (face == HEADS);
}

// flips the coin 5 times


public void flip5() {
for (int i = 1; i <= 5; i++)
flip();
}

}
Coin.Java
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 39
Example (Coin class)
public class FlipRace {

public static void main(String[] args) {


final int GOAL = 3;
int count1 = 0, count2 = 0; The first coint has 3 heads!
The second coint has 3 heads!
// Create two separate coin objects
Coin coin1 = new Coin();
Coin coin2 = new Coin();

// Flip the coins and count heads


while (count1<GOAL || count2<GOAL) {
coin1.flip();
coin2.flip();
if (coin1.isHeads()) count1++;
if (coin2.isHeads()) count2++;
}
System.out.println("The first coint has " + count1 + " heads!");
System.out.println("The second coint has " + count2 + " heads!");
}
}
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ FlipRace.Java 40
Just checking … Calling Methods
public class FlipRace {
public static void main(String[] args) {
Coin coin1 = new Coin();
boolean result;

coin1.flip(); // 1. OK? Yes


result = coin1.flip(); // 2. OK? No. Why??
if (coin1.flip()) // 3. OK? No Why??
System.out.print("whatever");
System.out.print(coin1.flip()); // 4. OK? No Why??
}
}
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 41
Just checking ….
Which one of these statements is syntactically correct?

Coin coin1 = new Coin();


boolean result;
/* 1 */ coin1.isHeads();
/* 2 */ result = coin1.isHeads();
/* 3 */ if(coin1.isHeads())
System.out.print("whatever");
/* 4 */ System.out.print(coin1.isHeads());

A. All are syntactically correct


B. 2 and 3 only are syntactically correct
C. 2, 3 and 4 only are syntactically correct
D. 1 is the only one which is syntactically correct
E. 3 is the only one which is syntactically correct

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


Just checking …

The body of a method that returns a value must contain


at least one _________ statement.

A. void
B. invocation statement
C. declaration
D. return

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


Local / Instance/ Global Variables
• Local variables:
• Declared inside a method
• variables that are necessary for that method only
• method parameters are considered local variables
• If 2 methods have a local variable of the same name, they are 2
entirely different variables

• Instance variables:
• Confined to an object of the class

• Global variables:
• Java does not have global variables

• Note: A variable declared within a block (braces {}) is local to that block,
and cannot be used outside the block.
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 44
public and private Modifiers
• The modifier public means that there are no restrictions
on where an instance variable or method can be used.

public String month; public int getMonth() {// Body}


public int day; public int getDay() {// Body}
public int year; public int getYear() {// Body}

• The modifier private means that an instance variable or


method cannot be accessed by name outside of the class.
private String month; private boolean isMonth() {// Body}
private int day; public boolean isDay() {// Body}
private int year; public boolean isYear() {// Body}

45
Parameters
• Some methods need to receive additional data in order to
perform their work

• Allows the function to be more generic


• ex. sqrt method works for any double
Math.sqrt(9), Math.sqrt(15.5), Math.sqrt(75),...

• Definitions:
• Formal parameter:
• parameter specified in the method header
• Argument:
• The value that is plugged in for the parameter

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


Example 1: Account
public class Account
{
private double balance;
public int accountNumber;

public void deposit(double theAmount) {


if (theAmount < 0) // deposit value is negative
System.out.println("Error: Deposit amount is invalid.");
else
balance = balance + theAmount;
}
public void printBalance()
{
System.out.println("The balance of the account " +
accountNumber + " is " + balance);
}
}
Account.java
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 47
Example 1: Account
public class Banking
{
public static void main (String[] args)
{
Account acct1 = new Account();
acct1.accountNumber = 12173093;
acct1.deposit(25.85);
acct1.deposit(10);
acct1.printBalance();
}
}
Banking.java

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


Just checking … Account ???
public class Banking
{
public static void main (String[] args)
{
Account acct1 = new Account();
acct1.accountNumber = 12173093;
acct1.Balance = 300;
acct1.printBalance();
}
}
Banking.java

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


Just checking …
In the code below, setPrice is a _____, and t is a _____.
public double setPrice(double t)
{
double taxRate = t * 0.0005;
return taxRate;
}

• field; field
• method; field
• method; parameter
• parameter; method

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


Class Employee: (1 of 2)
public class Employee {
private int id;
private String name;
private float salary;

public void insert(int i, String n, float s)


{
id = i;
name = n; Employee
salary = s;
} id
name
public void display() salary
{
System.out.println("___________________"); Insert (i,n,s);
System.out.println("The employee's info"); Display ()
System.out.println("ID: " + id );
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
}
}
‫ رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ‬.‫ د‬:‫اﺳﺗﺎذ اﻟﻣﺎدة‬ 51
Class Employee: (2 of 2) e1

Id = 123123
Name= Salam Ali
public class EmployeeDemo { Salary = 3000

Insert(i,n,s);
public static void main(String[] args) {
Display ()
Employee e1 = new Employee();
Employee e2 = new Employee();
Employee e3 = new Employee(); e2

Id = 999324
e1.insert(123123, "Salam Ali", 3000); Name=Kamal Jalal

e2.insert(999324, "Kamal Jalal", 4400); Salary = 4400

e3.insert(12323, "Sara Hassan", 8722); Insert(i,n,s);


Display ()

e1.display();
e3
e2.display();
e3.display(); Id = 12323
Name= Sara Hassan

} Salary = 8722

} Insert(i,n,s);
Display ()

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


Just checking …
A variable whose meaning is confined to a method
definition is called an/a

A. instance variable
B. local variable
C. global variable
D. none of the above

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


Next lecture, we will see:
1. Writing our own classes
1.1 Classes and Objects
1.2 Instance Variables
1.3 Methods (more)
2. Some notions of OOP
3. Passing and returning objects
4. Recap

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

You might also like