0% found this document useful (0 votes)
18 views50 pages

04 Object

Uploaded by

1762689820
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)
18 views50 pages

04 Object

Uploaded by

1762689820
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/ 50

Using Objects

This Lecture
• Objects
• Classes
• instances
• Methods
• String Methods
• Wrapper Classes
• Math Class
Objects
• Objects are variables of user-defined data types.
(reference type variables)
a person,
an item in a shop,
an order someone has made, etc.
Objects
• Objects are variables of user-defined data types.
(reference type variables)
Persons

name age gender height weight nationality ect…


Jack 86 male 178 65 china
Mike 66 male 170 55 china

Objects
• Objects are variables of user-defined data types.
(reference type variables)
Books
Object vs Primitive

• Objects : reference data types.

• Primitive : int, double, boolean …

int number = 9;

double temperature = 36.6;

boolean condition = true;

Person c =……

Book d =……
Object vs Primitive
int number = 9;
double temperature = 36.6;

name age gender height weight nationality ect…


Person c =…… Jack 86 male 178 65 china
Person d =…… Mike 66 male 170 55 china

Object vs Primitive
int number = 9;
double temperature = 36.6;

Book a =……
Book b =……
How to create a new
Book ( object ) with Java?

Template :

Class is the template you use for creating


objects.
How to create a class Book with
Java?

public class Book {


String bookName;
String Author;
String Publisher;
String Genre;
boolean Fiction;
double price;
}
How to create a new
Book ( object ) with Java?
Object 1 :

public Book(String bookName, String Author, String Publisher, String Genre, boolean
Fiction) {
this.bookName=bookName;
this.Author=Author; public class Book {
this.Publisher=Publisher; String bookName;
this.Genre=Genre; String Author;
this.Fiction=Fiction; String Publisher;
} String Genre;
boolean Fiction;
double price;
}
public class Book {
String bookName;
String Author;
String Publisher;
String Genre;
boolean Fiction;

public Book(String bookName, String Author, String Publisher, String Genre, boolean
Fiction) {
this.bookName=bookName;
this.Author=Author;
this.Publisher=Publisher;
this.Genre=Genre;
this.Fiction=Fiction;
} Object 1 :

public static void main(String[] argument){


Book bookObject1 = new Book("Picking daisies","J.Frank","Cambridge
horticulture","Gardening",false);

} reference Variables: bookObject1 is an instances of the


Object vs Primitive
int number = 9;
double temperature = 36.6;

Book a = new Book("Picking daisies","J.Frank","Cambridge


horticulture","Gardening",false);
Create an Object
• Class
• Instances Variable/static variable(state/attribute )
• Methods/behavior
• Constructor(new object)
public class Book {
String bookName;
String Author;
String Publisher;
String Genre;
boolean Fiction; instances Variable(state/attribute )

public Book(String bookName, String Author, String Publisher, String Genre, boolean
Fiction) {
this.bookName=bookName;
this.Author=Author;
this.Publisher=Publisher;
this.Genre=Genre; Constructor
this.Fiction=Fiction;
}

(methods/behavior)
public static void main(String[] argument){
Book bookObject1 = new Book("Picking daisies","J.Frank","Cambridge
new object
horticulture","Gardening",false);

} reference Variables: bookObject1 is an instances of the Book


How to create a new Rectangle?
width height
4 6
class Rectangle{
int width;
int height;
public Rectangle(int w,int h){
this.width=w;
this.height=h;
}
public static void main(String[] argument){
Rectangle objectRectangle1 = new
Rectangle(4,6);
}
}
How to create a new Rectangle?
width height perimeter() Area()
4 6 20 24
public class Rectangle{
int width; public class Rectangle{
int height; int width;
instances Variable(state/attribute )
int height;
public Rectangle(int w,int h){ public Rectangle(int w,int h){
width=w; this.width=w;
this.height=h; this.height=h;
Constructor
} }
public int Area() { public int Area() {
return return this.height*this.width;
this.height*this.width; }
} public int perimeter() {
public int perimeter() { return (this.width+this.height)*2;
return }
(this.width+this.height)*2; public static void main(String[] argument){
instances methods/behavior
} Rectangle objRect1 = new Rectangle(4,6);
} System.out.println(objRect1.perimeter());
System.out.println(objRect1.Area());
}
}
How to create a new car?
Object vs Primitive

• Objects are structures that contain a state and behavior.

• The state contains information about the specific object.

 Car state : color, productionDate, new energy

• The behavior is the actions that can be performed on a specific object.

 Car behavior : Brake, accelerate, shift gear, self-driving


Class vs Object
• Classes are the templates you use for creating
public class car {
objects.
public String color;
private int productionDate;
private boolean isNewEnergy;

public String getColor() {


return color;
}

public int getProductionDate () {


return productionDate;
}

public car(String cColor, int productionDate, boolean


cIsNewEnergy) {
color = cColor;
productionDate = cSize;
isNewEnergy = cIsNewEnergy;
}
instancess

• Remember, an object is an instances of a class.

public class testCar {


public static void main(String[] arguments) {
car c1 = new car("Blue",2023,true);
car c1
car c2 = new car("Green",2024,true);
}
}
car c2
instancess
• Remember, an object is an instances of a class.

public class testCar {


public static void main(String[] arguments) {
car c1 = new car("Blue",2023,true);
car c1
car c2 = new car("Green",2024,true);
}
}
car c2
instances can call instances variable :instances. instances variable(c1.color)
instances can call instances method :instances. instances method(c1.getcolor())
Constructor
• The constructor is used to instantiate – create an
public class testCar {
instances of – an object. public static void main(String[]
arguments) {
public class car { car c1 = new car("Blue",2023,true);
private String color; car c2 = new
private int productionDate; car("Green",2024,true);
private boolean isNewEnergy; }
public String getColor() { }
return color;
}

public car(String cColor, int productionDate, boolean cIsNewEnergy) {


color = cColor;
productionDate = cSize;
isNewEnergy = cIsNewEnergy;
}
}
Constructor Overload
public class car {
private String color; public class testCar {
private int productionDate; public static void main(String[]
private boolean isNewEnergy; arguments) {
public car(){} car c = new car();
public car(String cColor, int productionDate) { car c1 = new car("Blue",2023);
color = cColor; car c2 = new
productionDate = cSize; car("Green",2024,true);
}
} }

public car(String cColor, int productionDate, boolean cIsNewEnergy) {


color = cColor;
productionDate = cSize;
isNewEnergy = cIsNewEnergy;
}
}
Methods
• Procedures that allow us to control and define the
behavior of an object.
public class car {
private String color;
private int productionDate;
private boolean isNewEnergy;

public String getColor() {


return color;
}
public int getProductionDate () {
return productionDate;
}
public car(String cColor, int productionDate, boolean
cIsNewEnergy) {
color = cColor;
productionDate = cSize;
isNewEnergy = cIsNewEnergy;
}
Methods
• Procedures that allow us to control and define the
behavior of an object.
public class car {
type Name arguments

public String getColor(String color) {

Body return color;

}
Methods
public class car {
private String color;
private int productionDate;

public String getColor() {


return color;
}

public int getProductionDate () {


return productionDate;
} public static void main(String[] args)
} {
System.out.print(“Hello World”);
}
Calling a Void Method
Calling a Method
public class testCar {
public static void main(String[]
arguments) {
Rectangle r = new
Rectangle(20,20);
int area=r.getArea();
String rectangleInfo = r.toString();
Rectangle r1 = new
Rectangle(100,100);
System.out.println(r1.getArea());
System.out.println(r1.toString());
}
}
Calling a Method with Parameters(Pass By Value)
public class testCar {
class Rectangle{ public static void main(String[]
public int width;
arguments) {
public int height;
public Rectangle(int rectWidth, int rectHeight) { Rectangle r = new
width = rectWidth;
Rectangle(20,20);
height = rectHeight;
} r.setWidth(700);
public Rectangle(int sidelength) {
r.setRectangle(900,90);
width = sidelength;
height = sidelength; System.out.println(r1.width);
}
System.out.println(r1.height);
public void setWidth(int w){
width= w; Rectangle r1 = new
}
Rectangle(100);
public void setRectangle(int w,int h){
width= w; System.out.println(r1.width);
height=h;
System.out.println(r1.height);
}}
}
Calling a Method with Parameters(Pass By Value)
Calling a Method with Parameters(Pass By Value)
public class testCar {
class Rectangle{ public static void main(String[]
public int width; arguments) {
public int height;
public Rectangle(int rectWidth, int rectHeight) { Rectangle r1 = new
width = rectWidth; Rectangle(100);
height = rectHeight; change(r1);
} System.out.println(r1.height);
public Rectangle(int sidelength) { }
width = sidelength;
height = sidelength; private static void change(Rectangle
} r1) {
public void setWidth(int w){ r1.height=30;
width= w; }
}
public void setRectangle(int w,int h){ }
width= w;
height=h;
}}
String Objects: Concatenation, Literals & More

Introducing Strings:
A String is a sequence of characters.
For example, the famous "Hello World" is a String.

int number = 9;
double temperature = 36.6;
String str = "Hello World" ;

String str = new String("Hello"); // String

constructor
String Objects: Concatenation, Literals & More

Concatenating Strings
Concatenate is a fancy word for joining or linking two
things together.
You can do this in Java by using the addition operator, +,
String one = "Mc";
or +=.
String two = "Donald's";
String concatenate = one + two;
String Objects: Concatenation, Literals & More

Concatenating Strings with Primitive Data Types


You are not limited to concatenating strings with other
strings.
String str = "Mc";
int number = 24;
String newStr = str + number +
false;
// newStr = " Mc24false ";
String Objects: Concatenation, Literals & More

Escape Sequences

Escape Sequence Function Output

\" "\"Allows for quotations\"" "Allows for quotations “

\\ "Includes a backslash\\" Includes a


backslash\
\n "This creates \na line break" This creates
a line
break
\t "This adds a \ttab space" This adds a tab
space
String Methods

APIs, Libraries, and Documentation


String Methods

String Index Values


String str = "Hello World";
String Methods String Index Values

Index: 0 1 2 3 4 5
67
String BigGreeting = "Hi Jambo" ;
int len =
// int len = 8;
BigGreeting.length();
String name = // String name =
BigGreeting.substring(3,len); “Jambo”;
String name = // String name =
BigGreeting.substring(3,7); “Jamb”;
String name =
// String name = “Hi Jambo”;
BigGreeting.substring(0);
int index =
// int index = 1;
BigGreeting.indexOf("i");
int index =
// int index = -1;
BigGreeting.indexOf("c");
int index =
// int index = 3;
BigGreeting.indexOf("Jam");
boolean bool = BigGreeting.equals("hi
// boolean bool = false;
Jambo");
int com =BigGreeting.compareTo("Hi
// int com = 0;
Jambo");
String Methods String Index Values

Index: 0 1 2 3 4 5
67
String BigGreeting = "Hi Jambo" ;
int com =BigGreeting.compareTo("Hi
// int com = 0;
Jambo");
if string1 > string2, it returns positive number

if string1 < string2, it returns negative number


if string1 == string2, it returns 0
String Methods String Index Values
Wrapper Classes: Integers and Doubles

A wrapper class in Java is a class that contains or "wraps"


int number = 24;
primitive data types as an object.
Primitive Type Corresponding Wrapper Integer num = 25;
Class
boolean Boolean
int x = new Integer(number);
char Character
Integer z = new
int Integer Integer(number);
double Double int y = z. intValue();
Wrapper Class Methods Use

Integer.MIN_VALUE Returns the minimum possible int or Integer value(2^31-1) double varDbl = 15.3;
Integer.MAX_VALUE Returns the maximum possible int or Integer value(2^31) Double varDbl = 15.3;
varInt.intValue() Converts Integer value into an int value
double y = new Double(varDbl);
varDbl.doubleValue() Converts Double value into a double
value
Double x = new Double(varDbl);
double Num = x.doubleValue();
Autoboxing and Unboxing in Java
Autoboxing:
• Definition: Automatic conversion of primitive types to their corresponding wrapper classes.
• Example:
int num = 5;
Integer obj = num; // Autoboxing: int to Integer
• Usage: Occurs when a primitive value is passed to a method expecting an object or assigned to a
wrapper class variable.
Unboxing:
• Definition: Automatic conversion of wrapper class objects to their corresponding primitive types.
• Example:
Integer obj = 10;
int num = obj; // Unboxing: Integer to int
• Usage: Occurs when a wrapper class object is passed to a method expecting a primitive or
assigned to a primitive variable.
These processes simplify code by eliminating the need for manual conversion between primitives and
objects.
int number = 24;
Integer num = number; //Autoboxing: int to Integer Java's wrapper classes serve two
int x = new Integer(number); //Unboxing: Integer to int main purposes:
1.Object Conversion:
Integer z = new Integer(number); //create an Integer
object They convert primitive data types into
int y = z. intValue(); //Converts Integer value into an
int value objects. This is useful when an object
representation is required.
double var = 15.3;
2.Static Methods for Operations:
Double varDbl = var; //Autoboxing: double to Double They provide static methods for
double y = new Double(varDbl); //Unboxing: Integer to operations like converting strings to
double
numbers. These operations can be
Double x = new Double(varDbl); //create an Double performed without creating an
object
double Num = x.doubleValue(); Converts Double value
instance of the class.
into an double value
Using the Math Class

The Math class is a class that only contains static methods that are used
for specific purposes.

Method Use

Math.abs(x) Returns the absolute value of x. This can take either an int or a double.

Math.pow(base, exponent) Returns a double value of base raised to the power of exponent.

Math.sqrt(x) Returns the positive square root of x.(double value)

Math.random() Returns a double value greater than or equal to 0.0 and less than 1.0

int number = Math.abs(-89);


// |-89|=89
int number = Math.abs(-89);

// 9^(1/2))
double num = Math.sqrt(9);

// 9^3 = 9*9*9
double cubed = Math.pow(9, 3);

// Generate a random number[0,1)


double ranNum = Math.random();

// Generates random integers between 50 and 75 (not including 75)


[50,75)
int ranNumber = (int)(Math.random() * 25) + 50;

// Generates random integers between 50 and 75 ( including 75)


[50,75]

You might also like