Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3 public class stringbufferEx lic static void main (String arga[1 stringbufferex

ID: 3700050 • Letter: 3

Question

3 public class stringbufferEx lic static void main (String arga[1 stringbufferex stringbutterEestringbutfertx atringaufterix.proceed ) private void proceed ingBuffer stringBuffer new stringBuffer Initial value) System.out.printinistring, format (.8-35s ?s- .with inital value: ,, stringBuffer) r.setLength(o) //Setting the lenght to zero (0), which wil1 also rintin (String.format (-358tAfter setting the lenght to zero clear the contents of the stringBuffer System out stringBuffer.append ("New value") system.out.printin(string.format ("-35s , stringBuffer)) "with new value: stringbutfer))

Explanation / Answer


In the First Program StringBufferEx we are using String.Format method to format the String. String.format uses the syntax similar to c and C++ like %-35s and %s. So in this program first we are initialising the string buffer with content inital value. So String buffer holds "initial value". then with the line


System.out.println(String.format("%-35s %s", "with intial value: ", stringBuffer));


with above line we are priniting the content of String Buffer but by formatting it by using String.format


"%-35s"    says that left shift the string and total length must be 35 and then %s prints the content of string buffer


So it prints like


first string "with initial value: " then some spaces and total length with string and space must be 35


so it prints "with intial value:                 "                 its lenght must be 35

then from %s content of the string buffer gets prints so finally it prints


"with intial value:                  initial value"

stringBuffer.setLength(0)   sets the length of stringBuffer to Zero

So System.out.println(String.format("%-35s %s", "After setting the length to Zero: ", stringBuffer)); prints
After setting the length to Zero:  

stringBuffer.append("New Value")           appneds the string to the String buffer

System.out.println(String.format("%-35s %s", "with new value: ", stringBuffer));    prints
"with new value:                     New Value"


Program2:

Map map = new HashMap(collection.size());

From above line you are creating a hashMap of initial size equal to collection and with default load factor 0.75.

for(Object obj : collection){
map.put(obj.key, obj.value);
}

you are putting the collection obj in the map with key as obj.key and value as obj.value
as you have created the map with the size equal to collection but as soon as map is full till its deafaul load factor i.e. 75 percent of its size
it doubles the size of the hashmap.

Map map = new HashMap(1 + (int) (collection.size()/0.75))
creating the map with intial size as 1 + (int) (collection.size()/0.75)


Program3:

In the Third Program there is Student class which implements Serializable that means we can serialize its object
In Student Class there are two private members rollNum, studentName
Among these studentName is transient variable

There is class DoNotSerialize we are serializing Student object using Serialize() method
We are creating Student object with rollNum =1 and studentName="Hoffen"
we are writing this object to student.ser file as studentName is transient variable so it can not be serialized
so after serialize() method there will student class seralized object but in that object only rollnum will be serialized not the studentName

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote