The Programming Project

Tuesday, December 29, 2020

ICSE JAVA PROGRAMS 2019: SECTION B QUESTION 5

 

ICSE Computer Applications Question Paper 2019 Solved for Class 10

 

Question 5.

Using the switch-case statement, write a menu driven program to do the following: [15]

(a)             To generate and print Letters from A to Z and their Unicode

Letters                               Unicode

  A                                          65

  B                                          66

   .                                           .

   .                                           .              

  Z                                          90

 

 

 

(b)             Display the following pattern using iteration (looping):

 

1

1         2

1    2        3

1    2        3        4

1    2        3        4       5

 

JAVA CODE

 

import java.util.*;

public class ICSEB5 {

       public static void main(String[] args) {

          Scanner input = new Scanner(System.in);

          int choice;

          System.out.println("Press 1 for Unicode and 2 for Pattern:");

          System.out.println("Please enter your choice:");

          choice = input.nextInt();

          switch(choice) {

          case 1:

                 unicodeLetters obj = new unicodeLetters();

                 obj.generateUnicode();

                 break;

          case 2:

                 numberPattern ob = new numberPattern();

                 ob.generatePattern();

                 break;

          default:

                 System.out.println("INVALID CHOICE:");

                 break;

          }

          input.close();

     }

}

class unicodeLetters {

       public void generateUnicode() {

              System.out.println("Letters                     Unicode");

              for(int i=65;i<=90;i++)

                     System.out.println((char)i+"                           "+i);

       }

}

class numberPattern {

       public void generatePattern() {

              for(int i=1;i<=5;i++) {

                     for(int j=1;j<=i;j++)

                           System.out.print(j+"   ");

                     System.out.println();

              }

       }

}

 




PYTHON CODE



class unicodeLetters:
    def generateUnicode(self):
        print("Letters          Unicode")
        for i in range(26):
            print((chr)(i+65),"        ",i+65)
class numberPattern:
    def generatePattern(self):
        for i in range(5):
            k=i+1
            for j in range(k):
                print(j+1end=" ")
            print()
print("Press 1 for Unicode and 2 for Pattern:")
choice = int(input("Please enter your choice:"))
if choice == 1:
    obj = unicodeLetters()
    obj.generateUnicode()
elif choice == 2:
    ob = numberPattern()
    ob.generatePattern()
else:
    print("INVALID INPUT")

Monday, December 28, 2020

ICSE JAVA PROGRAMS 2019: SECTION B QUESTION 4

Design a class name ShowRoom with the following description :

Instance variables/ Data members :

String name – To store the name of the customer

long mobno – To store the mobile number of the customer

double cost – To store the cost of the items purchased

double dis – To store the discount amount

double amount – To store the amount to be paid after discount

Member methods: –

ShowRoom() – default constructor to initialize data members

void input() – To input customer name, mobile number, cost

void calculate() – To calculate discount on the cost of purchased items, based on following criteria

 

Cost                                                                     Discount (in percentage)

Less than or equal to ₹ 10000                                             5%

More than ₹ 10000 and less than or equal to ₹ 20000      10%

More than ₹ 20000 and less than or equal to ₹ 35000     15%

More than ₹ 35000                                                                20%

 

void display() – To display customer name, mobile number, amount to be paid after discount

Write a main method to create an object of the class and call the above member methods.

The calculation of the discount can easily be achieved by

Java if...else...if Statement

In Java, we have an  if...else...if ladder, which can be used to execute one block of code among multiple other blocks.

 

JAVA CODE

import java.util.*;

public class ICSE2019SECTIONBQ4 {

       public static void main(String[] args) {

          showRoom obj = new showRoom();

          obj.input();

          obj.calculate();

          obj.display();      

     }

}

class showRoom {

       public void display() {

              System.out.println("Customer's Name: "+name+" Mobile Number: "+mobileNumber);

              System.out.println("Amount to be paid after discount: "+amount);

       }

       public void calculate() {

              if (cost <= 10000) {

                     discount = (5*cost)/100;

                     amount = cost - discount;

              }

              else if (10000 < cost && cost <= 20000) {

                     discount = (10*cost)/100;

                     amount = cost - discount;

              }

              else if (20000 < cost && cost <= 35000) {

                     discount = (15*cost)/100;

                     amount = cost - discount;

              }

              else {

                     discount = (20*cost)/100;

                     amount = cost - discount;

              }

       }

       public void input() {

              Scanner input = new Scanner(System.in);

              System.out.println("Enter customer's name:");

              name = input.nextLine();

              System.out.println("Enter customer's mobile number:");

              mobileNumber = input.nextLong();

              System.out.println("Enter customer's bill (cost):");

              cost = input.nextDouble();

              input.close();

       }

       showRoom() {

              name ="";

              mobileNumber = 0;

              cost = 0;

              discount = 0;

              amount =0;

       }

       private String name;

       private long mobileNumber;

       private double cost;

       private double discount;

       private double amount;

       }

 

PYTHON CODE

 class showRoom:

    def display(self):
        print("Customer's Name: ",self.__name," Mobile Number: ",self.__mobileNumber)
        print("Amount to be paid after discount: ",self.__amount)
    def calculate(self):
        if self.__cost <= 10000:
            self.__discount = (5*self.__cost)/100
            self.__amount = self.__cost - self.__discount
        elif 10000 < self.__cost and self.__cost <= 20000:
            self.__discount = (10*self.__cost)/100
            self.__amount = self.__cost - self.__discount
        elif 20000 < self.__cost and self.__cost <= 35000:
            self.__discount = (15*self.__cost)/100
            self.__amount = self.__cost - self.__discount    
        else:
            self.__discount = (20*self.__cost)/100
            self.__amount = self.__cost - self.__discount
    def input(self):
        self.__name = str(input("Enter customer's name:"))
        self.__mobileNumber = int(input("Enter customer's mobile number:"))
        self.__cost = float(input("Enter customer's bill (cost):"))
    def __init__(self):
        self.__name=""
        self.__mobileNumber = 0
        self.__cost = 0.0
        self.__discount = 0.0
        self.__amount = 0.0
    __name =""
    __mobileNumber = 0
    __cost = 0.0
    __discount = 0.0
    __amount = 0.0
obj = showRoom()
obj.input()
obj.calculate()
obj.display()

Sunday, December 6, 2020

PYTHON PROGRAM FOR BEGINNERS : SCHOOL LEVEL

 Calculating How Old Your Dog is in Human Years

This assignment is designed to give you practice writing code and applying lessons and topics for the current module.


This homework deals with the following topics:


Getting user input

Error checking

Variables & data types

Conditionals

The Assignment


In this assignment, you will write a program in Python that calculates a dog’s age in human years.


The program will prompt the user for an age in dog years and calculate that age in human years.  Allow for int or float values, but check the user’s input to make sure it's valid -- it should be numeric and positive.  Otherwise, let the user know their input is not valid.


You can use the following rules to approximately convert a medium-sized dog’s age to human years:


For the first year, one dog year is equal to 15 human years

For the first 2 years, each dog year is equal to 12 human years

For the first 3 years, each dog year is equal to 9.3 human years

For the first 4 years, each dog year is equal to 8 human years

For the first 5 years, each dog year is equal to 7.2 human years

After that, each dog year is equal to 7 human years.  (Note: This means the first 5 dog years are equal to 36 human years (5 * 7.2) and the remaining dog years are equal to 7 human years each.)

Print the result in the following format, substituting for <dog_age> and <human_age>: "The given dog age <dog_age> is <human_age> in human years."  Round the result to 2 decimal places.  


For example:


If the user enters 2, the program will print: “The given dog age 2 is 24.0 in human years.”

If the user enters 3, the program will print: “The given dog age 3 is 27.9 in human years.”

If the user enters 4.5, the program will print: “The given dog age 4.5 is 32.4 in human years.”

If the user enters 12.1, the program will print: “The given dog age 12.1 is 85.7 in human years.”

Considering invalid inputs:


Your program must ask the user for an age in dog years - hint: use the input() function

We are going to test invalid inputs - make sure that your code can handle negative value inputs and non-numerical inputs!

For invalid inputs, make sure that your printed response adheres to the following:

     - If a text-based input is provided, make sure your response contains the word 'invalid'.  For example, if the user doesn’tinput a number, print “<age> is invalid.”, substituting for <age>.


     - If a negative input is provided, make sure your response contains the word 'negative'.  For example, if the user inputs a negative number, print “Age cannot be a negative number.”

  

  Python CODE:

 dog_age=(input("Enter the dog's age:"))

flag=False
try:
    dog_age = float(dog_age)
    dog_age_in_Human_years=0
    if dog_age <0:
        flag = True
    elif 0 < dog_age <=1:
        dog_age_in_Human_years += dog_age*15.0
    elif 1dog_age <=2:
        dog_age_in_Human_years += dog_age*12.0
    elif 2dog_age <=3:
        dog_age_in_Human_years += dog_age*9.3
    elif 3dog_age <=4:
        dog_age_in_Human_years += dog_age*8.0
    elif 4dog_age <=5:
        dog_age_in_Human_years += dog_age*7.2
    else:
        dog_age_in_Human_years += 5*7.2+(dog_age-5)*7.0 
except ValueError as e:
    print("Your input is invalid")
if flag == False:
    print("The given dog age"dog_age"is"round(dog_age_in_Human_years,2), "in human years.")
else:
    print("Age cannot be a negative number")