Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ICSE Java Programming Array of Strings 2023 Q8 SPECIMEN PAPER Solved

Friday, December 30, 2022

ICSE Java Programming Array of Strings 2023 Q8 SPECIMEN PAPER Solved

ICSE Java Programming Array of Strings 2023 Q8 SPECIMEN PAPER solved


Define a class to accept the names of 10 students in an array and check for the existence of the given name in the array using linear search, if found print the position of the name, if not found print the appropriate message. Also print the names which begins with the word "SRI".






import java.util.Scanner;

public class ICSEJava2023 {
    public static void main(String[] args) {
        String s = "";
        Scanner in = new Scanner(System.in);
        Name obj = new Name();
        obj.inputNames();
        System.out.println("Enter the name to be searched:");
        s = in.nextLine();
        obj.searchName(s);
        obj.checkName();
        in.close();
    }
}

class Name {
    public void checkName() {
        for (int i = 0; i < this.total_students; i++) {
            if (this.names[i].charAt(0) == 'S' && this.names[i].charAt(1) == 'R' && this.names[i].charAt(2) == 'I')
                System.out.println(this.names[i] + " begins with SRI");
        }
    }
    public void inputNames() {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the names:");
        for (int i = 0; i < this.total_students; i++) {
            System.out.println("Enter the name at position:" + (i + 1));
            this.names[i] = in.nextLine();
        }
    }
    public void searchName(String sname) {
        boolean flag = false;
        for (int i = 0; i < this.total_students; i++) {
            if (sname.equals(this.names[i])) {
                System.out.println("Position of the entered name in the array is: " + (i + 1));
                flag = true;
                break;
            }
        }
        if (flag == false)
            System.out.println("Name not found in the list");
    }
    Name() {
        this.names = new String[this.total_students];
    }
    private static int total_students = 10;
    private String[] names;
}

No comments:

Post a Comment