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

The Programming Project: September 2023

Tuesday, September 19, 2023

ICSE Java Programming Function Overload 2016 Q6 Solved

 Special words are those words which starts and ends with the same letter. 

Examples:
EXISTENCE
COMIC
WINDOW
Palindrome words are those words which read the same from left to right and vice versa
Examples:
MALAYALAM
MADAM
LEVEL
ROTATOR
CIVIC
All palindromes are special words, but all special words are not palindromes. Write a program to accept a word check and print Whether the word is a palindrome or only special word.

import java.util.Scanner;

public class PalindromeOrSpecialWord {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a word: ");
        String word = scanner.nextLine();
        scanner.close();

        if (isPalindrome(word) && isSpecial(word)) {
            System.out.println("The word is both a palindrome and a special word.");
        } else if (isPalindrome(word)) {
            System.out.println("The word is a palindrome.");
        } else if (isSpecial(word)) {
            System.out.println("The word is a special word.");
        } else {
            System.out.println("The word is neither a palindrome nor a special word.");
        }
    }

    // Method to check if a word is a palindrome
    public static boolean isPalindrome(String word) {
        String reversed = "";
        for (int i = word.length() - 1; i >= 0; i--) {
            reversed += word.charAt(i);
        }
        return word.equalsIgnoreCase(reversed);
    }

    // Method to check if a word is a special word
    public static boolean isSpecial(String word) {
        return word.length() >= 2 && word.charAt(0) == word.charAt(word.length() - 1);
    }
}

ICSE Java Programming Function Overload 2016 Q5 Solved

Using the switch statement, write a menu driven program in Java for the following: (i) To print the Floyd’s triangle [Given below] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 (ii) To display the following pattern: I I C I C S I C S E  

For an incorrect option, an appropriate error message should be displayed. 



import java.util.Scanner;

public class PatternCreation {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int choice;

        do {
            System.out.println("Menu:");
            System.out.println("1. Print Floyd's Triangle");
            System.out.println("2. Display Pattern");
            System.out.println("3. Exit");
            System.out.print("Enter your choice (1/2/3): ");
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    printFloydsTriangle();
                    break;
                case 2:
                    displayPattern();
                    break;
                case 3:
                    System.out.println("Exiting the program.");
                    break;
                default:
                    System.out.println("Invalid choice. Please select 1, 2, or 3.");
                    break;
            }
        } while (choice != 3);

        scanner.close();
    }

    // Method to print Floyd's Triangle
    public static void printFloydsTriangle() {
        int n = 1;
        int rows = 5; // You can change the number of rows as needed

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(n + " ");
                n++;
            }
            System.out.println();
        }
    }

    // Method to display the specified pattern
    public static void displayPattern() {
        String pattern = "ICSE";
        for (int i = 0; i < pattern.length(); i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(pattern.charAt(j) + " ");
            }
            System.out.println();
        }
    }
}

ICSE Java Programming Function Overload 2016 Q4 Solved

Define a class named BookFair with the following description: [15] Instance variables/Data members :

String Bname — stores the name of the book double price — stores the price of the book

Member methods : (i) BookFair() — Default constructor to initialize data members

(ii) void Input() — To input and store the name and the price of the book.

(iii) void calculate() — To calculate the price after discount. Discount is calculated based on the following criteria.

(iv) void display() — To display the name and price of the book after discount. Write a main method to create an object of the class and call the above member methods. Price Discount Less than or equal to Rs. 1000 2% of price More than Rs. 1000 and less than or equal to Rs. 3000 10% of price More than % 3000 15% of price


import java.util.Scanner;

class BookFair {

    private String Bname; // stores the name of the book

    private double price; // stores the price of the book

    // Default constructor to initialize data members

    public BookFair() {

        Bname = "";

        price = 0.0;

    }

    // To input and store the name and the price of the book

    public void Input() {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the name of the book: ");

        Bname = scanner.nextLine();

        System.out.print("Enter the price of the book: Rs. ");

        price = scanner.nextDouble();

        scanner.close();

    }

    // To calculate the price after discount based on the given criteria

    public void calculate() {

        if (price <= 1000) {

            price -= (0.02 * price); // 2% discount

        } else if (price > 1000 && price <= 3000) {

            price -= (0.10 * price); // 10% discount

        } else {

            price -= (0.15 * price); // 15% discount

        }

    }

    // To display the name and price of the book after discount

    public void display() {

        System.out.println("Book Name: " + Bname);

        System.out.println("Price after discount: Rs. " + price);

    }

    public static void main(String[] args) {

        BookFair book = new BookFair(); // Create an object of the class

        book.Input(); // Input the name and price of the book

        book.calculate(); // Calculate the price after discount

        book.display(); // Display the book details after discount

    }

}