Page 295 - CA_Blue( J )_Class10
P. 295

Ans. import java.util.*;
                       class BookFair {
                           String bName;
                           double price;
                           void input() {
                               Scanner sc=new Scanner(System.in);
                               System.out.println("Enter the name of the book:");
                               bName=sc.nextLine();
                               System.out.println("Enter the price of the book:");
                               price=sc.nextDouble();
                           }
                           void calculate()
                           {
                               double dis=0;
                               if(price<=1000)
                                   dis=2/100.0*price;
                               else if(price<=3000)
                                   dis=10/100.0*price;
                               else
                                   dis=15/100.0*price;
                               price=price-dis;
                           }
                           void display() {
                                System.out.println("Name:"+bName);
                                System.out.println("Price:"+price);
                           }
                           public static void main(String args[])
                           {
                               BookFair ob=new BookFair();
                               ob.input();
                               ob.calculate();
                               ob.display();
                           }
                       }
                    16.  Define a class called ParkingLot with the following description:                           [2015]
                       Instance variables/data members
                       int vno       :  to store the vehicle number.
                       int hours     :   to store the number of hours the vehicle is parked in the parking lot.
                       double bill   :  to store the bill amount.
                       Methods
                       void input()   :   to input and store the vehicle number and hours.
                       void calculate()  :   to compute the parking charge at the rate of Rs. 3 for the first hour or part thereof, and Rs. 1.50 for each
                                      additional hour or part thereof.
                       void display()   :  to display the detail.
                       Write a main() method to create an object of the class and call the above methods.
                   Ans. import java.util.*;
                      class ParkingLot
                      {
                          int vno;
                          int hours;
                          double bill;
                          void input()
                          {
                              Scanner sc=new Scanner(System.in);
                              System.out.print("Vehicle Number: ");

                                                                                                                       293
                                                                                      Class as the Basis of all Computation  293
   290   291   292   293   294   295   296   297   298   299   300