The Programming Project

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))

ICSE Java Programming Spy Number 2017 Q5 Solved

ICSE Java Programming Spy Number  2017 Q5  Solved

Write a program to accept a number and check and display whether it is a spy number or not. (A number is spy if the sum of its digits equals the product of its digits.)
Example : consider the number 1124.
Sum of the digits = 1 + 1+ 2 + 4 = 8
Product of the digits = 1×1 x 2 x 4 = 8
Thus 1124 is a Spy Number

Java Code

import java.util.Scanner;

public class ICSEJava {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int sum_of_digits = 0;
        int product_of_digits = 1;
        int number, digit, temp;
        System.out.println("Enter a positive integer:");
        number = in.nextInt();
        temp = number;
        while (temp > 0) {
            digit = temp % 10;
            temp = (int) (temp / 10);
            sum_of_digits += digit;
            product_of_digits *= digit;
        }
        if (sum_of_digits == product_of_digits)
            System.out.println(number + " is a spy number.");
        else
            System.out.println(number + " is not a spy number.");
    }
}

Python Code

sum_of_digits = 0
product_of_digits = 1
number = int(input("Enter a postive integer:"))
temp = number
while temp > 0:
    digit = temp%10
    temp = int(temp/10)
    sum_of_digits += digit
    product_of_digits *= digit
if sum_of_digits == product_of_digits:
    print(number," is a spy number.")
else:
    print(number," is not a spy number.")

ICSE Java Programming Strings 2022 Q7 SPECIMEN PAPER Solved

ICSE Java Programming Strings  2022 Q7 SPECIMEN PAPER Solved

Question 7
Define a class to accept and store 10 strings into the array and print the strings with even number of characters.


import java.util.Scanner;

public class ICSEJava {
    public static void main(String[] args) {
        EvenCharacters obj = new EvenCharacters();
        obj.inputString();
        obj.charCounter();
    }
}

class EvenCharacters {
    public void charCounter() {
        System.out.println("Strings with even number of characters:");
        for (int i = 0; i < 10; i++) {
            this.char_count = 0;
            for(int j =0; j<this.message[i].length();j++)    {
                if(this.message[i].charAt(j) == ' ')
                    continue;
                else
                    this.char_count++;
                }
            if (this.char_count%2 == 0)
                System.out.println(this.message[i]);
            }      
    }

    public void inputString() {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the strings one by one:");
        for (int i = 0; i < 10; i++) {
            System.out.println("Enter a string:");
            this.message[i] = in.nextLine();
        }
        in.close();
    }

    EvenCharacters() {
        this.message = new String[10];
        this.char_count = 0;
    }

    private int char_count;
    private String[] message;
}

ICSE Java Programming palindrome 2022 Q6 SPECIMEN PAPER Solved

ICSE Java Programming Palindrome strings  2022 Q6 SPECIMEN PAPER Solved

Define a class to accept a string, convert it into lowercase and check whether the string is a palindrome or not. A palindrome is a word which reads the same backward as forward.

Example: madam, racecar


import java.util.Scanner;

public class ICSEJava {
    public static void main(String[] args) {
        Palindrome obj = new Palindrome();
        obj.inputString();
        obj.checkPalindrome();
    }
}

class Palindrome {
    public void checkPalindrome() {
        for (int i = 0; i < this.message.length(); i++) {
            if (this.message.charAt(i) != this.message.charAt(this.message.length() - 1 - i)) {
                flag = false;
                break;
            } else
                continue;
        }
        if (flag)
            System.out.println("Entered word is a palindrome:" + this.message);
        else
            System.out.println("Entered word is not a palindrome.");
    }

    public void inputString() {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the word:");
        this.message = in.nextLine();
        this.message = this.toLowerCase(message);
        in.close();
    }

    private String toLowerCase(String msg) {
        String temp = "";
        for (int i = 0; i < msg.length(); i++) {
            if (msg.charAt(i) >= 'A' && msg.charAt(i) <= 'Z') {
                temp += (char) (msg.charAt(i) + 32);
            } else
                temp += (msg.charAt(i));
        }
        return temp;
    }

    Palindrome() {
        this.message = "";
        this.flag = true;
    }

    private boolean flag;
    private String message;
}