OOP Lesson 10 Theory
OOP Lesson 10 Theory
OOP Lesson 10 Theory
-If you redefine the Loan class in Listing 10.2 without setter methods, the class is not immutable. To
become immutable, its attributes must be assigned a value when the object is created and cannot be
changed afterward.
-There are 4 common relationships among classes: association, aggregation, composition, inheritance
-Association is a general binary relationship that describes an activity between two classes.
-Aggregation is a special form of association that represents an ownership relationship between two
objects.
-Composition is a design principle where one class contains references to other classes to build a
“whole-part” relationship.
-Aggregation UML:
-Composition UML:
10.6 Why both aggregation and composition are together referred to as composition?
-Since aggregation and composition relationships are represented using classes in the same way, we will
not differentiate them and call both compositions for simplicity.
Object Representation: Allows primitive types to be used as objects, enabling their use in data
structures like lists, sets, and maps.
Type Conversion: Provides methods to convert between primitive types and strings.
Constants: Offers useful constants, such as Integer.MAX_VALUE and Boolean.TRUE.
10.9 How do you convert an integer into a string? How do you convert a numeric string into an integer?
How do you convert a double number into a string? How do you convert a numeric string into a double
value?
- You can simply use number +”” to convert an interger to a string. Alternatively use new
Interget(int).toString() to convert an interger to a string. To convert a numeric string into an interger,
use Interger.parsenInt(s). Use new Double(double).toString() to convvert a double to string. To convert a
numeric string into a double, use Double.parseDouble(s).
System.out.println(Integer.parseInt("10"));
System.out.println(Integer.parseInt("10", 10));
System.out.println(Integer.parseInt("10", 16));
System.out.println(Integer.parseInt("11"));
System.out.println(Integer.parseInt("11", 10));
System.out.println(Integer.parseInt("11", 16));
10.12 What are autoboxing and autounboxing? Are the following statements correct?
b. Integer x = 3;Correct
c. Double x = 3;Wrong
d. Double x = 3.0;Correct
- Autoboxing and autounboxing: Converting a primitive value to a wrapper object is called boxing. The
reverse conversion is called unboxing. Java allows primitive types and wrapper classes to be converted
automatically. The compiler will automatically box a primitive value that appears in a context requiring
an object, and will unbox an object that appears in a context requiring a primitive value. This is called
autoboxing and autounboxing.
Double x = 3.5;
System.out.println(x.intValue());
System.out.println(x.compareTo(4.5));
Output: 3 and -1
java.math.BigInteger z = x.add(y);
Output: x is 3
y is 7
z is 10
10.15 Suppose that s1, s2, s3, and s4 are four strings, given as follows:
String s2 = s1;
a. s1 == s2 : True
b. s1 == s3 : False
c. s1 == s4 : True
d. s1.equals(s3) : True
e. s1.equals(s4) : True
j. s1.toCharArray() : W,e,l,c,o,m,e,t,o,J,a,v,a
10.16 To create the string Welcome to Java, you may use a statement like this:
or:
- String s = “Welcome to Java”; is better because this type of string is stored as an interned string. The
interned strings of the same value share the same object.
System.out.println(s1);
System.out.println(s2);
10.18 Let s1 be "Welcome" and s2 be "welcome". Write the code for the following
statements:
a. Replace all occurrences of the character e with E in s1 and assign the new string
to s2.
- String s2 = s1.replaceAll(‘e’,’E’);
b. Split Welcome to Java and HTML into an array tokens delimited by a space
10.19 Does any method in the String class change the contents of the string?
- No because String class is immutable
4 public Test(String s) {
5 String text = s;
6}
10 System.out.println(test.text.toLowerCase());
11 }
12 }
- The text is declared in Line 2 as a data field, but redeclared in Line 5 as a local variable.
The local is assigned with the string passed to the constructor, but the data field is still null. In
Line 10, test.text is null, which causes NullPointerException when invoking the toLowerCase()
method;
2 String text;
5 text = s;
6}
10 System.out.println(test);
11 }
12 }
- The constructor is defined incorrectly. It should not have void
10.24 Show the output of the following code.
System.out.println("A,B;C".replaceAll(",;", "#"));
System.out.println("A,B;C".replaceAll("[,;]", "#"));
}
Output: false
true
A,B;C
A#B#C
ABC
System.out.println(m(s));
int count = 0;
if (Character.isUpperCase(s.charAt(i)))
count++;
return count;
}
}
Output: 3
10.27 How do you create a string builder from a string? How do you return a string from a
string builder?
- Use the StringBuilder’s constructor to create a string buffer for a string and use the toString method in
StringBuilder class to return a string from a StringBuilder.
10.28 Write three statements to reverse a string s using the reverse method in the
StringBuilder class.
- StringBuilder sb = new;
- StringBuilder(s);
- sb.reverse();
- s = sb.toString();
10.29 Write three statements to delete a substring from a string s of 20 characters, starting at index 4
and ending with index 10. Use the delete method in the StringBuilder class.
- StringBuilder sb = new;
- StringBuilder(s);
- sb.delete(4,10);
- s = sb.toString();
10.30 What is the internal storage for characters in a string and a string builder?
- Both string and string buffer use arrays to hold characters. The array in a string is fixed once a string is
created. The array in a string is fixed once a string is created. The array in a string buffer may change if
the buffer capacity is changed. To accommodate the change, a new array is created.
Show the value of s1 after each of the following statements. Assume that the statements are
independent.
b. s1.append(s2); JavaHTML
e. s1.charAt(2); v
f. s1.length(); 4
g. s1.deleteCharAt(3); Jav
h. s1.delete(1, 3); Ja
i. s1.reverse(); avaJ
k. s1.substring(1, 3); av
l. s1.substring(2); va
String s = "Java";
change(s, builder);
System.out.println(s);
System.out.println(builder);
}
Output: Java
Java and HTML