Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

import java.util.

Scanner;

public class Change {


String str;
String newstr;
int len;

public Change() {
str = "";
newstr = "";
len = 0;
}

public void inputWord() {


Scanner scanner = new Scanner(System.in);
System.out.println("Enter the word:");
str = scanner.nextLine();
scanner.close();
}

public char caseConvert(char ch) {


if (ch >= 'A' && ch <= 'Z') {
return (char) (ch + 32);
} else if (ch >= 'a' && ch <= 'z') {
return (char) (ch - 32);
} else {
return ch;
}
}

public void recChange(int index) {


if (index < str.length()) {
char ch = str.charAt(index);
newstr += caseConvert(ch);
recChange(index + 1);
}
}

public void display() {


System.out.println("Original word: " + str);
System.out.println("Changed word: " + newstr);
}

public static void main(String[] args) {


Change changer = new Change();
changer.inputWord();
changer.recChange(0);
changer.display();
}
}

QUESTION-2 [2011]
A class DeciOct has been defined to convert a decimal number into its equivalent
octal number. Some of the members of the class are given below:
Class name: DeciOct

Data members/instance variables:


N : stores the decimal number
Oct : stores the octal equivalent number
Member functions:
DeciOct() : constructor to initialize the data members n = 0, oct = 0.

void getnum(int nn) : assign nn to n


void deci_oct() : calculates the octal equivalent of ‘n’ and stores it
in
oct using the recursive technique
void show() : displays the decimal number ‘n’, calls the function
deci_oct() and displays its octal equivalent.

(a) Specify the class DeciOct, giving details of the constructor( ), void getnum(int),
void deci_oct( ) and void show(). Also define a main() function to create an object
and call the functions accordingly to enable the task.
(b) State any two disadvantages of using recursion.

You might also like