You are on page 1of 7

Dt : 25/5/2022

faq:

define toString() method?

=>toString() method is used to display the data from the Object.

Method Signature:

public java.lang.String toString();

syntax:

String data = str.toString();

Execution Behaviour of toString():

=>This toString() method is executed automatically when we

display Object reference Variable.

Note:

=>This toString() method is available in String classes,

WrapperClasses,Collection<E> classes,Map<K,V> classes and

Enum<E> class.

----------------------------------------------------

faq:

define length() method?

=>length() method is used to display the length of String.

Method Signature:
public int length();

syntax:

int len = str.length();

-----------------------------------------------------

faq:

define charAt() method?

=>charAt() method is used to retrieve characters from the String

based on index values.

Method Signature:

public char charAt(int);

syntax:

char ch = str.charAt(index);

---------------------------------------------------------

faq:

define ASCII Codes?

=>ASCII stands for American Standard Code for Information

Interchange and every character from Keyword input will have its

own ASCII value.

UpperCase Letters : 65 to 90

LowerCase Letters : 97 to 122

Numbers(0 to 9) : 48 to 57
Ex : DemoString2.java

package p1;
public class DemoString2 {
public static void main(String[] args) {
System.out.println("====UpperCase Letters====");
for(int i=65;i<=90;i++)
{
char ch = (char)i;//ASCII value into char
System.out.print(ch+" ");
}//end of loop
System.out.println("\n====LowerCase Letters===");
for(int i=97;i<=122;i++)
{
char ch = (char)i;//ASCII value into char
System.out.print(ch+" ");
}
System.out.println("\n===Numbes(0-9)===");
for(int i=48;i<=57;i++)
{
char ch = (char)i; //ASCII value into char
System.out.print(ch+" ");
}
}
}

o/p:

====UpperCase Letters====

ABCDEFGHIJKLMNOPQRSTUVWXYZ

====LowerCase Letters===

abcdefghijklmnopqrstuvwxyz

===Numbes(0-9)===

0123456789

=====================================================

Ex:wap to read and String display,


Number of Vowels

Number of Consonents

Program : DemoString3.java

package p1;
import java.util.*;
public class DemoString3 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the String:");
String str = s.nextLine();
int len = str.length();
System.out.println("===Normal String===");
System.out.println(str.toString());
System.out.println("length:"+len);
int vcount=0,ccount=0;
for(int i=0;i<=len-1;i++)
{
char ch = str.charAt(i);//Char based on index
switch(ch)
{
case 'a':
case 'A':vcount++;
break;
case 'e':
case 'E':vcount++;
break;
case 'i':
case 'I':vcount++;
break;
case 'o':
case 'O':vcount++;
break;
case 'u':
case 'U':vcount++;
break;
}//end of switch
int k = (int)ch;//Char into ASCII
if((k>=65 && k<=90) || (k>=97 && k<=122))
{
ccount++;
}
}//end of loop
System.out.println("Number of Vowels:"+vcount);
System.out.println
("Number of Consonents:"+(ccount-vcount));
s.close();
}
}

o/p:

Enter the String:

java17 version by 2021 sept

===Normal String===

java17 version by 2021 sept

length:27

Number of Vowels:6

Number of Consonents:11

=========================================================

Assignment:

wap to read a String and display the following,

Number of vowels

Number of Consonents

Number of Numbers

Sum of Numbers

Number of others

I/P : java17 version by 2021 sept - 90% of WebDevelopment

Vowels : 12

Consonents : 21
Nubers : 1 7 2 0 2 1 9 0

sum = 22

Others : 10

========================================================

faq:

define substring() method?

=>substring() method is used to take the part of string based

on index values.

Method Signature:

public java.lang.String substring(int);

public java.lang.String substring(int,int);

Program : DemoString4.java

package p1;
public class DemoString4 {
public static void main(String[] args) {
String str = "java language";
String s1 = str.substring(5);
//SubString taken from index 4
System.out.println("s1:"+s1.toString());
String s2 = str.substring(2,8);
//substring taken from index 2 to 7
System.out.println("s2:"+s2.toString());
}
}

====================================================

Assignment:

wap to read Student RollNo


(i)length must be 10,else invalid rollNo

(ii)rollNo length is 10,then display the following details

Year of Joining

CollCode

BranchCode

No

==========================================================

You might also like