The Programming Project: JAVA
Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

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

    }

}

Sunday, April 30, 2023

ICSE Java Programming Function Overload 2017 Q8 Solved

ICSE Java Programming Design a class to overload a function 2017 Q8 Solved 



import java.util.Scanner;

public class ICSEJava {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s;
        char c;
        System.out.println("Enter the string");
        s = in.nextLine();
        System.out.println("Enter the character");
        c = in.nextLine().charAt(0);
        StringCheck obj = new StringCheck();
        obj.check(s, c);
        obj.check(s);
        in.close();
    }
}

class StringCheck {
    public void check(String str, char ch) {
        for (int i = 0; i < str.length(); i++) {
            if (ch == str.charAt(i))
                counter++;
        }
        System.out.println("Number of times " + ch + " present is " + counter);
    }

    public void check(String str) {
        str = str.toLowerCase();
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o'
                    || str.charAt(i) == 'u')
                System.out.print(str.charAt(i) + " ");
        }
    }

    StringCheck() {
        counter = 0;
    }

    private int counter;
}

ICSE Java Programming Switch Case 2017 Q7 Solved

 

ICSE Java Programming  2017 Q7 Solved

Finding the sum, largest and the smallest element of an array with 20 integer elements.




import java.util.Scanner;

public class ICSEJava {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] array = new int[20];
        int sum = 0;
        int max = 0;
        int min = 0;
        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < 20; i++) {
            System.out.println("Enter the element at the position:" + (i + 1));
            array[i] = in.nextInt();
            sum += array[i];
        }
        max = array[0];
        min = array[0];
        for (int i = 0; i < 20; i++) {
            if (max < array[i])
                max = array[i];
            if (min > array[i])
                min = array[i];
        }
        System.out.println("Sum of the elements of the array is "+sum);
        System.out.println("Largest number  of the array is "+max);
        System.out.println("Smallest number of the array is "+min);
        in.close();
    }
}



Thursday, January 12, 2023

ICSE Java Programming Switch Case 2017 Q6 Solved

ICSE Java Programming Switch Case  2017 Q6  Solved Computer Application



import java.util.Scanner;

public class ICSEJava {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int choice;
        double sum = 0;
        System.out.println("Enter 1 to calculate the sum.");
        System.out.println("Enter 2 to print the series.");
        System.out.println("Enter your choice:");
        choice = in.nextInt();
        switch (choice) {
            case 1:
                for (int i = 1; i <= 5; i++) {
                    sum += Math.pow(2, i) * Math.pow(-1, (double) (i + 1));
                }
                sum -= Math.pow(2, 20);
                System.out.println("The sum is: " + sum);
                break;
            case 2:
                int n;
                System.out.println("Enter the number of terms:");
                n = in.nextInt();
                for (int j = 1; j <= n; j++) {
                    for (int k = 1; k <= j; k++)
                        System.out.print("1");
                    System.out.print("  ");
                }
                break;
            default:
                System.out.println("Wrong choice.");
                break;
        }
        in.close();

    }
}

Wednesday, January 11, 2023

ICSE Java Programming Electricity Bill 2017 Q4 Solved

ICSE Java Programming Electricity Bill  2017 Q4  Solved

Question 4.

Define a class Electricity Bill with the following specifications : [15]
class : ElectricBill
Instance variables /data member :
String n – to store the name of the customer
int units – to store the number of units consumed
double bill – to store the amount to be paid
Member methods :
void accept ( ) – to accept the name of the customer and number of units consumed
void calculate ( ) – to calculate the bill as per the following tariff :

ICSE Computer Applications Question Paper 2017 Solved for Class 10 - 1
A surcharge of 2.5% charged if the number of units consumed is above 300 units.
ICSE Computer Applications Question Paper 2017 Solved for Class 10 - 2
Write a main method to create an object of the class and call the above member methods.

Python Code


class ElectricityBill:
    def accept(self):
        self.customer_Name = str(input("Enter the name of the customer:"))
        self.units_Consumed = int(input("Enter the units consumed:"))
    def calculate(self):
        if self.units_Consumed <= 100:
            self.bill_Amount = self.units_Consumed *2.0
        elif self.units_Consumed > 100 and self.units_Consumed <= 300:
            self.bill_Amount  = self.units_Consumed *3.0
        else:
            self.bill_Amount  = self.units_Consumed *5.0
self.bill_Amount  += self.bill_Amount*(2.5/100.0)
    def printBill(self):
        print("Name of the customer: ",self.customer_Name)
        print("Total units consumed: ",self.units_Consumed)
        print("Bill Amount: ",self.bill_Amount)
    def __init__(self) -> None:
        self.customer_Name = ""
        self.units_Consumed = 0
        self.bill_Amount =0.0
obj = ElectricityBill()
obj.accept()
obj.calculate()
obj.printBill()


JAVA Code 


import java.util.Scanner;

public class ICSEJava {
    public static void main(String[] args) {
        ElectricityBill obj = new ElectricityBill();
        obj.accept();
        obj.calculate();
        obj.print();

    }
}

class ElectricityBill {
    public void print() {
        System.out.println("Name of the customer: " + this.customer_Name);
        System.out.println("Total units consumed: " + this.units_Consumed);
        System.out.println("Bill Amount: " + this.bill_Amount);
    }

    public void calculate() {
        if (this.units_Consumed <= 100)
            this.bill_Amount = this.units_Consumed * 2.0;
        else if (this.units_Consumed > 100 && this.units_Consumed <= 300)
            this.bill_Amount = this.units_Consumed * 3.0;
        else {
            this.bill_Amount = this.units_Consumed * 5.0;
            this.bill_Amount += (this.bill_Amount)*(2.5/100);
        }
    }

    public void accept() {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the name of the customer.");
        this.customer_Name = in.nextLine();
        System.out.println("Enter the unit consumed.");
        this.units_Consumed = in.nextInt();
        in.close();
    }

    ElectricityBill() {
        this.bill_Amount = 0.0;
        this.units_Consumed = 0;
        this.customer_Name = "";
    }

    private int units_Consumed;
    private String customer_Name;
    private double bill_Amount;
}

Sunday, January 8, 2023

Leet Code Roman to Integer

Roman numerals are represented by seven different symbols: IVXLCD and M.

Symbol       Value
I                   1
V                  5
X                 10
L                  50
C                 100
D                 500
M               1000

  For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII,       which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

 Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is   not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making   four. The same principle applies to the number nine, which is written as IX. There are six instances where   subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9. 
  • X can be placed before L (50) and C (100) to make 40 and 90. 
  • C can be placed before D (500) and M (1000) to make 400 and 900.

 Given a roman numeral, convert it to an integer.

 

 Example 1:

Input: s = "III"
Output: 3
Explanation: III = 3.

 Example 2:

Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

 Example 3:

Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

 

 Constraints:

  • 1 <= s.length <= 15
  • s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
  • It is guaranteed that s is a valid roman numeral in the range [1, 3999].
 Explanation:
The logic of the program is explained below with the example of MCMXCIV.
Take the value of each of the letters in the sequence:
1000 = M, 100 = C, 1000 = M, 10 = X, 100 = C, 1 = I, 5 = V
Keed adding the values in a variable. But for the second position onwards we
have check the difference of the value of the current position and the previous
position. If the difference is 4 or 9 or 40 or 90 or 400 or 900 then by the exception
rule we have to adjust the value.
For the example above, as 100-1000 = -900 ( i = 1 and i = 0) which is not under
exception rule we add 1000+100 = 1100
But for the next letter ('M') the value is 1000 ( i = 2 ) and 1000 - 100 = 900
( under exception rule), so instead of adding 1000 to 1100 we have to have add 900
to the value of the first letter ( since CM is forming a single value). For this
we need to subtract the value of the current letter (M = 1000) and the previous
letter ( C = 100 ) from the sum of 1000+100+1000 = 2100 and add 900.
Which gives 2100-1000 - 100 + 900 = 1900.


PYTHON CODE

class Solution(object):

    def romanToInt(self, s):
        temp = 0
        for i in range(len(s)):
            self.output += self.value[self.position(s[i])]
            #print ("BEFORE ADJUSTMENT",self.output)
            if i > 0:
                temp = (self.value[self.position(s[i])] -
                        self.value[self.position(s[i - 1])])
            if temp == 4 or temp == 9 or temp == 40 or temp == 90 or temp == 400 or temp == 900:
                self.output = self.output - self.value[self.position(
                    s[i - 1])] - self.value[self.position(s[i])] + temp
            #print ("AFTER ADJUSTMENT",self.output)
        return self.output

    def position(self, symb):
        counter = 0
        while symb != self.symbol[counter]:
            counter += 1
        return (counter)

    symbol = ['I', 'V', 'X', 'L', 'C', 'D', 'M']
    value = [1, 5, 10, 50, 100, 500, 1000]
    output = 0


obj = Solution()
s = str(input("Enter a valid roman numeral in range(1,3999):"))
print(obj.romanToInt(s))