0% found this document useful (0 votes)
156 views5 pages

Lab # 04 Home Activity: 1 Write A Java Program To Implement All The Methods of Stringbuffer Class. Answer

The document provides code examples to implement methods of the StringBuffer and StringTokenizer classes in Java, including append, insert, replace, delete, reverse, capacity, ensureCapacity, substring, length, countTokens, nextToken, hasMoreTokens, and nextElement methods. It also contains an example to demonstrate the difference between class and interface anonymous classes in Java by implementing an interface with an anonymous class.

Uploaded by

maha bella
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views5 pages

Lab # 04 Home Activity: 1 Write A Java Program To Implement All The Methods of Stringbuffer Class. Answer

The document provides code examples to implement methods of the StringBuffer and StringTokenizer classes in Java, including append, insert, replace, delete, reverse, capacity, ensureCapacity, substring, length, countTokens, nextToken, hasMoreTokens, and nextElement methods. It also contains an example to demonstrate the difference between class and interface anonymous classes in Java by implementing an interface with an anonymous class.

Uploaded by

maha bella
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

LAB # 04

Home Activity: 1
Write a Java Program to implement all the methods of StringBuffer
Class.

Answer:
import java.lang.StringBuffer;
class StringBufferExample{

public static void main(String args[]){

StringBuffer sb1=new StringBuffer("Hello ");


StringBuffer sb2=new StringBuffer("Hello ");
StringBuffer sb3=new StringBuffer("Hello ");
StringBuffer sb4=new StringBuffer("Hello ");
StringBuffer sb5=new StringBuffer("Hello ");
StringBuffer sb6=new StringBuffer("Hello ");
StringBuffer sb7=new StringBuffer("Hello ");
StringBuffer sb8=new StringBuffer("Hello ");
StringBuffer sb9=new StringBuffer("Hello ");

//1. StringBuffer append() method


sb1.append("Java");//now original string is changed
System.out.println(sb1);//prints Hello Java

//2. StringBuffer insert() method


sb2.insert(1,"Java");//now original string is changed
System.out.println(sb2);//prints HJavaello

//3. StringBuffer replace() method


sb3.replace(1,3,"Java");
System.out.println(sb3);//prints HJavalo

//4. StringBuffer delete() method


sb4.delete(1,3);
System.out.println(sb4);//prints Hlo

//5. StringBuffer reverse() method


sb5.reverse();
System.out.println(sb5);//prints olleH

//6. StringBuffer capacity() method


System.out.println(sb6.capacity());
sb6.append("java is my favourite language");
System.out.println(sb6.capacity());

//7. StringBuffer ensureCapacity() method


System.out.println(sb7.capacity());
sb7.append("java is my favourite language");
System.out.println(sb7.capacity());
sb7.ensureCapacity(20);
System.out.println(sb7.capacity());
sb7.ensureCapacity(60)
System.out.println(sb7.capacity());

//8. substring

System.out.println();
sb8.substring(3);// get substring start from index 3
System.out.println(sb8);//print lo

//9. length-method
System.out.println(sb9.length());//print 6

}
}
Question#2:
Write a Java Program to implement all the methods of
StringTokenizer Class.

Answer:
import java.util.StrinTokenizer;
public class StringTokenizer_methods {

public static void main(String args[])


{
String my_delim = " : ";
String my_str = " A : B : C : D : E";

// Here we are passing Delimiter - "my_delim"


StringTokenizer st = new StringTokenizer(my_str, my_delim);

// Printing count of tokens and tokens


//1) Method is return total tokens count - .countToken()

int count = st.countTokens();


System.out.println("Number of tokens : " + count + "\n");

//2) Method return next token - .nextToken()

for (int i = 0; i <count; i++)


System.out.println("token at [" + i + "] : " + st.nextToken());

//3) Method checks for more Tokens -.hasMoreTokens()


// Here not working as no Tokens left

while (st.hasMoreTokens())

//4) Method is returning next token -.nextToken()

System.out.println(st.nextToken());

// 5) Method: .nextElement(): The method


java.util.StringTokenizer.nextElements() works similar to nextToken except that it
returns Object rather than String.
// 6) Method: .hasMoreElements(): This method
java.util.StringTokenizer.hasMoreElements() returns same value as hasMoreToken.

}
}
LAB#06:
Home Activity# 1:
Consider the following class:
public class IdentifyMyParts {
public static int x = 7;
public int y = 3;
}

a. What are the class variables?


Ans: ‘x’ is the class variable.

b. What are the instance variables?


Ans: ‘y’ is the instance variable.

c. What is the output from the following code:


IdentifyMyParts a = new IdentifyMyParts();
IdentifyMyParts b = new IdentifyMyParts();
a.y = 5;
b.y = 6;
a.x = 1;
b.x = 2;
System.out.println("a.y = " + a.y);
System.out.println("b.y = " + b.y);
System.out.println("a.x = " + a.x);
System.out.println("b.x = " + b.x);
System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x);

Ans:
a.y = 5
b.y = 6
a.x = 2
b.x = 2
IdentifyMyParts.x = 2

Because ’x’ is defined as a public static int in the


class IdentifyMyParts.
Question:2
Write a Java program to show difference between two methods
for anonymous classes – classes and Interfaces.

Answer:

interface Age
{
    int x = 21;
    void getAge();
}
class AnonymousDemo
{
    public static void main(String[] args) 
    {
      MyClass obj=new MyClass();
      obj.getAge();     
    }
}
class MyClass implements Age
{
    @Override
    public void getAge() 
    {
        System.out.print("Age is "+x);
    }
}

You might also like