OOP Lesson 10 Theory

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

10.1 If you redefine the Loan class in Listing 10.2 without setter methods, is the class immutable?

-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.

10.2 Is the BMI class defined in Listing 10.4 immutable?

- Yes it’s immutable

10.3 What are common relationships among classes?

-There are 4 common relationships among classes: association, aggregation, composition, inheritance

10.4 What is association? What is aggregation? What is composition?

-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.

10.5 What is UML notation of aggregation and composition?

-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.

10.7 Describe primitive-type wrapper classes.

- Byte: Wraps a byte type.


- Short: Wraps a short type.
- Integer: Wraps an int type.
- Long: Wraps a long type.
- Float: Wraps a float type.
- Double: Wraps a double type.
- Character: Wraps a char type.
- Boolean: Wraps a boolean type.

- Features of Primitive-Type Wrapper Classes:

 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.8 Can each of the following statements be compiled?

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).

10.10 Show the output of the following code.


public class Test {
public static void main(String[] args) {
Integer x = new Integer(3);
System.out.println(x.intValue());
System.out.println(x.compareTo(new Integer(4)));
}
}
Output: 3 and -1

10.11 What is the output of the following code?

public class Test {

public static void main(String[] args) {

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));

parseInt() is change to base 10


pasreInt(,xxx) is change to base xxx
if xxx base not exist it will return NumberFormatExeption
Answer:10,10,16,11,11,17

10.12 What are autoboxing and autounboxing? Are the following statements correct?

a. Integer x = 3 + new Integer(5); Correct

b. Integer x = 3;Correct

c. Double x = 3;Wrong

d. Double x = 3.0;Correct

e. int x = new Integer(3);Correct

f. int x = new Integer(3) + new Integer(4);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.

10.13 Show the output of the following code?

public class Test {

public static void main(String[] args) {

Double x = 3.5;

System.out.println(x.intValue());

System.out.println(x.compareTo(4.5));

Output: 3 and -1

10.14 Show the output of the following code?


public class Test {

public static void main(String[] args) {

java.math.BigInteger x = new java.math.BigInteger("3");

java.math.BigInteger y = new java.math.BigInteger("7");

java.math.BigInteger z = x.add(y);

System.out.println("x is " + x);

System.out.println("y is " + y);

System.out.println("z is " + z);

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 s1 = "Welcome to Java";

String s2 = s1;

String s3 = new String("Welcome to Java");

String s4 = "Welcome to Java";

What are the results of the following expressions?

a. s1 == s2 : True

b. s1 == s3 : False

c. s1 == s4 : True

d. s1.equals(s3) : True

e. s1.equals(s4) : True

f. "Welcome to Java".replace("Java", "HTML") : Welcome to HTML

g. s1.replace('o', 'T') : WelcTme tT Java

h. s1.replaceAll("o", "T") : WelcTme tT Java

i. s1.replaceFirst("o", "T") : WelcTme tT Java

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:

String s = "Welcome to Java";

or:

String s = new String("Welcome to Java");

Which one is better? Why?

- 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.

10.17 What is the output of the following code?

String s1 = "Welcome to Java";

String s2 = s1.replace("o", "abc");

System.out.println(s1);

System.out.println(s2);

- Output: Welcome to Java


Welcabcme tabc Java

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

and assign the first two tokens into s1 and s2.

- String[] tokes = “Welcome to Java and HTML”.split(‘ ’);


-s1 = tokens[0];
-s2 = tokens[1]=;

10.19 Does any method in the String class change the contents of the string?
- No because String class is immutable

10.20 Suppose string s is created using new String(); what is s.length()?


- The is s.length() is 0

10.21 How do you convert a char, an array of characters, or a number to a string?


- Use the overloaded static valueOf method in the String class

10.22 Why does the following code cause a NullPointerException?


1 public class Test {

2 private String text;

4 public Test(String s) {

5 String text = s;

6}

8 public static void main(String[] args) {

9 Test test = new Test("ABC");

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;

10.23 What is wrong in the following program?

1 public class Test {

2 String text;

4 public void Test(String s) {

5 text = s;

6}

8 public static void main(String[] args) {

9 Test test = new Test("ABC");

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.

public class Test {

public static void main(String[] args) {

System.out.println("Hi, ABC, good".matches("ABC "));

System.out.println("Hi, ABC, good".matches(".*ABC.*"));

System.out.println("A,B;C".replaceAll(",;", "#"));

System.out.println("A,B;C".replaceAll("[,;]", "#"));

String[] tokens = "A,B;C".split("[,;]");

for (int i = 0; i < tokens.length; i++)

System.out.print(tokens[i] + " ");

}
Output: false
true
A,B;C
A#B#C
ABC

10.25 Show the output of the following code.

public class Test {

public static void main(String[] args) {

String s = "Hi, Good Morning";

System.out.println(m(s));

public static int m(String s) {

int count = 0;

for (int i = 0; i < s.length(); i++)

if (Character.isUpperCase(s.charAt(i)))

count++;

return count;

}
}
Output: 3

10.26 What is the difference between StringBuilder and StringBuffer?


- StringBuffer is synchronized and StringBuilder is not.

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.

10.31 Suppose that s1 and s2 are given as follows:

StringBuilder s1 = new StringBuilder("Java");

StringBuilder s2 = new StringBuilder("HTML");

Show the value of s1 after each of the following statements. Assume that the statements are
independent.

a. s1.append(" is fun"); Java is fun

b. s1.append(s2); JavaHTML

c. s1.insert(2, "is fun"); Jais funva

d. s1.insert(1, s2); JHTMLava

e. s1.charAt(2); v

f. s1.length(); 4
g. s1.deleteCharAt(3); Jav

h. s1.delete(1, 3); Ja

i. s1.reverse(); avaJ

j. s1.replace(1, 3, "Computer"); JComputera

k. s1.substring(1, 3); av

l. s1.substring(2); va

10.32 Show the output of the following program:

public class Test {

public static void main(String[] args) {

String s = "Java";

StringBuilder builder = new StringBuilder(s);

change(s, builder);

System.out.println(s);

System.out.println(builder);

private static void change(String s, StringBuilder builder) {

s = s + " and HTML";

builder.append(" and HTML");

}
Output: Java
Java and HTML

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy