PRASHNIKAप्रश्निका
Back to the paper

Design a class Coding to perform some string related operations on a word containing alphabets…

Computer Science202410 marksProgram
Design a class Coding to perform some string related operations on a word containing alphabets only. Example: Input: “Java” Output: Original word: Java J = 74 a = 97 v = 118 a = 97 Lowest ASCII code: 74 Highest ASCII code: 118 Some of the members of the class are given below. Class name : Coding Data members/instance variables: wrd : stores the word len : stores the length of the word Methods / Member functions: Coding() : constructor to initialise the data members with legal initial values void accept( ) : to accept a word void find() : to display all the characters of ‘wrd’ along with their ASCII codes. Also display the lowest ASCII code and the highest ASCII code, in ‘wrd’ void show( ) : to display the original word and all the characters of ‘wrd’ along with their ASCII codes. Also display the lowest ASCII code and the highest ASCII code in ‘wrd’, by invoking the function find( ) Specify the class Coding giving details of the constructor( ), void accept( ), void find( ) and void show( ). Define a main() function to create an object and call all the functions accordingly to enable the task.

Answer

Answer

AI
import java.util.Scanner;

class Coding
{
    String wrd;
    int len;

    Coding()
    {
        wrd = "";
        len = 0;
    }

    void accept()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a word (alphabets only): ");
        wrd = sc.next();
        len = wrd.length();
    }

    void find()
    {
        int lowest = wrd.charAt(0);
        int highest = wrd.charAt(0);
        for (int i = 0; i < len; i++)
        {
            char ch = wrd.charAt(i);
            int code = (int) ch;
            System.out.println(ch + " = " + code);
            if (code < lowest)
                lowest = code;
            if (code > highest)
                highest = code;
        }
        System.out.println("Lowest ASCII code: " + lowest);
        System.out.println("Highest ASCII code: " + highest);
    }

    void show()
    {
        System.out.println("Original word: " + wrd);
        find();
    }

    public static void main(String[] args)
    {
        Coding obj = new Coding();
        obj.accept();
        obj.show();
    }
}
Explanation: accept() reads the word and stores its length in len. find() loops through each character of wrd, prints the character with its ASCII code, and tracks the lowest and highest ASCII codes seen; these are printed at the end. show() prints the original word and then calls find(). Tested with input "Java": output is exactly as specified - J=74, a=97, v=118, a=97, Lowest ASCII code: 74, Highest ASCII code: 118.
Arrays, Strings

From ISC 2024 Computer Science Paper 1, question 8.