You are on page 1of 7

A string is Encrypted as follows.

Words that begins with a vowel


are reversed, and all other words remain unchanged.
Design a class Encrypt using the description of the data members
and member functions given below:
Class name: Encrypt
Data Members/instance variables:
Sen: to store a sentence in mixed case
NewSen: to store the encrypted sentence
len: to store the length of the sentence

Member functions: Encrypt (): constructor to initialize the


instance variables
void readSen (): to accept the sentence input
void convert (): encrypts the sentence into its encrypted form
and stores it in string NewSen
void reverse(): to reverse and display the reversed sentence.
For example: Anamika and Susan are friends.
Output: friends are Susan and Anamika .
void display(): displays the original and the encrypted word
Specify the class Encrypt giving the details of the constructor
(), void readSen ( ), void convert (), void reverse() and void
display ().
Define a main () function to create an object and call the
function accordingly to enable the task.

import java.util.Scanner;

public class Encrypt {


private String Sen;
private String NewSen;
private int len;

public Encrypt() {
Sen = "";
NewSen = "";
len = 0;
}

public void readSen() {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a sentence: ");
Sen = scanner.nextLine();
len = Sen.length();
}

public void convert() {


StringBuilder word = new StringBuilder();
for (int i = 0; i < len; i++) {
if (Sen.charAt(i) != ' ') {
word.append(Sen.charAt(i));

1
} else {
if (isVowel(word.charAt(0))) {
reverseWord(word);
}
NewSen += word.toString() + " ";
word.setLength(0);
}
}
// Process the last word in the sentence
if (word.length() > 0) {
if (isVowel(word.charAt(0))) {
reverseWord(word);
}
NewSen += word.toString();
}
}

public void reverseWord(StringBuilder word) {


int start = 0;
int end = word.length() - 1;
while (start < end) {
char temp = word.charAt(start);
word.setCharAt(start, word.charAt(end));
word.setCharAt(end, temp);
start++;
end--;
}
}

public boolean isVowel(char ch) {


ch = Character.toLowerCase(ch);
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
|| ch == 'u');
}

public void reverse() {


System.out.print("Reversed sentence: ");
for (int i = NewSen.length() - 1; i >= 0; i--) {
System.out.print(NewSen.charAt(i));
}
System.out.println();
}

public void display() {


System.out.println("Original sentence: " + Sen);
System.out.println("Encrypted sentence: " + NewSen);
}

public static void main(String[] args) {


Encrypt encryptObj = new Encrypt();
encryptObj.readSen();
encryptObj.convert();

2
encryptObj.display();
encryptObj.reverse();
}
}

program 2
A class DeciHexa has been defined to convert a decimal number
into its equivalent Hexadecimal number. Some of the members of
the class are given below:
Class name: DeciHexa
Data members/instance variables:
n: stores the decimal number
hexa: string to store the hexa decimal equivalent number
Member functions: DeciHexa(): constructor to initialize the data
members n = 0, hexa=†â€
void getnum(int nn): assign nn to n
void deci_hexa(): calculates the hexa decimal equivalent of ‘nâ
€™ and stores it in hexa using the recursive technique
void show(): displays the decimal number ‘n’, calls the
function deci_hexa() and displays its hexa decimal equivalent.
(a) Specify the class DeciHexa, giving details of the
constructor( ), void getnum(int), void deci_hexa( ) and void
show(). Also define a main() function to create an object and
call the functions accordingly to enable the task. ​​[8]
(b) State any two disadvantages of using recursion.

import java.util.Scanner;

public class DeciHexa {


private int n;
private String hexa;

public DeciHexa() {
n = 0;
hexa = "";
}

public void getnum(int nn) {


n = nn;
}

public void deci_hexa() {


hexa = ""; // Initialize hexa to an empty string before
the recursive call
convertToHexadecimal(n);
}

3
private void convertToHexadecimal(int decimal) {
if (decimal != 0) {
int remainder = decimal % 16;
convertToHexadecimal(decimal / 16);
if (remainder < 10) {
hexa += Integer.toString(remainder);
} else {
hexa += (char) ('A' + remainder - 10);
}
}
}

public void show() {


System.out.println("Decimal number: " + n);
deci_hexa(); // Calculate hexadecimal before displaying
System.out.println("Hexadecimal equivalent: " + hexa);
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a decimal number: ");
int num = scanner.nextInt();

DeciHexa deciHexaObj = new DeciHexa();


deciHexaObj.getnum(num);
deciHexaObj.show();
}
}

program 3
A class ArrayMin contains a square matrix which finds the
smallest element in each column.
Some of the members of the class are given below:
Class name : ArrayMin
Data members/instance variables:
arr[ ] [ ] : array to store character elements
m : to store the order of the matrix
Member functions/methods:
ArrayMin(int mm ) : parameterized constructor to initialize the
size of the matrix, m=mm and to declare the array
void readArray() : to accept the array elements
void small( ) : finds and displays the smallest element in each
column with appropriate message.
int sumofarray() : finds and returns the sum of the elements
whose ASCII codes are even.
void display( ) : to display the array in a matrix form, and also
displays the sum of the elements whose ascii code is even by
invoking sumofarray(). And also displays the array elements
equivalent ASCII codes in matrix form.
For example:

4
If array arr[][]= A B C
D E F
G H I
Its equivalent ASCII code in matrix form
65 66 67
68 69 70
71 72 73
Specify the class ArrayMin giving the details of readArray(),
small(), sumofarray(), display(). Define a main( ) function to
create an object and call all the functions accordingly to enable
the task..

import java.util.Scanner;

public class ArrayMin {


private char[][] arr;
private int m;

public ArrayMin(int mm) {


m = mm;
arr = new char[m][m];
}

public void readArray() {


Scanner scanner = new Scanner(System.in);
System.out.println("Enter the array elements:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = scanner.next().charAt(0);
}
}
}

public void small() {


System.out.println("Smallest element in each column:");
for (int j = 0; j < m; j++) {
char min = arr[0][j];
for (int i = 1; i < m; i++) {
if (arr[i][j] < min) {
min = arr[i][j];
}
}
System.out.println("Column " + (j + 1) + ": " + min);
}
}

public int sumofarray() {


int sum = 0;

5
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
if ((int) arr[i][j] % 2 == 0) {
sum += (int) arr[i][j];
}
}
}
return sum;
}

public void display() {


System.out.println("Array in matrix form:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}

System.out.println("\nEquivalent ASCII code in matrix


form:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
System.out.print((int) arr[i][j] + " ");
}
System.out.println();
}

int sum = sumofarray();


System.out.println("\nSum of elements with even ASCII
codes: " + sum);
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter the order of the matrix: ");
int order = scanner.nextInt();

ArrayMin arrayMinObj = new ArrayMin(order);


arrayMinObj.readArray();
arrayMinObj.small();
arrayMinObj.display();
}
}

6
7

You might also like