java unit 4 tt2
java unit 4 tt2
Que-1- write a java program to check whether the entered string is palindrome
or not (5 marks)
class ChkPalindrome
{
public static void main(String args[])
{
String str, rev = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
str = sc.nextLine();
if (str.equals(rev))
System.out.println(str+" is a palindrome");
else
System.out.println(str+" is not a palindrome");
}
}
Program Output:
Enter a string:
radar
radar is a palindrome
Que-2- What is string? Explain with example (5 marks)
char[] ch = {'t','e','c','h','t','a','r','g','e','t'};
String s2 = new String (ch);
String s2 = "techtarget";
The following shows the use of valueOf() for determining the value of a
letter in the above char array ch.
public class StringValueOfExample5 {
public static void main(String[] args) {
char[] ch =
{'t','e','c','h','t','a','r','g','e','t'};
String s2 = new String (ch);
String v = String.valueOf(ch[1]);
System.out.println(v);
}
}
The example above prints the value of the first position in the character
array ch. The exact print statement is: e.
Utility methods like valueOf() and toLowerCase() are only used with
strings that are immutable. They perform operations on strings, but they do
not change the string itself. To create mutable strings that can be changed,
the StringBuilder() and StringBuffer() classes are used.
class Example {
public static void main(String args[]) {
StringBuilder x = new StringBuilder("Ben ");
x.append("eats vegetables");
System.out.println(x);
}
}
class Example{
public static void main (String args[]) {
StringBuilder x = new StringBuilder ("Ben eats
vegetables");
x.reverse();
System.out.println(x);
}
}
1. Delete =
The delete() method of the StringBuffer class deletes the string from the
specified beginIndex to endIndex-1.
import java.io.*;
class A {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello");
sb.delete(0, 2);
System.out.println(sb);
}
}
Output : llo
2. Insert=
The insert() method inserts the given string with this string at the given
position.
import java.io.*;
class A {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello ");
sb.insert(1, "Java");
// Now original string is changed
System.out.println(sb);
}
}
Output : HJavaello
3. Append=
The append() method concatenates the given argument with this string.
import java.io.*;
class A {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello ");
sb.append("Java");
// now original string is changed
System.out.println(sb);
}
}
Output : Hello Java
4.Replace=
The replace() method replaces the given string from the specified beginIndex and
endIndex-1.
import java.io.*;
class A {
sb.replace(0, 3, "Java");
System.out.println(sb);
Output : Javalo
5.Reverse=
The reverse() method of the StringBuilder class reverses the current string.
import java.io.* ;
class A {
sb.reverse();
System.out.println(sb);
Output : olleH
Que-4- What is Generics class? Explain with example (5 marks)
Ans=A Generic class simply means that the items or functions in that
class can be generalized with the parameter(example T) to specify that we
can add any type as a parameter in place of T like Integer, Character,
String, Double or any other user-defined type.
Example: Single type parameter
class Solution<T>
{
T data;
public static T getData(){
return data;
}
}
public class Pair<K, V> {
private K key;
private V value;
Here in this example, we can use the object or instance of this class as
many times with different Parameters as T type. If we want the data to be
of int type, the T can be replaced with Integer, and similarly for String,
Character, Float, or any user-defined type. The declaration of a generic
class is almost the same as that of a non-generic class except the class
name is followed by a type parameter section. The type parameter section
of a generic class can have one or more type parameters separated by
commas.
We can write a single generic method declaration that can be called with
arguments of different types. Based on the types of arguments passed to
the generic method, the compiler handles each method call appropriately.
Following are the rules to define Generic Methods
All generic method declarations have a type parameter section
indicated by angle brackets <> that precedes the method’s return type.
Each type parameter section can contain one or more type
parameters separated by commas. A type parameter or a type
variable is an identifier that specifies a generic type name.
The type parameters can be used to declare the return type which is
known as actual type arguments.
A generic method’s body is declared like that of any non-generic
method. The point to be noted is that type parameter can represent
only reference types, and not primitive types (like int, double, and
char).
Advantages of Java Generics
1. Type-Safety: One can hold only a single type of objects in generics.
2. Type Casting Is Not Required: There is no need to typecast.
Example:
Before Generics
List l= new ArrayList();
l.add("India");
String s = (String) l.get(0); // typecasting
Que-5- Write a java program to perform below operations of string input given
by the user.
Output:
1. class Teststringcomparison1{
2. public static void main(String args[]){
3. String s1="Sachin";
4. String s2="Sachin";
5. String s3=new String("Sachin");
6. String s4="Saurav";
7. System.out.println(s1.equals(s2));//true
8. System.out.println(s1.equals(s3));//true
9. System.out.println(s1.equals(s4));//false
10. }
11. }
Output:
true
true
false
Java Collections can achieve all the operations that you perform on a data
such as searching, sorting, insertion, manipulation, and deletion.
There are many methods declared in the Collection interface. They are as
follows:
Que-7- Explain difference between string and stringbuffer class with example
(10 marks)
Ans=
ConcatTest.java
Output:
Output:
Integer Result: 5
Double Result: 6.0
Ans= There may be times when you want to restrict the types that can be
used as type arguments in a parameterized type. For example, a method
that operates on numbers might only want to accept instances
of Number or its subclasses. This is what bounded type parameters are for.
private T t;
public T get() {
return t;
}
private T n;
// ...
}
Multiple Bounds
The preceding example illustrates the use of a type parameter with a single
bound, but a type parameter can have multiple bounds:
A type variable with multiple bounds is a subtype of all the types listed in
the bound. If one of the bounds is a class, it must be specified first. For
example:
Class A { /* ... */ }
interface B { /* ... */ }
interface C { /* ... */ }
Type safety:
Code reuse:
Generics in Java allow you to write code that is more reusable. By specifying
a type parameter in a generic class, method, or interface, you can create
code that can work with multiple data types. This reduces code duplication,
improves code maintainability, and makes your code more efficient.
Compile-time checking:
Improved performance:
Generics in Java can also improve the performance of your program. By
using generics, the compiler can optimize your code, reducing the number
of casts and improving the overall performance of your program.
Greater flexibility:
Better documentation:
Avoiding casting:
Easier debugging:
Interoperability:
Generics in Java also make it easier to write code that can work with other
Java libraries and frameworks. By using generics, you can ensure that your
code is compatible with other Java code that uses generics, making it easier
to integrate your code with other software and systems.
Cleaner code:
Finally, generics in Java can help you write cleaner, more elegant code. By
using generics to create type-safe classes, methods, and interfaces, you can
avoid messy code that relies on casting, type checking, and other error-
prone techniques. This can make your code more readable, maintainable,
and efficient, and can improve the overall quality of your Java programs.