Unit 2 - Objects#
This unit will introduce the concepts of classes and objects, and how they are used in Java. The unit will also cover the String class and the Math class, two essential parts of the Java standard library.
Classes and Objects#
We dive more deeply into writing classes in chapter 5, the next section will give you a quick idea of what a class is so you have an easier time with the latter parts of this chapter.
What are Classes and Objects?#
In object-oriented programming, a class is a blueprint for creating objects. An object is an instance of a class. In simpler terms, classes are user defined data types. Objects are variables of the new types.
A class defines the properties (data) and behaviours (methods) that its objects will have. For example, a Dog
class might have properties like name
, breed
, and age
, and methods like bark()
, eat()
, and sleep()
.
Here’s how you might define a simple Dog
class in Java:
public class Dog {
// Properties (data)
private String name;
private String breed;
private int age;
// Constructor
public Dog(String name, String breed, int age) {
this.name = name;
this.breed = breed;
this.age = age;
}
// Methods (behaviours)
public void bark() {
System.out.println("Woof!");
}
public void eat(String food) {
System.out.println(name + " is eating " + food);
}
public void sleep() {
System.out.println(name + " is sleeping.");
}
}
In this example:
name
,breed
, andage
are private properties of theDog
class. They represent the data that aDog
object can hold.The
Dog(String name, String breed, int age)
function is a constructor. It is used to create newDog
objects and initialize their properties.bark()
,eat(String food)
, andsleep()
are public methods of theDog
class. They define the behaviours that aDog
object can perform.
Creating and Using Objects#
Once you have a class defined, you can create objects (instances) of that class. Each object will have its own copy of the class’s properties and can call the class’s methods.
public class Main {
public static void main(String[] args) {
// Create a new Dog object named "buddy"
Dog buddy = new Dog("Buddy", "Golden Retriever", 3);
// Call methods on the "buddy" object
buddy.bark(); // Output: Woof!
buddy.eat("bones"); // Output: Buddy is eating bones
buddy.sleep(); // Output: Buddy is sleeping.
// Create another Dog object named "lucy"
Dog lucy = new Dog("Lucy", "Poodle", 5);
// Call a method on the "lucy" object
lucy.bark(); // Output: Woof!
}
}
In this example:
Dog buddy = new Dog("Buddy", "Golden Retriever", 3);
creates a newDog
object namedbuddy
and initializes its properties with the given values.buddy.bark()
,buddy.eat("bones")
, andbuddy.sleep()
call the respective methods on thebuddy
object.Dog lucy = new Dog("Lucy", "Poodle", 5);
creates anotherDog
object namedlucy
with different property values.lucy.bark()
calls thebark()
method on thelucy
object.
Key Notes on Classes and Objects#
Overloading Constructors: You can have multiple constructors in a class with different parameters. This is called constructor overloading and allows for flexibility in creating objects.
Procedural Abstraction: When using a method, you don’t need to worry about the internal details of how it works. This is called procedural abstraction and simplifies code usage.
Tracing Methods: Understanding how methods work and the order in which they are executed is crucial for debugging and understanding program flow.
String Class#
Introduction#
The String class in Java represents a sequence (string) of characters (data of type char). It provides various methods for manipulating and working with strings.
Creating Strings#
There are two common ways to create strings in Java:
Using the
new
keyword:String s1 = new String("Hello");
Using string literals:
String s2 = "World!";
String Methods#
The String class provides a rich set of methods for working with strings. Here are some key methods:
charAt(int index)
: Returns the character at the specified index.char c = s1.charAt(0); // c will be 'H'
substring(int beginIndex, int endIndex)
: Returns a new string that is a substring of the original string, starting atbeginIndex
and ending atendIndex - 1
.String sub = s1.substring(1, 4); // sub will be "ell"
length()
: Returns the length of the string.int len = s1.length(); // len will be 5
indexOf(String str)
: Returns the index of the first occurrence of the specified substring within the string. Returns -1 if the substring is not found.int index = s1.indexOf("lo"); // index will be 3
equals(Object anObject)
: Compares the string to the specified object for equality. Returnstrue
if the strings are equal,false
otherwise.boolean isEqual = s1.equals("Hello"); // isEqual will be true
contains(CharSequence s)
: Checks if the string contains the specified sequence of characters. Returnstrue
if the sequence is found,false
otherwise.boolean contains = s1.contains("el"); // contains will be true
Example:#
String lyrics = "The wheels on the bus go round and round";
String word = "bus";
char secondLetter = word.charAt(1); // secondLetter will be 'u'
int wordIndex = lyrics.indexOf(word); // wordIndex will be 15
int wordLength = word.length(); // wordLength will be 3
String extractedWord = lyrics.substring(wordIndex, wordIndex + wordLength); // extractedWord will be "bus"
boolean areEqual = extractedWord.equals(word); // areEqual will be true
boolean doesContain = lyrics.contains(extractedWord); // doesContain will be true
Additional Notes:#
Immutability: Strings in Java are immutable, meaning their values cannot be changed after creation. Methods like
substring()
andtoUpperCase()
create new string objects instead of modifying the original string.String Concatenation: You can concatenate strings using the
+
operator.String combined = s1 + " " + s2; // combined will be "Hello World!"
Resources:#
For a complete list of String class methods, refer to the Java documentation: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html
Math Class#
Introduction#
The Math
class in Java provides various mathematical functions and constants. It’s a static class, meaning you don’t need to create an object of the Math
class to use its methods. You can directly call its methods using the class name, like Math.method()
.
Key Methods:#
Math.random()
: Returns a random double value between 0.0 (inclusive) and 1.0 (exclusive).double randomValue = Math.random(); // randomValue will be between 0.0 and 0.999...
Generating Random Integers: You can generate random integers within a specific range using
Math.random()
.int randomInt = (int)(Math.random() * 10); // Random integer between 0 and 9
Math.max(double a, double b)
: Returns the greater of the two given values.int max = Math.max(10, 20); // max will be 20
Math.min(double a, double b)
: Returns the smaller of the two given values.int min = Math.min(10, 20); // min will be 10
Math.abs(double a)
: Returns the absolute value of the given value.int absValue = Math.abs(-10); // absValue will be 10
Math.pow(double a, double b)
: Returns the value ofa
raised to the power ofb
.double power = Math.pow(2, 3); // power will be 8.0
Math.sqrt(double a)
: Returns the square root of the given value.double sqrtValue = Math.sqrt(25); // sqrtValue will be 5.0
Example:#
double randomDouble = Math.random();
System.out.println("Random double: " + randomDouble);
int randomInt = (int)(Math.random() * 10) + 1; // Random integer between 1 and 10
System.out.println("Random integer: " + randomInt);
int maxValue = Math.max(5, 15);
System.out.println("Max value: " + maxValue);
Additional Notes:#
Constants: The
Math
class also provides mathematical constants likeMath.PI
(π) andMath.E
(Euler’s number).
Resources:#
For a complete list of
Math
class methods and constants, refer to the Java documentation: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html
Exercises:#
Classes and Objects:
Create a class of your choice (e.g.,
Book
,VideoGameCharacter
) with at least five methods.Test your code by creating objects of the class and calling its methods.Add a default constructor to the class you created in the previous exercise.
Add a method to your class that takes mathematical arguments and returns a calculated value.
Strings:
Create two strings: one containing song lyrics and another containing a word that appears in the lyrics.
Print the second letter of the word using both
charAt()
andsubstring()
methods.Use
indexOf()
to find the first occurrence of the word in the lyrics and store the index in anint
variable.Use
length()
to find the length of the word and store it in anint
variable.Create a new string variable and extract the word from the lyrics using the
substring()
method, the index, and the length you found in the previous steps.Use the
equals()
method to check if the extracted word is the same as the original word. Verify your answer using a print statement.Use the
contains()
method to check if the lyrics contain the extracted word. Verify your answer using a print statement.
Math:
Print the maximum and minimum values of the
int
data type.Generate and display two random numbers.
Simulate a dice roll and display the result using a print statement.
Remember to test your code thoroughly and refer to the resources provided for more information and examples.