icse-promo

Question

Design a class Change to perform string related operations . The details of the class are given below:
Classname: Change
Data Members / instance variables:
str :stores the word
newstr : stores the changed word
len: store the length of the word
Memberfunctions:
Change():default constructor
void inputword():to accept a word
char caseconvert(char ch) : converts the case of the character and returns it
void recchange(int):extracts characters using recursive technique and changes its case using caseconvert() and forms a new word
void display():displays both the words
(a) Specify the class Change, giving details of the Constructor(),member functions
void inputword(), char caseconvert(char ch),void recchange(int) and void display().
Define the main() function to create an object and call the functions accordingly to enable the above change in the given word.

 

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class Change
{
    String str,newstr;
    int len;
    Scanner sc = new Scanner(System.in);

 
    public Change()
    { 
        str="";
        newstr="";
        len=0;
    }
    public void inputword()
    {	
        System.out.println("ENTER A Word");
        str=sc.next();
        len=str.length();
    }
    public char caseconvert(char ch)
    {    
        if(ch>='A' && ch<='Z')
        {
            ch=Character.toLowerCase(ch);
        }
        else if(ch>='a' &&ch<='z')
        {
            ch=Character.toUpperCase(ch);
        }
        return ch;
    }

    public void recchange(int pos)
    {	
        char c=' ';
        if(pos>-1)
        {
            c=str.charAt(pos);
            recchange(pos-1);
            newstr+=caseconvert(c);
        }
    }
    public void display()
    {
        System.out.println("ChangedWord:"+newstr);
        System.out.println("OriginalWord:"+str);
    }
    public static void main()
    { 
        Change ob1=new Change();
        ob1.inputword();
        int length=ob1.len;
        ob1.recchange(length-1);
        ob1.display();
    }
}





				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *