Lecture 02
Lecture 02
Lecture 02:
Classes and Objects
Slides have originally been prepared by Rose Williams, Binghamton University and Kenrick
Mock, University of Alaska Anchorage
Last class: Object Oriented Programming
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 {
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
• Example:
objectName = new ClassName();
3. Or simple:
ClassName objectName = new ClassName();
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 {
dateOne.printDate();
dateTwo.printDate();
}
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.
_________________________
Your order is:
Size: Large
Topping: 3
Total Price: 23.0$
}
}
رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ. د:اﺳﺗﺎذ اﻟﻣﺎدة 22
Example: Pizza place
import java.util.Scanner;
public class PizzeDemo {
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
dateTwo.month = "July";
dateOne and dateTwo dateOne.day = 03;
are objects from the class
Date dateTwo.year = 2020;
• 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;
}
method
public void printDate()
{
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;
}
}
}
} 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)
• Static:
• We will cover this late…
}
Coin.Java
رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ. د:اﺳﺗﺎذ اﻟﻣﺎدة 39
Example (Coin class)
public class FlipRace {
A. void
B. invocation statement
C. declaration
D. return
• 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.
45
Parameters
• Some methods need to receive additional data in order to
perform their work
• Definitions:
• Formal parameter:
• parameter specified in the method header
• Argument:
• The value that is plugged in for the parameter
• field; field
• method; field
• method; parameter
• parameter; method
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
e1.display();
e3
e2.display();
e3.display(); Id = 12323
Name= Sara Hassan
} Salary = 8722
} Insert(i,n,s);
Display ()
A. instance variable
B. local variable
C. global variable
D. none of the above