Page 1 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , Chapter 3, , Class as the Basis of All, Computation, Mental Drill, A., , Tick () the correct option., 1., , Which among the following is not a primitive data type?, a. int, b. short, c. String, d. Long, , Ans., , c. String, , 2., , Name the operator that is used to allocate memory space for an object., a. Dot, b. New, c. Both a and b, d. None of these, Ans. b. New, , 3., , What is the name given to a memory location called in Java?, a. Variable, , c. Data Type, Ans. a. Variable, Which are the data types used in Java?, a. Primitive data type, c. Both a and b, Ans. c. Both a and b, , b. Constant, d. None of these, , 4., , b. Composite data type, d. None of these, , 5., , How are the characteristics of an object represented in a class?, a. Data Members, b. Member Functions, c. Access specifiers, d. None of these, Ans. a. Data Members, , 6., , Which among the following is used to change the state of an object?, a. Data Members, , c. Both a and b, Ans. a. Data Members, 7., , b. Name of the class, d. None of these, , How is encapsulation implemented in a program?, a. Using a class, b. Using only Functions, c. Using only Variables, , Ans., , d. None of these, , a. Using a class, , Computer Applications – X (ICSE Course) Answers, , 92
Page 2 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , 8., , Information passed to a method through arguments is called, a. Message, , b. Variables, , c. Numbers, , d. Data, , Ans., 9., , ., , a. Message, , What among the following is not a component of a message?, a. An object identifier, , b. A method name, , c. Arguments, , d. Data Members, , Ans., , d. Data Members, , 10. How many objects can you create from a class?, a. One, , b. Two, , c. Three, , d. Any number, , Ans., , d. Any number, , SECTION A, A., , Answer the following questions., 1., , Mention any two attributes required for class declaration., , Ans. i) The keyword ‘class’, ii) The class-name, 2., , What is a class in Java?, , Ans. A class is a mechanism to implement encapsulation. Thus class encloses characteristics, represented by data members or variables and behaviour represented by member methods or, functions into a single unit. It acts as a blueprint or template for an object., 3., , Why is a class called an object factory?, , Ans. A class is called an object factory because it acts as a factory for a design or abstraction which, produces objects. Each object that is generated from the class will have the same design or, abstraction as represented by its data members and member functions., 4., , Define Instance Variable. Give an example of the same., , Ans. The data members declared within a class are called instance variables. For example:, class Myclass, {, int a,b;, void getval(int x, int y), {, a=x;, 93, , Class as the Basis of All Computation
Page 3 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , b=y;, }, }, Here the variables ‘a’ and ‘b’ are instance variables of the class ‘Myclass’., 5., , What does the following mean?, Employee stuff = new Employee( );, , Ans. An object named ‘stuff’ is being created or memory is being allocated of ‘Employee’ type., 6., , How do you declare objects? Show with the help of an example., , Ans. An object is declared using the following general example:, <class-name> <object name>=new <class-constructor>;, For example,, Example ob=new Example();, Here an object named ‘ob’ is being allocated in the memory of ‘Example’ type, which is being, initialised with the constructor ‘Example()’., 7., , Write Java statement to create an object mp4 of class digital., , Ans. digital mp4=new digital();, 8., , What is the difference between an object and a class?, , Ans. Difference between class and object:, Class, , Object, , A class is a template from which objects An object is an instance of a class., are created., , 9., , A class is a group of similar objects., , Object is a real world entity such as pen,, laptop, mobile, chair etc., , A class is a logical entity., , An object is a physical entity., , What is a variable? How do you initialize a variable?, , Ans. Variable is a name given to a memory location in Java., Initialisation of variables are done during declaration. For example,, int a=5,b=6;, 10., , What are data types? State its categories., , Ans. Java uses data types to identify data and dictates the method of handling it. It is categorised as, Fundamental data type and Composite data type., 11., , Why is a class known as a composite data type?, , Ans. A composite data type is one which is composed with various primitive data type. A class defined, with various primitive data types such as int, double etc. so it is known as a composite data type., Computer Applications – X (ICSE Course) Answers, , 94
Page 4 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , 12., , Write one difference between primitive data types and composite data types., , Ans., Fundamental Data Type, , Composite Data Type, , These are inbuilt data type provided by the These are data types created by the user using, Java Language., fundamental or composite data type or both., The size of it is fixed., 13., , The size of it depends upon the size of the, individual components of it., , What does a class encapsulate?, , Ans. A class encapsulate characteristics represented by data members and behaviour represented by, member functions., 14., , Why is a class considered to be an abstraction for a group of objects?, , Ans. A class is an abstraction because it describes what is created, whereas an object is created itself., A class acting as an abstraction or blueprint, multiple objects can be created from it., 15., , What do you understand by data abstraction? Explain with an example., , Ans. Data Abstraction is the property by virtue of which only the essential details are displayed to the, user., For example, when we ride a bike, we concentrate only driving rather than how the engine is, working according to the different switches, lever and steering., 16., , Why is an object called an instance of a class?, , Ans. An object is called an instance of class as every object created from a class get its own instances, of the variables defined in the class., B., , Consider the following code and answer the questions that follow:, class academic, {, int x,y;, void access(), {, int a,b;, academic student=new academic();, System.out.println(“Object Created”);, }, }, a. What is the object name of the class?, b. Name the instance variables used in the class., c. Write the name of local variables used in the program., d. Give the type of function being used and its name., , 95, , Class as the Basis of All Computation
Page 5 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , Ans., a. student, b. x and y, c. a and b, d. Procedural function or pure function., Name: access(), C., , Consider the following code and answer the questions that follow:, class vxl, {, int x,y;, void init( ), {, x=5;, y=10;, }, protected void access( ), {, int a=50, b=100;, vxl vin=new vxl( );, vin.int( );, System.out.println(“Object created”);, System.out.println(“I am X=”+vin.x);, System.out.println(“I am Y=”+vin.y);, }, }, a. What is the object name of the class vxl?, b. Name the local variables of class., c. What is the access specifier of method access( )?, d. Write the output of the above program., Ans., a., , vin, , b. a and b, c., , protected, , d. Output:, Object created, I am X=5, I am Y=10, , Computer Applications – X (ICSE Course) Answers, , 96
Page 7 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , SECTION B, Write programs for the following:, 1., , Write a class with name Employee and basic as its data member, to find the gross pay of an, employee for the following allowances and deduction. Use meaningful variables., Dearness Allowance = 25% of the Basic Pay, House Rent Allowance = 15% of Basic Pay, Provident Fund = 8.33% of Basic Pay, Net Pay = Basic Pay + Dearness Allowance + House Rent Allowance, Gross Pay = Net Pay − Provident Fund, , Ans., class Employee, {, double basic;, Employee(double b), {, basic=b;, }, void calc(), {, double pf,gp,np,hra,da;, da=25/100.0*basic;, hra=15/100.0*basic;, pf=8.33/100*basic;, np=basic+da+hra;, gp=np-pf;, System.out.println(“Gross Pay=”+gp);, }, }, 2., , Define a class ‘Salary’ described as below:, Data Members:, Name, Address, Phone, Subject Specialisation, Monthly Salary, Income Tax., Member methods:, i. To accept the details of a teacher including the monthly salary., ii. To display the details of the teacher., iii. To compute the annual Income Tax as 5% of the annual salary above ` 1,75,000/-., Write a main method to create object of the class and call the above member method., , Computer Applications – X (ICSE Course) Answers, , 98
Page 10 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , {, Student ob=new Student();, ob.input();, ob.compute();, ob.display();, }, }, 4., , Define a class Employee having the following description:, Instance variables:, int pan, to store personal account number, String name, to store name, double tax_income, to store annual taxable income, double tax, to store tax that is calculated, Member functions:, input ( ), calc( ), display ( ), , Store the pan number, name, taxable income, Calculate tax for an employee, Output details of an employee, , Write a program to compute the tax according to the given conditions and display the output, as per the given format., Total Annual Taxable Income, Tax Rate, Upto ` 1,00,000, No tax, From 1,00,001 to 1,50,000, 10% of the income exceeding ` 1,00,000, From 1,50,001 to 2,50,000, ` 5000 + 20% of the income exceeding ` 1,50,000, Above ` 2,50,000, ` 25,000 + 30% of the income exceeding ` 2,50,000, Output:, Pan Number, , Name, , Tax-income, , Tax, , Ans., import java.util.*;, class Employee, {, String pan, name;, double tax_income,tax;, void input(), {, Scanner sc=new Scanner(System.in);, System.out.println(“Enter your PAN no.:”);, pan=sc.nextLine();, System.out.println(“Enter your name:”);, name=sc.nextLine();, System.out.println(“Enter taxable income:”);, 101, , Class as the Basis of All Computation
Page 11 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , tax_income=sc.nextDouble();, }, void display(), {, System.out.println(“Pan Number\t\tName\t\tTax-income\t\tTax”);, System.out.println(pan+“\t\t”+name+“\t\t”+tax_income+“\t\t”++ tax);, }, void calc(), {, if(tax_income<=100000), tax=0;, else if(tax_income>100000 && tax_income<=150000), tax=10/100.0*(tax_income-100000);, else if(tax_income>150001 && tax_income<=250000), tax=5000+20/100.0*(tax_income-150000);, else, tax=25000+30/100.0*(tax_income-250000);, }, }, 5., , Define a class called Mobike with the following description:, Instance variables/ Data members:, bno, :, to store the bike’s number, phno, :, to store the phone number of the customer, name, :, to store the name of the customer, days, :, to store the number of days the bike is taken on rent, charge, :, to calculate and store the rental charge, Member methods:, void input (), : to input and store the detail of the customer, void compute () : to compute the rental charge. The rent for a Mobike is charged on the, following basis, First five days, :, ` 500 per day, Next five days, :, ` 400 per day, Rest of the days : ` 200 per day, void display () : to display the details in the following format:, Bike No., -------, , Phone No., -------, , Name, ------, , No. of days, --------, , Charge, -------, , Ans., import java.util.*;, class Mobike, {, String bno,phno,name;, int days;, double charge;, Computer Applications – X (ICSE Course) Answers, , 102
Page 12 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , void input(), {, Scanner sc=new Scanner(System.in);, System.out.println(“Enter your bike no.:”);, bno=sc.nextLine();, System.out.println(“Enter your phone no.:”);, phno=sc.nextLine();, System.out.println(“Enter your name:”);, name=sc.nextLine();, System.out.println(“Enter no. of days taken for rent:”);, days=sc.nextInt();, }, void display(), {, System.out.println(“Bike No.\t\tPhone No.\t\tName\t\tNo. of days\t\tCharge”);, System.out.println(bno+“\t\t”+phno+“\t\t”+name+“\t\t”+days+“\t\t”+charge);, }, void calc(), {, if(days<=5), charge=days*500;, else if(days>5 && days<=10), charge=5*500+(days-5)*400;, else, charge=5*500+5*400+(days-10)*200;, }, }, 6., , Write a program with the following specifications:, Class name, :, Student, Data members, :, name, :, To store the name of a student, hindi, :, To store the marks in hindi subject, english, :, To store the marks in english subject, maths, :, To store the marks in mathematics, computer, :, To store the marks in computer, average, :, To store the avergae of the marks obtained, grade, :, To store the grade depending upon the average., Member methods:, void accept( ), :, void calcavg( ), :, , to accept name and marks in the 4 subjects., to calculate and store the grade according to the following slabs:, , Average marks, 90 and above, Between 75 to 89 (both inclusive), 103, , Grade Obtained, A++, A, Class as the Basis of All Computation
Page 14 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , 7., , Design class called Bank with the following descriptions:, Data members:, name, :, to store the name of the depositor, acno, :, to store the account number, type, :, to store type of the account, bal, :, to store the balance amount in the account, Member functions:, initialise( ), : to assign the data members with any value., depo(int a ), : where a is the amount to be deposited and the variable bal is to be, updated., withdraw( int a) : where a is the amount to be withdrawn after checking the balance, (Minimum balance should be ` 1000) and the variable bal is to be, updated., print( ), : to print all the details., Write the main method to create the object of the class and call the above method., , Ans., import java.util.*;, class Bank, {, String name;, long acno;, float bal;, String type;, void initialise(), {, name=“Saurav Agarwal”;, acno=1001098721;, bal=10000;, type=“Savings”;, }, void depo(int a), {, bal+=a;, }, void withdraw(int a), {, if(bal-a<1000), System.out.println(“Minimum balance should be 1000 rupees”);, else, bal-=a;, }, , 105, , Class as the Basis of All Computation
Page 15 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , void print(), {, System.out.println(“Name:”+name);, System.out.println(“Account No.:”+acno);, System.out.println(“Balance:”+bal);, System.out.println(“Type of Account:”+type);, }, public static void main(String args[]), {, Bank ob=new Bank();, Scanner sc=new Scanner(System.in);, ob.initialise();, char c;int a;, System.out.println(“Enter (D)eposit/(W)ithdraw:”);, c=sc.next().charAt(0);, if(c==‘D’), {, System.out.println(“Enter the amount to deposit:”);, a=sc.nextInt();, ob.depo(a);, }, else if(c==‘W’), {, System.out.println(“Enter the amount to withdraw:”);, a=sc.nextInt();, ob.withdraw(a);, }, else, System.out.println(“Invalid input”);, ob.print();, }, }, 8., , Define a class Bill as described below:, Data members are:, name, :, to store the name of the consumer, consumerno, :, to store the consumer number, unitconsumed, :, to store the unit cosumed, Member methods are :, datainput(), :, to read the data of a person, compute(), :, to calculate the bill amount as per criteria., Units Consumed, Up to 100 units, More than 100 and up to 200 units, , Computer Applications – X (ICSE Course) Answers, , Rate, 1.20, 2.20, 106
Page 17 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , 9., , Write a program with the following specifications:, Class, :, Empl, Data Members:, Emp_No, :, To store the employee number, Name, :, To store the name of the employee, Basic, :, To store the basic salary of an employee, DA, :, To store the dearness allowance of an employee., HRA, :, To store the House Rent Allowance of an employee, TA, :, To store the Travelling Allowance of an employee, PF, :, To store the Provident Fund of an employee, Gross, :, To store the Gross Salary, Member Methods:, get ( ), :, calcu ( ), :, Basic Salary, , To accept Employee No., Name and Basic Salary of the employees, To calculate the Gross Salary based on the following condition:, DA(%), , TA(%), , HRA(%), , PF(%), , >=20,000, , 53, , 12, , 10, , 8, , >=10,000 to <20,000, , 45, , 10, , 12, , 7.5, , < 10,000, , 40, , 8, , 14, , 7, , Gross Salary = ( Basic Salary + DA + TA + HRA ) – PF, display ( ) : To display the following data in given format:, EMPLOYEE No., NAME, GROSS SALARY, , PF, , Write a main method to create the object of the above class and call the above method to, calculate and print the Employee No., Name, Gross Salary and PF of an employee., Ans., import java.util.*;, class Empl, {, int Emp_No;, String Name;, float Basic,DA,HRA,TA,PF,Gross;, void get(), {, Scanner sc=new Scanner(System.in);, System.out.println(“Enter your employee no.:”);, Emp_No=sc.nextInt();, System.out.println(“Enter your name:”);, Name=sc.nextLine();, System.out.println(“Enter the basic pay:”);, Basic=sc.nextFloat();, }, Computer Applications – X (ICSE Course) Answers, , 108
Page 18 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , void calcu(), {, if(Basic>=20000), {, DA=53/100f*Basic;, TA=12/100f*Basic;, HRA=10/100f*Basic;, PF=8/100f*Basic;, }, else if(Basic>=10000 && Basic<20000), {, DA=45/100f*Basic;, TA=10/100f*Basic;, HRA=12/100f*Basic;, PF=7.5f/100f*Basic;, }, else, {, DA=40/100f*Basic;, TA=8/100f*Basic;, HRA=14/100f*Basic;, PF=7/100f*Basic;, }, Gross=Basic+DA+TA+HRA+PF-PF;, }, void display(), {, System.out.println(“EMPLOYEE No.\t\tNAME\t\tGROSS SALARY\t\tPF”);, System.out.println(Emp_No+”\t\t”+Name+”\t\t”+Gross+”\t\t”+PF);, }, public static void main(String args[]), {, Empl ob=new Empl();, ob.get();, ob.calcu();, ob.display();, }, }, 10., , 109, , Define a class called Library with the following description:, Instance variables/data members:, int acc_num, :, stores the accession number of books, String title, :, stores the title of book, String author, :, stores the name of author, , Class as the Basis of All Computation
Page 19 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , Member methods:, void input(), :, void compute(), :, void display(), , :, , to input and store the accession number, title and author, to accept the number of days late, calculate and display the fine, charged the rate of ` 2 per day, to display the details in the following format:, , Accession number, Title, Author, ………………………….., ……………………, …………………………., Write the main method to create an object of the class and call the above member methods., Ans., import java.util.*;, class Library, {, int acc_num;, String title,author;, void input(), {, Scanner sc=new Scanner(System.in);, System.out.println(“Enter the accession no.:”);, acc_num=sc.nextInt();, System.out.println(“Enter the title:”);, title=sc.nextLine();, System.out.println(“Enter the author:”);, author=sc.nextLine();, }, void compute(), {, Scanner sc=new Scanner(System.in);, int late,fine;, System.out.println(“Enter the no. of days late:”);, late=sc.nextInt();, fine=late*2;, System.out.println(“Fine:”+fine);, }, void display(), {, System.out.println(“Accession number\t\tTitle\t\tAuthor”);, System.out.println(acc_num+“\t\t”+title+“\t\t”+author);, }, public static void main(String args[]), {, Library ob=new Library();, Computer Applications – X (ICSE Course) Answers, , 110
Page 21 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , }, void display(), {, System.out.println(“Product code:”+product_code);, System.out.println(“Flavour:”+flavour);, System.out.println(“Pack type:”+pack_type);, System.out.println(“Pack size:”+pack_size);, System.out.println(“Product price:”+product_price);, }, public static void main(String args[]), {, FruitJuice ob=new FruitJuice();, ob.input();, ob.discount();, ob.display();, }, }, 12., , Define a class Book with the following specifications., Instance variables/data members:, BOOK_NO, :, int type to store the book number, BOOK_TITLE, :, String type to store the title of the book, PRICE, :, float type to store the price per copy, Member Methods:, TOTAL_COST(), :, INPUT(), PURCHASE(), , :, :, , to calculate the total cost for N number of copies, where N is passed, to the function as argument, to read BOO_NO, BOOK_TITLE, PRICE, to ask the user to input the number of copies to be purchased. It, , invokes TOTAL_COST() and prints the total cost to be paid by the user. Write the main method, to create an object of the class and call the above member methods., Ans., import java.util.*;, class Book, {, int BOOK_NO;, String BOOK_TITLE;, float PRICE;, void INPUT(), {, Scanner sc=new Scanner(System.in);, System.out.println(“Enter the book no.:”);, BOOK_NO=sc.nextInt();, System.out.println(“Enter the book title:”);, Computer Applications – X (ICSE Course) Answers, , 112
Page 22 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , BOOK_TITLE=sc.nextLine();, System.out.println(“Enter the price:”);, PRICE=sc.nextFloat();, }, void TOTAL_COST(int n), {, float tcost;, tcost=PRICE*n;, System.out.println(“Total Cost:”+tcost);, }, void PURCHASE(), {, Scanner sc=new Scanner(System.in);, System.out.println(“Enter the no. of copies to purchase:”);, int n=sc.nextInt();, TOTAL_COST(n);, }, public static void main(String args[]), {, Book ob=new Book();, ob.INPUT();, ob.PURCHASE();, }, }, 13., , Define a class Flight with the following description:, Instance variables/data members:, fl_no, :, to store the flight number of int type, dest, :, to store the destination of the flight of String type, dist, :, to store the distance of the flight of float type, fuel, :, to store the fuel required by the flight of float type, Member Methods:, i, calfuel() :, to calculate the value of fuel as per the following criteria Distance Fuel, <=1000 500, >1000 and <=2000 1100, >2000 2200, ii feedinfo() to allow user to enter values for Flight Number, Destination, Distance and call, function calfuel() to calculate the quantity of Fuel., iii showinfo() to allow user to view the content of all the data members., Write the main method to create an object of the class and call the above member methods., , Ans., import java.util.*;, class Flight, {, 113, , Class as the Basis of All Computation
Page 25 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , hotel ob=new hotel();, ob.Checkin();, ob.Checkout();, }, }, 15., , Define a class Telephone having the following description:, Instance Variables / Data Members:, int prv, pre, to store the previous and present meter reading, int call, to store the calls made (i.e. pre – prv), String name, to store name of the customer, double amt, to store the amount, double total, to store the total amount to be paid, Member Methods:, void input ( ), -, , to input the previous reading, present reading and name of the, customer, void cal ( ), to calculate the amount and total amount to be paid, void display ( ), to display the name of the customer, calls made, amount and total, amount to be paid in the following format:, Name, Calls Made, Amount, Total Amount, ……........., ......……..…….., ……......…….., ……........……...., Write a program to compute the monthly bill to be paid according to the given conditions:, Calls made, Upto 100 calls, For the next 100 calls, For the next 200 calls, More than 400 calls, , Rate, No Charge, 90 paise per call, 80 paise per call, 70 paise per call, , However every customer has to pay ` 180 per month as monthly rent for availing the service., Ans., import java.util.*;, class Telephone, {, int prv, pre,call;, String name;, double amt,total;, void input(), {, Scanner sc=new Scanner(System.in);, System.out.println(“Enter the previous meter reading:”);, prv=sc.nextInt();, System.out.println(“Enter the present meter reading:”);, pre=sc.nextInt();, Computer Applications – X (ICSE Course) Answers, , 116
Page 26 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , System.out.println(“Enter the name:”);, name=sc.nextLine();, }, void cal(), {, call=pre-prv;, if(call<=100), amt=0;, else if(call>100 && call<=200), amt=0*100+(call-100)*0.90;, else if(call>200 && call<=400), amt=0*100+100*0.90+(call-200)*0.80;, else, amt=0*100+100*0.90+200*0.80+(call-400)*0.70;, total=amt+180;, }, , void display(), {, System.out.println(“Name\t\tCalls Made\t\tAmount\t\tTotal Amount”);, System.out.println(name+“\t\t”+call+“\t\t”+amt+“\t\t”+total);, }, public static void main(String args[]), {, Telephone ob=new Telephone();, ob.input();, ob.cal();, ob.display();, }, }, 16., , Define a class named movieMagic with the following description:, Instance variables/data members:, int year, –, to store the year of release of a movie, String title, –, to store the title of the movie., float rating, –, to store the popularity rating of the movie., (minimum rating = 0.0 and maximum rating = 5.0), Member Methods:, (i) void accept(), (ii) void display() -, , 117, , To input and store year, title and rating., To display the title of a movie and a message based on the rating as, per the table below., , Class as the Basis of All Computation
Page 27 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , Rating, 0.0 to 2.0, 2.1 to 3.4, 3.5 to 4.5, 4.6 to 5.0, , Message to be displayed, Flop, Semi-hit, Hit, Super Hit, , Write a main method to create an object of the class and call the above member methods., Ans., import java.util.*;, class movieMagic, {, int year;, String title;, float rating;, void accept(), {, Scanner sc=new Scanner(System.in);, System.out.println(“Enter the year:”);, year=sc.nextInt();, System.out.println(“Enter the title:”);, title=sc.nextLine();, System.out.println(“Enter the rating:”);, rating=sc.nextFloat();, }, void display(), {, System.out.println(“Title:”+title);, if(rating>=0.0f && rating<=2.0f), System.out.println(“Flop”);, else if(rating>=2.1f && rating<=3.4f), System.out.println(“Semi Hit”);, else if(rating>=3.5f && rating<=4.5f), System.out.println(“Hit”);, else if(rating>=4.6f && rating<=5.0f), System.out.println(“Super Hit”);, }, public static void main(String args[]), {, movieMagic ob=new movieMagic();, ob.accept();, ob.display();, }, }, Computer Applications – X (ICSE Course) Answers, , 118
Page 28 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , 17., , Define a class ParkingLot with the following description:, Instance variables/data members:, int vno, int hours, double bill, , –, –, –, , To store the vehicle number, To store the number of hours the vehicle is parked in the parking lot, To store the bill amount, , Member methods:, void input(), –, void calculate(), –, , To input and store vno and hours, To compute the parking charge at the rate of ` 3 for the first hour or, part thereof, and ` 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,hours;, double bill;, void input(), {, Scanner sc=new Scanner(System.in);, System.out.println(“Enter the vehicle number:”);, vno=sc.nextInt();, System.out.println(“Enter the number of hours:”);, hours=sc.nextInt();, }, void calculate(), {, if(hours<=1), bill=3*hours;, else, bill=3*1+(hours-1)*1.50;, }, void display(), {, System.out.println(“Vehicle number:”+vno);, System.out.println(“Number of hours:”+hours);, System.out.println(“Bill:”+bill);, }, public static void main(String args[]), {, ParkingLot ob=new ParkingLot();, ob.input();, 119, , Class as the Basis of All Computation
Page 29 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , ob.calculate();, ob.display();, }, }, 18., , Define a class named BookFair with the following description:, Instance variables/Data members:, String Bname, –, stores the name of the book., double price, –, stores the price of the book., Member Methods:, (i) void Input(), , –, , To input and store the name and the price of the book., , (ii) void calculate() –, , To calculate the price after discount. Discount is calculated based on, the following criteria:, PRICE, DISCOUNT, Less than or equal to ` 1000, 2% of price, More than ` 1000 and less than or equal or ` 3000, 10% of price, More than ` 3000, 15% of price, (iii) 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., 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>1000 && price<=3000), dis=10/100.0*price;, else, dis=15/100.0*price;, Computer Applications – X (ICSE Course) Answers, , 120
Page 30 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , 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();, }, }, 19., , Define a class Electric Bill with the following specifications:, class: ElectricBill, Instance Variable/ 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 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 :, Number of units –, Rate per unit, First 100 units, –, ` 2.00, Next 200 units, –, ` 3.00, Above 300 units –, ` 5.00, , A surcharge of 2.5% charged if the number of units consumed is above 300 units., • void print(), –, To print the details as follows :, Name of the customer ………, Number of units consumed ……, Bill amount ……., Write a main method to create an object of the class and call the above member methods., Ans. import java.util.*;, class ElectricBill, {, String n;, int units;, double bill;, void accept(), , 121, , Class as the Basis of All Computation
Page 31 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , {, Scanner sc=new Scanner(System.in);, System.out.println(“Enter the name of the customer:”);, n=sc.nextLine();, System.out.println(“Enter the units consumed:”);, units=sc.nextInt();, }, void calculate(), {, double rate=0;, if(units<=100), rate=2*units;, else if(units>100 && units<=300), rate=100*2+(units-100)*3;, else, rate=100*2+200*3+(units-300)*5;, if(units>300), bill=rate+2.5/100*rate;, else, bill=rate;, }, void print(), {, System.out.println(“Name of the customer:”+n);, System.out.println(“Number of units consumed:”+units);, System.out.println(“Bill amount:”+bill);, }, public static void main(String args[]), {, ElectricBill ob=new ElectricBill();, ob.accept();, ob.calculate();, ob.print();, }, }, 20., , Design a class RailwayTicket with the following description:, Instance variables/data members:, String name :, To store the name of the customer, String coach :, To store the type of coach customer wants to travel, long mobno :, To store customer’s mobile number, , Computer Applications – X (ICSE Course) Answers, , 122
Page 32 :
VXL TUTORIAL , 30 K BLOCK KIDWAI NAGAR, KANPUR PH: 9336061496, , int amt, :, int totalamt :, , To store basic amount of ticket, To store the amount to be paid after updating the original amount, , Member methods:, • void accept(), To take input for name, coach, mobile number and amount, • void update(), To update the amount as per the coach selected, (extra amount to be added in the amount as follows), Type of Coaches, , Amount, , First_AC, , 700, , Second_AC, , 500, , Third_AC, , 250, , Sleeper, , None, , • void display(), , -, , To display all details of a customer such as name, coach, total amount, and mobile number., Write a main method to create an object of the class and call the above member methods., Ans., import java.util.*;, class RailwayTicket, {, String name, coach;, long mobno;, int amt,totalamt;, void accept(), {, Scanner sc=new Scanner(System.in);, System.out.println(“Enter the name:”);, name=sc.nextLine();, System.out.println(“Enter the type of coach:”);, coach=sc.nextLine();, System.out.println(“Enter the mobile number:”);, mobno=sc.nextLong();, System.out.println(“Enter the amount:”);, amt=sc.nextInt();, }, void update(), {, double rate=0;, if(coach.equals(“First_AC”)), totalamt=amt+700;, else if(coach.equals(“Second_AC”)), totalamt=amt+500;, 123, , Class as the Basis of All Computation