ICSE Class 10 Computer Applications Question 15 of 16

Library Classes — Question 15

Back to all questions
15
Question

Question 10

Define a class (using the Scanner class) to generate a pattern of a word in the form of a triangle or in the form of a­n inverted triangle, depending upon user's choice.

Sample Input:

Enter a word: CLASS  
Enter your choice: 1	    
Sample Output:	            
C
CL
CLA
CLAS
CLASS

Enter your choice: 2
Sample Output:
CLASS
CLAS
CLA
CL
C
import java.util.Scanner;

public class KboatTriangleMenu
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a word: ");
        String word = in.nextLine();
        System.out.println("Type 1 for a triangle");
        System.out.println("Type 2 for an inverted triangle");
        System.out.print("Enter your choice: ");
        int choice = in.nextInt();
        int len = word.length();
        
        switch (choice) {
            case 1:
                for(int i = 0; i < len; i++) {
                    for(int j = 0; j <= i; j++) {
                        System.out.print(word.charAt(j));
                    }
                    System.out.println();
                }
                break;
                
            case 2:
                for (int i = len - 1; i >= 0; i--) {
                    for (int j = 0; j <= i; j++) {
                        char ch = word.charAt(j);
                        System.out.print(ch);
                    }
                    System.out.println();
                }
                break;
                
            default:
                System.out.println("Incorrect choice");
                break;
        }
    }
}
Output
BlueJ output of KboatTriangleMenu.java
BlueJ output of KboatTriangleMenu.java
Answer

Source: This question is from Library Classes, Computer Applications — Class 10, ICSE Board.

Key Concepts Covered

This question tests your understanding of the following concepts from the chapter Library Classes: Question, Class, Scanner, Generate, Pattern, Word. These are fundamental topics in Computer Applications that students are expected to master as part of the ICSE Class 10 curriculum.

A thorough understanding of these concepts will help you answer similar questions confidently in your ICSE examinations. These topics are frequently tested in both objective and subjective sections of Computer Applications papers. We recommend revising the relevant section of your textbook alongside practising these solved examples to build a strong foundation.

How to Approach This Question

Read the question carefully and identify what is being asked. Break down complex questions into smaller parts. Use the terminology and concepts discussed in this chapter. Structure your answer logically — begin with a definition or key statement, then provide supporting details. Review your answer to ensure it addresses all parts of the question completely.

Key Points to Remember

  • Write programs with proper indentation and comments.
  • Trace through your code with sample inputs to verify correctness.
  • Explain the logic behind each step of your solution.
  • Familiarise yourself with common library functions and methods.

Practice more questions from Library Classes — Computer Applications, Class 10 ICSE