Lowest & Highest ASCII Code Java Program | ISC Computer Science 2024 Theory

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 initialize 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.

import java.util.Scanner;
class Coding{
    String wrd;
    int len;
    public Coding(){
        wrd = "";
        len = 0;
    }
    public void accept(){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the word: ");
        wrd = in.next();
        len = wrd.length();
    }
    public void find(){
        if(len == 0)
            return;
        int low = wrd.charAt(0);
        int high = wrd.charAt(0);
        for(int i = 0; i < len; i++){
            char ch = wrd.charAt(i);
            int ascii = (int)ch;
            if(low > ascii)
                low = ascii;
            if(high < ascii)
                high = ascii;
            System.out.println(ch + " = " + ascii);
        }
        System.out.println("Lowest ASCII code: " + low);
        System.out.println("Highest ASCII code: " + high);
    }
    public void show(){
        System.out.println("Original word: " + wrd);
        find();
    }
    public static void main(String[] args) {
        Coding obj = new Coding();
        obj.accept();
        obj.show();
    }
}

Leave a Reply

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