Program to check if String is palindrome
Program to reverse a string
Program to implement a LinkList
Program to open a file and write or read something in file using java
Questions on Hashmap
Questions on Design patterns
Strategy and State design Pattern,singleton,etc
Program to reverse a string
new StringBuilder(“meeraja”).reverse().toString();
String s = "sample";
String result = new StringBuffer(s).reverse().toString();
import java.util.*;
class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);
System.out.println("Reverse of entered string is: "+reverse);
}
}
import java.util.*;
public class LinkedListDemo {
public static void main(String args[]) {
// create a linked list
LinkedList ll = new LinkedList();
// add elements to the linked list
ll.add("F");
ll.add("B");
ll.add("D");
ll.add("E");
ll.add("C");
ll.addLast("Z");
ll.addFirst("A");
ll.add(1, "A2");
System.out.println("Original contents of ll: " + ll);
// remove elements from the linked list
ll.remove("F");
ll.remove(2);
System.out.println("Contents of ll after deletion: "
+ ll);
// remove first and last elements
ll.removeFirst();
ll.removeLast();
System.out.println("ll after deleting first and last: "
+ ll);
Program to open a file and write or read something in file using java
import java.io.*;
public class FileRead{
public static void main(String args[])throws IOException{
File file = new File("Hello1.txt");
// creates the file
file.createNewFile();
// creates a FileWriter Object
FileWriter writer = new FileWriter(file);
// Writes the content to the file
writer.write("This\n is\n an\n example\n");
writer.flush();
writer.close();
//Creates a FileReader Object
FileReader fr = new FileReader(file);
char [] a = new char[50];
fr.read(a); // reads the content to the array
for(char c : a)
System.out.print(c); //prints the characters one by one
fr.close();
}
}
HashMap works on the principle of hashing
put(key, value): HashMap stores both key and value object as
Map.Entry. Hashmap applies hashcode(key) to get the bucket. if there is
collision ,HashMap uses LinkedList to store object.
get(key): HashMap uses Key Object's hashcode to find
out bucket location and then call keys.equals() method to identify correct node
in LinkedList and return associated value object for that key in Java HashMap
Difference between equals and ==
In Java,
when the “==” operator is used to compare 2 objects, it checks to see if the
objects refer to the same place in memory
String obj1 = new String("xyz");
String obj2 = new String("xyz");
if(obj1 == obj2)
System.out.println("obj1==obj2 is TRUE");
else
System.out.println("obj1==obj2 is FALSE");
o/p false
String obj1 = new String("xyz");
String obj2 = obj1;
if(obj1 == obj2)
System.out.println("obj1==obj2 is TRUE");
else
System.out.println("obj1==obj2 is FALSE");
o/p true
String obj1 = new String("xyz");
String obj2 = new String("xyz");
if(obj1.equals(obj2))
System.out.printlln("obj1==obj2 is TRUE");
else
System.out.println("obj1==obj2 is FALSE");
Output true
Strategy and State design Pattern,singleton,etc
Design
patterns are useful because they provide a pre-formulated solution to problems
based on the experience of other programmers. This can save you a lot of time.
how to solve a
problem in different situations.
Singleton
pattern is one of the design patterns that is utilized for restricting
instantiation of a class to one or few specific objects................
Process pattern:
Methods, best practices, techniques for developing an Object-Oriented software
comprises a process pattern..
Access modifiers in java
·
Visible to the package. the default. No modifiers are needed.
·
Visible to the class only (private).
·
Visible to the world (public).
·
Visible to the package and all subclasses (protected).
Class and interfaces cannot be private.
Using the private modifier is the main way that an object
encapsulates itself and hide data from the outside world.
The main() method of an application has to be public.
Otherwise, it could not be called by a Java interpreter (such as java) to run
the class.
The protected access modifier cannot be applied to class and
interfaces. Methods, fields can be declared protected, however methods and
fields in a interface cannot be declared protected.