Assignment No 01
THEORY:
Features Of Java
Java is an object-oriented, multi-threaded programming language developed by Sun Microsystems in 1991. Java Development Environment consists of Java complier and a Java interpreter. Java complier generates byte code instead of machine code and Java interpreter executes the Java program. The disadvantage of using byte code is the execution speed. In order to write Java program, an editor, a java complier and a java runtime Environment are needed.
The important features of java are
- Simple and powerful
- Secure and Portable
- Robust
- Multithreaded
- Distributed and dynamic
- Object-oriented
String Operations
A combination of characters is a string. Strings are instances of the class String. The addition operator(+) can be used to create and concate the strings.
String names[ ] ={“anand”, ”Rahul”, ”sachin”};
Calling the default constructor with no parameters can create an empty string.
String s= new String( );
In order to create a string initialized with characters, we need to pass an array of char to the constructor.
char chars [ ] ={‘a’, ’b’, ’c’);
String s= new String (chars);
System.out.println(s);
Some String methods are :
Method
|
Use
|
s.Length( )
|
Length of the string
|
s.charAt( index)
|
Character at a location given by index
|
s.equals(s1), s.equalsIgnoreCase(s1 )
|
An equality checking for two strings
|
s.compareTo(s1)
|
Result is negative, positive or zero depending on the lexicographical ordering
|
s.indexOf(char c), s.lastIndexOf(char c )
|
Returns the index or –1 if the character not found.
|
s.substring( )
|
Returns a new string object containing the required character set.
|
s.concat( )
|
Returns a new string object with the concatenation done.
|
s.replace( )
|
Returns a new string object with the replacement done.
|
s.toLowercase( ), s.toUpperCase( )
|
Returns a new string object with the
Lowercase case or uppercase change.
|
s.trim( )
|
Returns a new string object with the white spaces removed from each end.
|
Strings represent fixed length character sequences and StringBuffer represents variable length character sequences.
StringBuffer sb=new StringBuffer(“abc”);
sb.append (“xyz”);
//the argument gets appended to the end of the current StringBuffer.
Casting is used to convert the value of an object or primitive type into another type. Conversion of primitive types cannot be done explicitly.
CONCLUSION
Various String operations such as calculating the length of the string, reverse the string, palindrome, concatenation of the string, finding the substring are successfully created.
import java.io.*; public class Main { public Main() { } public static void main(String[] args) throws Exception{ BufferedReader ss=new BufferedReader (new InputStreamReader(System.in)); System.out.println("enter the desired string:\t"); String str=ss.readLine(); BufferedReader b= new BufferedReader(new InputStreamReader (System.in)); int choice,temp; String c1,c2; do{ System.out.println("select desired option:\n1.length \n2.character At.\n3.equality\n4.compare\n5.index of char.\n6.last index of char.\n7.substring\n8.concat\n9.replace\n10.to lowercase\n11.trim \n12. Reverse"); choice=Integer.parseInt (b.readLine()); switch(choice) { case 1: System.out.println("String length is "+str.length()+"\n\n"); break; case 2: System.out.println("select index to determine character: "); temp=Integer.parseInt (b.readLine()); System.out.println("CHARACTER AT INDEX " +temp+ " is "+ str.charAt(temp-1)); break; case 3: System.out.println("ENTER SECOND STRING: "); String str2= ss.readLine(); System.out.println("the condition that strings "+str+" and "+ str2+" are equal is: "+ str.equalsIgnoreCase(str2)); break; case 4: System.out.println("ENTER SECOND STRING: "); str2= ss.readLine(); temp=str.compareTo(str2); if(temp==0) System.out.println("THE STRINGS ARE EQUAL "); else if(temp>0) System.out.println("THE STRING "+str+" is greater than the string "+str2); else System.out.println("THE STRING "+str2+" is greater than the string "+str); break; case 5: System.out.println("ENTER CHAR: "); str2=ss.readLine(); System.out.println(str.indexOf(str2)); break; case 6: System.out.println("ENTER CHAR: "); str2=ss.readLine(); System.out.println(str.lastIndexOf(str2)); break; case 7: System.out.println("ENTER SUBSTRINGs STARTING AND ENDING INDEX : "); int start=Integer.parseInt(ss.readLine()); int end=Integer.parseInt(ss.readLine()); System.out.println(str.substring(start,end)); break; case 8: System.out.println("ENTER SECOND STRING: "); str2= ss.readLine(); System.out.println(str.concat(str2)); break; case 9: System.out.println("ENTER CHAR TO BE REPLACED: "); c1= ss.readLine(); System.out.println("ENTER CHAR TO REPLACE: "); c2= ss.readLine(); System.out.println(str.replace(c1,c2)); break; case 10: System.out.println(str.toLowerCase()); break; case 11: System.out.println(str.trim()); break; case 12: StringBuffer sb = new StringBuffer(str); System.out.println(sb.reverse()); break; default: System.out.println(“Invalid Choice”); } }while(choice!=12); } }
Leave a Reply