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

JAVA 3

Uploaded by

aelshahed2027
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JAVA 3

Uploaded by

aelshahed2027
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

JAVA Programming

Fundamental & OOP


Eng. Nada Mohamed Sarhan
[email protected]
OUTLINES
• Questions
• Describe how objects are stored in memory
• Array of objects
• Describe how an array of objects is stored in memory
• Describe how string is stored in memory
• Declare an object as a field
• Constructor
• Destructor
• Use the NetBeans IDE
OUTLINES
• OOP Concepts:
• Inheritance
• Polymorphism
• Overloading
• Overriding
• Abstraction
• Encapsulation
• Interfaces
• Packages
Questions
Which of the following statements is true?
a.An object is a blueprint for a class.
b.An object and a class are exactly the same.
c.An object is an instance of a class.
d.A class is an instance of an object.
References and Objects in Memory
Reference
type
Reference
variable
Create a new object.

6 Camera remote1 = new Camera();


7 remote1.menu();
8
9 TV remote2 = new TV();
10 remote2.menu();
11
12 Shirt myShirt = new Shirt();
13 myShirt.display();
14
15 Trousers myTrousers = new Trousers();
16 myTrousers.display();
References and Objects in Memory
12 int counter = 10;
13 Shirt myShirt = new Shirt();
14 Shirt yourShirt = new Shirt();

Stack 0x034009 Heap


12 shirtID
15.99 price
B colorCode

counter 10

myShirt 0x034009 12 shirtID


15.99 price
yourShirt 0x99f311
colorCode
B

Variables Objects
Assigning a Reference to Another
Reference
myShirt = yourShirt;

0x034009

12
15.99
B

counter 10
0x99f311
myShirt 0x99f311 12 shirtID
15.99 price
yourShirt 0x99f311
colorCode
B
Two References, One Object
Code fragment:

12 Shirt myShirt = new Shirt();


13 Shirt yourShirt = new Shirt();
14
15 myShirt = yourShirt; //The old myShirt object is
16 //no longer referenced
17 myShirt.colorCode = 'R';
18 yourShirt.colorCode = 'G';
19
20 System.out.println("Shirt color: “ + myShirt.colorCode);
Two References, One Object
Code fragment:

12 Shirt myShirt = new Shirt();


13 Shirt yourShirt = new Shirt();
14
15 myShirt = yourShirt; //The old myShirt object is
16 //no longer referenced
17 myShirt.colorCode = 'R';
18 yourShirt.colorCode = 'G';
19
20 System.out.println("Shirt color: “ + myShirt.colorCode);

Output from code fragment:

Shirt color: G
Array of Objects All in
• Examples: one
1 String[] names = {"Mary","Bob","Carlos"}; line
2
3 int[] ages = new int[3];
4 ages[0] = 19; Multistep
5 ages[1] = 42;
approach
6 ages[2] = 92;

int [] ages;
ages = {19, 42, 92};
Array of Objects All in
• Examples: one
1 String[] names = {"Mary","Bob","Carlos"}; line
2
3 int[] ages = new int[3];
4 ages[0] = 19;
Multistep
approach
5 ages[1] = 42;
6 ages[2] = 92;

• Not permitted (compiler will show an error):


int [] ages;
ages = {19, 42, 92};
Storing Arrays in Memory
int age = 35;
int[] ages = {19, 42, 92};

Primitive
variable of
type int
0x034009
age
35 0 19 Primitive
ages 0x034009
42 variables of
2 92 type int held
as array
elements
• The Array variable is an object reference, not a primitive data type.
Storing Arrays of Object References
in Memory
Item item = new Item();
Item[] items = { new Item(), new Item(), new Item() };

0 itemId
item 0x034009 0.0 price
U colorCode

items 0x99f311 0 itemId


0x99f311 price
0.0
0x00099 U colorCode
0x00327
0 itemId
0x00990
0.0 price
U colorCode

0 itemId
0.0 price
U colorCode
Sorting Strings

String myString = "Hello";

0x034009

Hello

myString 0x034009
Concatenate Strings

String myString = "Hello";


myString = myString.concat(" World");
Concatenate Strings

String myString = "Hello";


myString = myString.concat(" World");

0x034009

"Hello"

0x99f311
myString 0x99f311
"Hello World"
Question
Given the following array declaration, which of the following
statements are true?
• int [ ] ages = new int [13];
a. ages[0] is the reference to the first element in the array.
b. ages[13] is the reference to the last element in the array.
c. There are 13 integers in the ages array.
d. ages[5] has a value of 0.
Question
Which statements are true?
a. There are eight primitive types built in to the Java programming language.
b. byte, short, char, and long are the four integral primitive data types in
the Java programming language.
c. A boolean type variable holds true, false, and nil.
d. short Long = 10; is a valid statement that adheres to the variable
declaration and initialization syntax.
Overloading Methods
• Have the same name in a class
• Have different signatures
• The number of parameters
• The types of parameters
• The order of parameters
• May have different functionality or similar functionality
• Are widely used in the foundation classes
Overloading Methods
The method
signature
• 1 public final class Calculator {
• 2 The method type
• 3 public static int sum(int num1, int num2){
• 4 System.out.println("Method One");
• 5 return num1 + num2;
• 6 }
• 7
• 8 public static float sum(float num1, float num2) {
• 9 System.out.println("Method Two");
• 10 return num1 + num2;
• 11 }
• 12 public static float sum(int num1, float num2) {
• 13 System.out.println("Method Three");
• 14 return num1 + numb2;
• 15 }
Overloading Methods
• 1 public class CalculatorTest {
• 2
• 3 public static void main(String[] args) {
• 4
• 5 int totalOne = Calculator.sum(2, 3);
• 6 System.out.println("The total is " + totalOne);
• 7
• 8 float totalTwo = Calculator.sum(15.99F, 12.85F);
• 9 System.out.println(totalTwo);
• 10
• 11 float totalThree = Calculator.sum(2, 12.85F);
• 12 System.out.println(totalThree);
• 13 }
• 14 }
Question
• Which method corresponds to the following method call?
• myPerson.printValues(100, 147.7F, "lavender");

a. public void printValues (int i, float f)

b. public void printValues (i, float f, s)

c. public void printValues (int i, float f, String s)

d. public void printValues (float f, String s, int i)


Constructor
• A constructor method is a special method that is invoked when you
create and initialize an object instance.
• It is called by using the new keyword. (at the time of object creation)
• Its purpose is to instantiate an object of the class and store the reference in
the reference variable. Constructor
method is called.
Shirt myShirt = new Shirt();

• It has a unique method signature.


<modifier> ClassName()
Constructor
• There are basically 2 rules defined for the constructor:
1. Constructor name must be same as its class name
2. Constructor must have no explicit return type

• There are 2 types for constructor:


1. Default constructor (No parameters)
2. Parameterized
Constructor (Writing and Calling)
1 public static void main(String[] args){
2 Shirt myShirt = new Shirt();
3 }

1 public class Shirt {


2 //Fields
3 public String description;
4 public char colorCode;
5 public double price;
6
7 //Constructor
8 public Shirt(){
9 description = "--description required--";
10 colorCode = 'U'
11 price = 0.00;
12 }
13
14 //Methods
15 public void display(){
16 System.out.println("Shirt description:" + description);
17 System.out.println("Color Code: " + colorCode);
18 System.out.println("Shirt price: " + price);
19 }…
Constructor
• Calling a Method in the Same Class
1 public class Shirt {
2 public String description;
3 public char colorCode;
4 public double price;
5
6 public Shirt(){
7 description = "--description required--";
8 colorCode = 'U'
9 price = 0.00;
10
11 display(); //Called normally
12 this.display(); //Called using the 'this' keyword
13 }
14
15 public void display(){
16 System.out.println("Shirt description:" + description);
17 System.out.println("Color Code: " + colorCode);
18 System.out.println("Shirt price: " + price);
19 }
20 …
Destructor (finalizers)
• Is used to delete or destroy the object that releases the resource occupied by
the object.
• We can call it by using the method itself or invoking the
method System.runFinalizersOnExit(true).
• It is a protected method of the Object class that is defined in the java.lang
package.
• It can be called only once.
• We need to call the finalize() method explicitly if we want to override the
method.
• Except for the unchecked exceptions, the JVM ignores all the exceptions that
occur by the finalize() method.
Destructor (finalizers)
Destructor (finalizers)
Introducing NetBeans IDE
A Java Integrated Development Environment (IDE) is a type of software
that makes it easier to develop Java applications.
• An IDE provides:
• Syntax checking
• Various automation features
• Runtime environment for testing
• It enables you to organize all your Java resources and environment settings
into a Project.
Introducing NetBeans IDE
• To create new project:
Introducing NetBeans IDE
Code
Project
Editor
Navigator

Class
Navigato
r

Program Output
Introducing NetBeans IDE
• The code editor will tell you when you have done something wrong.
• Avoiding Syntax Problems
Introducing NetBeans IDE
• Compile Error: Variable Not Initialized
1 public static void main(String[] args){
2
3 Customer customer01; //Declare the reference
4 //No instantiation
5 customer01.name = "Robert";
9
10 }

NetBeans indicates
that the variable
may not have been
initialized.
Exercises Time!
Exercises
• Write a Java program to create a class called "Person" with a name
and age attribute. Create 3 instances of the "Person" class, 1 set its
attributes using the constructor, 2 set their attributes from user, and
print their name and age.

• Write a Java program to create a class called "Rectangle" with width


and height attributes. Calculate the area and perimeter of the
rectangle.
Assignment
• Write a Java program to create a class called "Employee" with a name,
salary, and hire date attributes, and a method to calculate years of
service.
• Write a Java program to create a class called "Circle" with a radius
attribute. You can access and modify this attribute. Calculate the area
and circumference of the circle.
Bounce
• Write a Java program to create a class called "Student" with a name,
grade, and courses attributes, and methods to add and remove
courses.
REFERENCE
• https://medium.com/@saygiligozde/design-patterns-in-java-5251032
ca244

• https://discuss.educative.io/t/dao-vs-dto-vs-pojo/37663/2

You might also like