KEMBAR78
Unit 2 - Using Objects | PDF | Parameter (Computer Programming) | Constructor (Object Oriented Programming)
0% found this document useful (0 votes)
18 views48 pages

Unit 2 - Using Objects

Unit 2 of the AP CS A course focuses on using objects in Java, covering concepts such as classes, objects, instance variables, constructors, and methods. It explains how to create and manipulate objects, including procedural abstraction, return values, and the use of getters and setters. Additionally, it introduces string objects, wrapper classes, and static methods, particularly within the Math class.

Uploaded by

veddwivedi39
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)
18 views48 pages

Unit 2 - Using Objects

Unit 2 of the AP CS A course focuses on using objects in Java, covering concepts such as classes, objects, instance variables, constructors, and methods. It explains how to create and manipulate objects, including procedural abstraction, return values, and the use of getters and setters. Additionally, it introduces string objects, wrapper classes, and static methods, particularly within the Math class.

Uploaded by

veddwivedi39
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/ 48

Unit 2 - Using Objects

AP CS A
(Work in Progress)
Lesson 1 - Objects -
Instances of Classes
Unit 2 - Using Objects
Classes and objects
A class is how we abstract/represent a real-life concept or object in Java.
● Classes are like blueprints for a house–not an actual house, but something that defines how the
class should look.
An object is an instance of a class.
● A house is like an object. Many houses can be made from one blueprint.
Classes and objects (CB Daily Video)
Instance Variables
Most classes have instance variables–variables that are part of every instance of that class (or
object). They help to define the class.
They are almost always:
1. private
2. declared, rather than defined, at the beginning of the class

These instance variables


mean that EVERY
student object will have
an id, a name, and a
grade level.
Lesson 2 - Creating and
Storing Objects (Instantiation)
Unit 2 - Using Objects
Constructor
A constructor defines the rules for what happens when a class is created.
It is a special method that is automatically called when we create an object.

The format for creating a class is as follows:

public ClassName(parameters){
}
Constructor
The constructor for the student class will print “Student created!”
Meaning that when we create a new Student object, that message will print in the console:

Note: the object MUST be created in another file in the same package. Both the Student and Test
classes pictured above are in the same package. We can only create a Student object in the Test
class.
Constructor
A method’s signature is its name and list of parameters.
This includes constructors.
Note that the Student constructor below has no parameters.
Parameters
The variables specified as input for a method
Constructors, cont’d
A typical constructor takes in parameters that match the instance variables for the class, and sets
those instance variables to whatever the parameters are.
Constructors, cont’d
You don’t have to do this, though. Variables that have been declared but not defined are assigned
values. For non-primitive data types (like Strings), this is null.
The constructor below does not define any of the instance variables, but prints them anyway.
The id (an integer) defaults to 0. The name (a String) defaults to null.
Constructors, cont’d
Note we are saying this. – this is because the parameters have the same names as the instance
variables. this.id refers to the instance variable, while just id refers to the parameter.
Constructors, cont’d
You do not need to use the this keyword if your parameters are named differently. In this example,
id is the instance variable, and s_id is the parameter.
Overloading
A method is overloaded if another method exists with the same name, but different parameters.
Since most new students every year are in the 9th grade, it’s a little easier to use the second
constructor here. That way, we don’t have to explicitly say the student is in 9th grade–if we
omit that information, their grade level is set to 9 by default.
Creating Objects
To create an object, you say:
ObjectType objectName = new ObjectType(arguments);

Note that the arguments given above match the type and order of the ones below.
Lessons 3-5 - Calling methods
Unit 2 - Using Objects
Procedural Abstraction
Procedural Abstraction refers to the concept that you do not need to know exactly how a method
works (internally) to know what it does or how to use it.

You do not need to see what is going on behind the scenes to understand that
Math.sqrt returns the square root of a number, or that String.charAt(i)
returns the character at index i in a String.

Try to adopt the same mindset when naming your own methods.
Return values
Return values are what is returned, or given back, by a method.
We use them so that we can call a method and save its return value to use later.
Return values
We use the word “return” for
two reasons:
1. The method “returns”, or
gives back to us, a value
we can use

2. Execution of the
program “returns” back
to where the method
was called from (see the
comments in the
accompanying code
snippet)
Return values
We don’t have to return
anything. Sometimes all we
want to do is change an
instance variable or even print
some information.
In this case, the return type of
our method is void.
Setters
Setters are methods that simply
take one parameter and set an
instance variable to its value.
They don’t return anything, so their
return type is void.
Getters
Getters are methods that take no
parameters, but return the value of
the instance variable specified by
their name.
NullPointerException
If you attempt to use null as if it were an actual object, you will get a NullPointerException.
Lesson 6-7 - String Objects
and Methods
Unit 2 - Using Objects
Strings
A “string” of characters
Like a friendship bracelet!
Strings
Strings can be instantiated either with literals or with
a constructor
Strings
Strings are immutable
(They cannot be changed)
Index
The position of an item in a data structure
The first one is 0, then 1…all the way to length - 1
Concatenation
Sticking things together
Concatenation
Concatenation has the same precedence as +-
Concatenation
Concatenation has the same precedence as +-
Concatenation
Have to use parentheses if you want math to happen
in the same expression as concatenation
String Methods
For a previously defined String, s:
● s.substring(a, b) - returns a section (String) of s starting from a and going to (but not including) b.

● s.indexOf(c) - returns the index (int) at which the first instance of c is located (-1 if it is not in the
string at all)
String Methods
For a previously defined String, s:
● s.length() - returns the length of s

● s.compareTo(d) - compares the first two characters in s and d that are different and returns an
integer representing how many letters d is before s.
.compareTo()
If one string is the other string but with additional characters, .compareTo returns the number of
additional characters.
Lesson 8 - Wrapper Classes
Unit 2 - Using Objects
Wrapper Classes
A wrapper class is a class that “wraps” a primitive data type in a class.
It allows us to represent a primitive data type as an Object instead.
For now, we have no use for this, but we will.
Wrapper Classes
Wrapper classes allow us to…wait, why are there lines through Integer and Double?
These constructors are deprecated. They are no longer intended to be used when writing Java.
Soon, they will be removed entirely, and this code will error instead.
A hard truth
Collegeboard does not care that Integer() and Double() are deprecated. They want you to know them
anyway.
This course is not about Java best practices. It’s about what’s on the exam.
Therefore, you need to know these.
Lesson 9 - The Math Class
Unit 2 - Using Objects
Static methods
So far we have been writing instance methods. To call an instance method, you have to first create an
object, then call the method using that object. This is because they rely on the data for that specific object.

Static methods can be called directly from the class–without creating an object of it. They do not rely
on data for a specific object.
The Math Class
The Math class has a bunch of useful built-in
methods. They are all static.
Math Methods
Manipulating Math.random()
Most of the time we are using Math.random(), we don’t actually want a number b/w 0 and 1. So we can
manipulate the result to get the range we want.
Manipulating Math.random()
Most of the time we are using Math.random(), we don’t actually want a number b/w 0 and 1. So we can
manipulate the result to get the range we want.
Manipulating Math.random()
Manipulating Math.random()
In general, to get a random integer between min and max, the formula is:

Where:

● rn is the result of Math.random


● max is the maximum result you want
● min is the minimum result you want

Note that it doesn’t matter whether you cast as an int first or add min first–these are the last two steps and
can be done in any order.

You might also like