knowt logo

Using Objects

Creating and Storing Objects

Each class should have its own java file

Name the class; Example: public class Rectangle

State the instance variables

Examples: private int width; private int height;

Private is used to prevent access outside of that class or file

This declares the variables, but does not initiate them, it will be done when the object is created

Constructors

Constructors allow the creation of a new object

Example: public Rectangle(int rectWidth, int rectHeight)

public Rectangle: example of constructor name, must match class name

(int rectWidth, int rectHeight): Formal Parameter list, lists the type of variables that can be passed into that constructor and their variable names, should not be variable names

Creating Objects

Rectangle rect1 = new Rectangle(14, 16);

Rectangle: class type example

rect1: object name example

new: new operator (instantiates — the process of creating an instance of an object — a new class object)

Rectangle(14,16): call to the constructor (constructor name + actual parameters)

Method Overloading

Multiple constructors can be made in a class if only one of the parameters needs to be filled

Having multiple constructors with the same name but different formal parameters is called method overloading

The compiler knows which constructor to use based on the list of parameters that are passed

A constructor con be written with no formal parameters and instead has predetermined values

Useful when wanting to create a lot of the same type of object

Example: Rectangle rect# = new Rectangle();

Objects in Memory

Variables with values stores a location or reference to where the actual object date is located, in memory

Null: when the variable is not pointing to any object or does not allocate or memory

When using someone else’s class, we are a client of that class

Procedural abstraction: not knowing how a method or thing was written but still using it effectively in programs as long as you know what it does

Calling a Void Method

We can control an object’s behaviors and actions by defining methods

Methods: procedures that allow us to control and define the behavior of an object

Writing Method Declarations

Public void printArea() —> the signature, provides info about how it will function

Public: access specifier, determines who has access to use the method when writing classes and objects

Void: return type, indicates what type of value is being returned from the method, indicates that the method doesn’t return anything for this type

printArea(): method name, named like variables, must be concise

Writing Methods

Example on how to write a method

Calling methods

To use a method, we must call it using objectName.method();

Example: Rectangle rect1 = new Rectangle(3,7);

             rect1.printArea(); —> 21

Calling Void Methods with Parameters

How can you change an object without making a completely new one? —> Make a method with parameters

Example:

public void setWidth(int rectWidth)

{

 width = rectWidth;

}

rect1.setWidth(10);

Method can also be overloaded

Calling a Non-void Method

Return types: determines the type that will be returned from a method

We can return a value from a method by using the keyword return

Allows us to return a variable or value back to the existing program so it can be used further along

Non-void methods can be stored in another variable or used in an expression

The return type must match the data type that is being returned from the method

String Objects

If a data type starts with a capital letter, it is a reference type

Strings are Objects

We can write —> String name = “Karen”; or String name = new String(“Karen”);

Strings also have methods, i.e. name.replace(“a”, “p”) —> returns a new string with all instance of “a” replaced with “p”

Immutability

String methods do not alter, they create new string

Strings are immutable: they can’t be changed once created

Only way to change string is to re-assign it

String Concatenation

Concatenation: the process of adding 2 string values together

String can be concatenated with another to create a new one

fullName = firstName + lasName

fullName = “KarelThe Dog”

Escape Sequences

We can include “” in a program by writing \”

String dialogue = “And he said, \” Thank you!\” ”;

The following needs to be in string to work

\” is an escape sequence

\\ : Includes a backslash in the string

\n: adds a line break

\t: adds a tab space

Wrapper Classes

Wrapper classes: converts primitive types to object types in order for methods to work with them

Integer = int

Double = double

Boolean = boolean

Character = char

Example: Integer y = new Integer(17);

Converted primitive types must be changed back in order for it to be printed

Auto boxing: primitive type → wrapper class (Integer X = 4;)

Unboxing: wrapper class → primitive type (int x2 = X;)

Using the Math Class

Static methods = methods that can be called without creating an object of that class, referenced by the class name

Includes static in method declaration

Math.abs(X) = returns absolute value

Math.pow(2, 3) = 2^3

Math.sqrt(X) = takes the square root of a number

Math.random() = random number in 0.0-1.0

int rand = (int)(Math.random()*10); → makes 0.0-1.0 to 0-9

int randInt = (int)(Math.random()*(integer+1); → 0 - X.99

RW

Using Objects

Creating and Storing Objects

Each class should have its own java file

Name the class; Example: public class Rectangle

State the instance variables

Examples: private int width; private int height;

Private is used to prevent access outside of that class or file

This declares the variables, but does not initiate them, it will be done when the object is created

Constructors

Constructors allow the creation of a new object

Example: public Rectangle(int rectWidth, int rectHeight)

public Rectangle: example of constructor name, must match class name

(int rectWidth, int rectHeight): Formal Parameter list, lists the type of variables that can be passed into that constructor and their variable names, should not be variable names

Creating Objects

Rectangle rect1 = new Rectangle(14, 16);

Rectangle: class type example

rect1: object name example

new: new operator (instantiates — the process of creating an instance of an object — a new class object)

Rectangle(14,16): call to the constructor (constructor name + actual parameters)

Method Overloading

Multiple constructors can be made in a class if only one of the parameters needs to be filled

Having multiple constructors with the same name but different formal parameters is called method overloading

The compiler knows which constructor to use based on the list of parameters that are passed

A constructor con be written with no formal parameters and instead has predetermined values

Useful when wanting to create a lot of the same type of object

Example: Rectangle rect# = new Rectangle();

Objects in Memory

Variables with values stores a location or reference to where the actual object date is located, in memory

Null: when the variable is not pointing to any object or does not allocate or memory

When using someone else’s class, we are a client of that class

Procedural abstraction: not knowing how a method or thing was written but still using it effectively in programs as long as you know what it does

Calling a Void Method

We can control an object’s behaviors and actions by defining methods

Methods: procedures that allow us to control and define the behavior of an object

Writing Method Declarations

Public void printArea() —> the signature, provides info about how it will function

Public: access specifier, determines who has access to use the method when writing classes and objects

Void: return type, indicates what type of value is being returned from the method, indicates that the method doesn’t return anything for this type

printArea(): method name, named like variables, must be concise

Writing Methods

Example on how to write a method

Calling methods

To use a method, we must call it using objectName.method();

Example: Rectangle rect1 = new Rectangle(3,7);

             rect1.printArea(); —> 21

Calling Void Methods with Parameters

How can you change an object without making a completely new one? —> Make a method with parameters

Example:

public void setWidth(int rectWidth)

{

 width = rectWidth;

}

rect1.setWidth(10);

Method can also be overloaded

Calling a Non-void Method

Return types: determines the type that will be returned from a method

We can return a value from a method by using the keyword return

Allows us to return a variable or value back to the existing program so it can be used further along

Non-void methods can be stored in another variable or used in an expression

The return type must match the data type that is being returned from the method

String Objects

If a data type starts with a capital letter, it is a reference type

Strings are Objects

We can write —> String name = “Karen”; or String name = new String(“Karen”);

Strings also have methods, i.e. name.replace(“a”, “p”) —> returns a new string with all instance of “a” replaced with “p”

Immutability

String methods do not alter, they create new string

Strings are immutable: they can’t be changed once created

Only way to change string is to re-assign it

String Concatenation

Concatenation: the process of adding 2 string values together

String can be concatenated with another to create a new one

fullName = firstName + lasName

fullName = “KarelThe Dog”

Escape Sequences

We can include “” in a program by writing \”

String dialogue = “And he said, \” Thank you!\” ”;

The following needs to be in string to work

\” is an escape sequence

\\ : Includes a backslash in the string

\n: adds a line break

\t: adds a tab space

Wrapper Classes

Wrapper classes: converts primitive types to object types in order for methods to work with them

Integer = int

Double = double

Boolean = boolean

Character = char

Example: Integer y = new Integer(17);

Converted primitive types must be changed back in order for it to be printed

Auto boxing: primitive type → wrapper class (Integer X = 4;)

Unboxing: wrapper class → primitive type (int x2 = X;)

Using the Math Class

Static methods = methods that can be called without creating an object of that class, referenced by the class name

Includes static in method declaration

Math.abs(X) = returns absolute value

Math.pow(2, 3) = 2^3

Math.sqrt(X) = takes the square root of a number

Math.random() = random number in 0.0-1.0

int rand = (int)(Math.random()*10); → makes 0.0-1.0 to 0-9

int randInt = (int)(Math.random()*(integer+1); → 0 - X.99

robot