Thinking in Objects
Thinking in Objects
2
Designing the Loan Class
Loan
-annualInterestRate: double The annual interest rate of the loan (default: 2.5).
-numberOfYears: int The number of years for the loan (default: 1)
-loanAmount: double The loan amount (default: 1000).
-loanDate: Date The date this loan was created.
Loan TestLoanClass
3
Class Relationships
Association
Aggregation
Composition
Inheritance (Later)
Association
Association is a general binary relationship that describes an activity between two classes
Examples:
a student taking a course is an association between the Student class and the Course class
faculty member teaching a course is an association between the Faculty class and the Course class
Association Labels
If an object is exclusively owned by an aggregating object, the relationship between the object
and its aggregating object is referred to as a composition
Examples(Aggregation andComposition)
a filled diamond is attached to an aggregating class (in this case, Student) to denote the
composition relationship with an aggregated class (Name).
14
Aggregation Between Same Class
Aggregation may exist between objects of the same class. For example, a
person may have a supervisor.
15
Aggregation Between Same Class
What happens if a person has several supervisors?
16
Examples (Car & Engine)
autoboxing and unboxing feature converts primitive into object and object
into primitive automatically.
These classes are called wrapper classes because each wraps or encapsulates a primitive type value in an object.
WrapperClasses
Boolean NOTE:
Character (1) The wrapper classes do not
Short have no-arg constructors.
Byte (2) The instances of all wrapper
Integer classes are immutable, i.e.,
Long their internal values cannot be
changed once the objects are
Float created.
Double
17
The Integer and Double Classes
18
TheInteger and Double Classes
You can construct a wrapper object either from a primitive data type value or
from a string representing the numeric value, by using the valueOf method
(autoboxing or boxing):
For example:
Double.valueOf(5.0), Double.valueOf("5.0"), Integer.valueOf(5), and
Integer.valueOf("5").
TheInteger and Double Classes
Integer x3 = 32;
Recall the String class contains the compareTo method for comparing two strings. The
numeric wrapper classes contain the compareTo method for comparing two numbers and
returns 1, 0, or –1, if this number is greater than, equal to, or less than the other number. For
example,
Double.valueOf(12.4).compareTo(Double.valueOf(12.3)) returns 1;
Double.valueOf(12.3).compareTo(Double.valueOf(12.3)) returns 0;
20
Examples
21
Numeric Wrapper ClassConstants
Each numerical wrapper class has the constants MAX_VALUE and MIN_VALUE.
MAX_VALUE represents the maximum value of the corresponding primitive data type. For
Byte, Short, Integer, and Long, MIN_VALUE represents the minimum byte, short, int, and
long values.
For Float and Double, MIN_VALUE represents the minimum positive float and
double values.
22
ConversionMethods
Each numeric wrapper class implements the
abstract methods doubleValue, floatValue,
intValue, longValue, and shortValue, which are
defined in the Number class. (Number class is a
superclass for the wrapper classes).
These methods “convert” objects into
primitive type values.
23
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:
Double doubleObject = Double.valueOf("12.4");
Integer integerObject = Integer.valueOf("12");
24
The Methods forParsing Strings into Numbers
You have used the parseInt method in the Integer class
to parse a numeric string into an int value and the
parseDouble method in the Double class to parse a
numeric string into a double value.
Each numeric wrapper class has two overloaded parsing
methods to parse a numeric string into an appropriate
numeric value based on 10 (decimal) or any specified
radix
(e.g., 2 for binary, 8 for octal, and 16 for hexadecimal).
25
Parsing Strings into Numbers
• Integer.parseInt("11", 2) returns 3;
• Integer.parseInt("12", 8) returns 10;
• Integer.parseInt("13", 10) returns 13;
• Integer.parseInt("1A", 16) returns 26;
• Integer.parseInt("12", 2) would raise a runtime exception
because 12 is not a binary number.
Automatic Conversion BetweenPrimitive
Typesand Wrapper ClassTypes
JDK 1.5 allows primitive type and wrapper classes to be converted automatically. For
example, the following statement in (a) can be simplified as in (b):
27
BigIntegerandBigDecimal
The largest integer of the long type is Long.MAX_VALUE (i.e., 9223372036854775807).
An object of BigInteger can represent an integer of any size.
The methods add, subtract, multiply, divide, and remainder can be used to perform
arithmetic operations.
BigInteger andBigDecimal
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
BigDecimal
class
BigInteger.ONE (line 9) is a constant defined in the BigInteger class. BigInteger.ONE is the
same as new BigInteger("1").