Lecture 17 18 19 More On Objects
Lecture 17 18 19 More On Objects
By
Dr. Bharati Mishra
Objectives
• Chapter 10
• Immutable objects
• this keyword
• Composition
• Differences between procedural programming &
OOP
• Guidelines for OOP
More on Objects
Immutable
• Immutable object:
• If the contents of an object cannot be changed
once the object is created
• Its class is called an immutable class.
Circle3
Immutable
• A class with all private data fields and
without mutators is not necessarily
immutable.
• Example: next slide (Student class)
Example..
{
id = ssn;
birthDate = new BirthDate(year, month, day);
}
8
Immutable
• For a class to be immutable, it must
1. Mark all data fields private
2. Provide no mutator methods
3. Provide no accessor methods that would
return a reference to a mutable data field
object.
Scope
Variable Scope
• Example
Variable Scope
• Example
this Keyword
this
19
Composition & Aggregation
Composition
• Procedural programming
• Methods
• OOP
• Entities grouping related methods and data
Class Abstraction
• Example: BMI
BMI
The get methods for these data fields are
provided in the class, but omitted in the
UML diagram for brevity.
UseBMIClass
BMI
-name: String The name of the person.
-age: int The age of the person. Run
-weight: double The weight of the person in pounds.
-height: double The height of the person in inches.
+BMI(name: String, age: int, weight: Creates a BMI object with the specified
double, height: double) name, age, weight, and height.
+BMI(name: String, weight: double, Creates a BMI object with the specified
height: double) name, weight, height, and a default age
20.
+getBMI(): double Returns the BMI
+getStatus(): String Returns the BMI status (e.g., normal,
overweight, etc.)
Class Abstraction
• Example: Course
Course
TestCource
Course
-name: String The name of the course.
-students: String[] The students who take the course.
-numberOfStudents: int The number of students (default: 0).
+Course(name: String) Creates a Course with the specified name.
+getName(): String Returns the course name.
+addStudent(student: String): void Adds a new student to the course list.
+getStudents(): String[] Returns the students for the course.
+getNumberOfStudents(): int Returns the number of students for the course.
Class Abstraction
Push Pop
Z
Y
x
Class Abstraction
• Example: Stack
StackOfIntegers
-elements: int[] An array to store integers in the stack.
-size: int The number of integers in the stack.
+StackOfIntegers() Constructs an empty stack with a default capacity of 16.
+StackOfIntegers(capacity: int) Constructs an empty stack with a specified capacity.
+empty(): boolean Returns true if the stack is empty.
+peek(): int Returns the integer at the top of the stack without
removing it from the stack.
+push(value: int): int Stores an integer into the top of the stack.
+pop(): int Removes the integer at the top of the stack and returns it.
+getSize(): int Returns the number of elements in the stack.
TestStackOfIntegers Run
Class Abstraction
StackOfIntegers
Class Abstraction
• Coherence
• A class should describe a single entity
• All the class operations should support a
coherent purpose.
• You can use a class for students, for
example, but you should not combine
students and staff in the same class,
because students and staff have different
entities.
Guideline
9-52
Wrapper Classes Examples
• Creating a new object:
Integer studentCount = new Integer(12);
9-53
Auto Boxing / UnBoxing
• You can get the primitive value out of the wrapper class
object directly without calling a method (as we did
when we called .intValue()). This is called Unboxing
System.out.println(studentCount);
9-54
Wrapper Classes and ArrayList Example
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
Scanner k = new Scanner(System.in);
System.out.println("Enter some non-zero integers. Enter 0 to end.");
int number = k.nextInt();
while (number != 0)
{
list.add(number); // autoboxing happening here
number = k.nextInt();
}
• The Short class has a parseShort method that converts a String to a Short
• The Float class has a parseFloat method that converts a String to a Float
• Etc.
9-57
The Parse Methods Examples
byte b = Byte.parseByte("8");
short sVar = Short.parseShort("17");
int num = Integer.parseInt("28");
long longVal = Long.parseLong("149");
float f = Float.parseFloat("3.14");
double price = Double.parseDouble("18.99");
9-58
Helpful Methods on Wrapper Classes
• The toString is static method that can convert a number back to a
String:
• output: 10000 10 20
9-59
Helpful Static Variables on Wrapper Classes
• The numeric wrapper classes each have a set of static final
variables to know the range of allowable values for the data
type:
• MIN_VALUE
• MAX_VALUE
9-60
The Static valueOf Methods
The numeric wrapper classes have a useful
class method, valueOf(String s). This method
creates a new object initialized to the value
represented by the specified string. For
example:
61
61
BigInteger and BigDecimal
If you need to compute with very large integers or high
precision floating-point values, you can use the
BigInteger and BigDecimal classes in the java.math
package. Both are immutable. Both extend the Number
class and implement the Comparable interface.
62
62
BigInteger and BigDecimal
BigInteger a = new BigInteger("9223372036854775807");
BigInteger b = new BigInteger("2");
BigInteger c = a.multiply(b); // 9223372036854775807 * 2
System.out.println(c);
LargeFactorial Run