Encapsulation Solutions ICSE Class 10 Computer Applications

ICSE Solutions for Class 10

Get ICSE Encapsulation Solutions Class 10. It will help you to make good preparation before attending the ICSE Board exam. We have provided you with Encapsulation Solutions class 10 solution to score good marks in your exam.

In ICSE CLass 10, Encapsulation Solutions is compulsory to score good marks in computer applications. Students Who are planning to score higher marks in class 10 should practice ICSE Understanding Computer Applications with BlueJ solution for class 10.

ICSE Encapsulation Solutions for Class 10 Computer Applications

A. Tick (✓) the correct option.

1. Which access specifier allows accessibility of a member only within the same class where it is declared?
a. public
b. private
c. protected
d. default

Answer

B

2. Wrapping up of data and method into a single unit is called _________.
a. Encapsulation
b. Abstraction
c. Inheritance
d. Polymorphism

Answer

A

3. An OOP feature that allows all members of a class to be the members of another class.
a. Encapsulation
b. Abstraction
c. Inheritance
d. Polymorphism

Answer

C

4. Which keyword allows Inheritance?
a. extend
b. extends
c. for
d. protected

Answer

B

5. Which access specifier allows accessibility by all classes in the same package, but only by subclasses in a different package?
a. public
b. private
c. protected
d. default

Answer

C

6. A static member a is declared in a class named ‘Myclass’, write the statement to initialise it with 5, from a function in another class.
a. a=5;
b. a of Myclass=5;
c. Myclass.a=5;
d. a.Myclass=5;

Answer

C

7. Which among the following best describes encapsulation?
a. It is a way of combining various data members into a single unit
b. It is a way of combining various member functions into a single unit
c. It is a way of combining various data members and member functions into a single unit which can operate on any data
d. It is a way of combining various data members and member functions that operate on those data members into a single unit

Answer

D

8. If data members are private, what can we do to access them from the class object?
a. Create public member functions to access those data members
b. Create private member functions to access those data members
c. Create protected member functions to access those data members
d. Private data members can never be accessed from outside the class

Answer

A

9. Which feature can be implemented using encapsulation?
a. Inheritance
b. Abstraction
c. Polymorphism
d. Overloading

Answer

B

10. How can Encapsulation be achieved?
a. Using Access Specifiers
b. Using only private members
c. Using inheritance
d. Using Abstraction

Answer

A

Section A

Answer the following questions.

1. What is encapsulation?
Ans. Wrapping up of data and methods into a single unit is called encapsulation.

2. What are access specifiers used for? Name the access specifiers used in Java.
Ans. Access modifiers (or access specifiers) are keywords in object-oriented languages that set the accessibility of classes, methods, and other members. Access modifiers are a specific part of programming language syntax used to facilitate the encapsulation of components.
Access specifiers used in Java are:
• Default
• private
• protected
• public

3. State the difference between:
a. Public and private access specifier
b. Protected and default access specifier
Ans. a. Public access specifier allows accessibility inside a package as well as outside the package.
Private access specifier allows accessibility only within the class.
b. The protected specifier allows access by all subclasses of the class in a program, whatever package they reside in, as well as to other code in the same package. The default specifier allows access by other code in the same package, but not by code that is in subclasses residing in different packages.

4. What can’t a class be declared as private?
Ans. A top-level class as private would be completely useless because nothing would have access to it.

5. Give an example to illustrate overloaded constructor with different access specifier.
Ans. Program to show how overloaded constructors can have different access specifiers and how are they accessed from a different class:
class Access
{
      private int a,b;
      private Access(int x,int y)
      //constructor with private access specifier
      {
            a=x;
            b=y;
      }
      Access(int x) //overloaded constructor with default access.
      {
            a=b=x;
      }
      void show( )
      {
            System.out.println(a+ “ ”+b);
      }
      static void createObject( )
      {
            Access obj=new Access(5,6);
            obj.show();
      }
}
public class MainClass
{
      public static void main(String args[ ])
      {
            /*Access obj=new Access(5,6);Error constructor has private
            access specifier.*/
            Access.createObject();
            Access obj=new Access(7); /*Correct as the default access
            version of the constructor is being called.*/
      }
}

6. What is Inheritance? Which keyword is used to implement inheritance?
Ans. Inheritance is a mechanism wherein a new class is derived from an existing class. The ‘extends’ keyword is used to implement inheritance.

7. What is a package? What is its significance?
Ans. A package is a namespace that organises a set of related pre-compiled classes and interfaces into folders.
Packages are used for:
• Preventing naming conflicts.
• Making searching/locating and usage of classes, interfaces, enumerations and annotations easier
• Providing controlled access: protected and default have package level access control.
• Packages can be considered as data encapsulation (or data-hiding).

8. How are packages created in BlueJ?
Ans. To create a package:
Step 1: Select the Edit menu and select the New Package… option.
Step 2: Enter the name of the package in the ‘Create New Package’ dialog and hit the ‘OK’ button.

9. What do you understand by “Scope of a Variable”?
Ans. The scope of a variable is defined as the extent of the program code within which the variable can be accessed or declared or worked with.

10. What is the scope of argument variables?
Ans. Argument variables have their scope limited only within the method or constructor block where it is declared.

SECTION B

Write programs for the following:

1. Create a class name Check having the following:
private data members: c of char type.
public member functions:
i. Parameterised constructor to initialise it with a character.
ii. To return true if it is in uppercase else return false.
    Also create a main() and call the member methods.

Ans.
import java.util.*;
class Check
{
      private char c;
      public Check(char x)
      {
            c=x;
      }
      public boolean check()
      {
            if(c>=’A’ && c<=’Z’)
                  return true;
            else
                  return false;
      }
      public static void main(String args[])
      {
            char x;
            Scanner sc=new Scanner(System.in);
            System.out.println(“Enter a character:”);
            x=sc.next().charAt(0);
            Check ob=new Check(x);
            if(ob.check()==true)
                  System.out.println(“Uppercase”);
            else
                  System.out.println(“Not in upper case”);
      }
}

2. Define a class Candidate with the following descriptions:
Private Members:
• A data member RNo(Registration Number) of type long
• A data member Name of type String
• A data member Score of type float
• A data member Remarks of type String
• A member function AssignRem() to assign the remarks as per the score obtained by a candidate. Score range and the respective remarks are shown as follows:

Encapsulation

Public Members:
• A function ENTER() to allow user to enter values for Rno, Name, Score and call function
   AssignRem() to assign grade.
• A function DISPLAY() to allow user to view the content of all data members.
   Also create a main() method to create an object and show its implementation by calling the above methods

Ans.
import java.util.*;
class Candidate
{
      private long Rno;
      private String Name;
      private float Score;
      private String Remarks;
      private void AssignRem()
      {
            if(Score>=50)
            Remarks=“Selected”;
            else
            Remarks=“Not Selected”;
      }
      public void ENTER()
      {
            Scanner sc=new Scanner(System.in);
            System.out.println(“Enter the reg. no.:”);
            Rno=sc.nextLong();
            System.out.println(“Enter the name:”);
            Name=sc.nextLine();
            System.out.println(“Enter the score:”);
            Score=sc.nextFloat();
            AssignRem();
      }
      public void DISPLAY()
      {
            System.out.println(“Reg. no.:”+Rno);
            System.out.println(“Name:”+Name);
            System.out.println(“Score:”+Score);
            System.out.println(“Remarks:”+Remarks);
      }
      public static void main(String args[])
      {
            Candidate ob=new Candidate();
            ob.ENTER();
            ob.DISPLAY();
      }
}

3. Define a function RESTRA with the following descriptions:
Private members:
• FoodCode of type int
• Food of type String
• FType of type String
• Sticker of type String
• A member function GetSticker() to assign the following values for Sticker as per the given FType:

Encapsulation

Public Members:
• A function GetFood() to allow user to enter values for FoodCode, Food, FType and call function GetSticker() to assign Sticker.
• A function ShowFood() to allow user to view the content of all the data members.
Also create a main() method to create an object and show its implementation by calling the above methods.

Ans.
import java.util.*;
class RESTRA
{
      private int FoodCode;
      private String Food,FType,Sticker;
      private void GetSticker()
      {
            if(FType.equalsIgnoreCase(“Sticker”))
                  Sticker=“GREEN”;
            else if(FType.equalsIgnoreCase(“Contains Egg”))
                  Sticker=“YELLOW”;
            else if(FType.equalsIgnoreCase(“Non-Vegetarian”))
                  Sticker=“YELLOW”;
      }
      public void GetFood()
      {
            Scanner sc=new Scanner(System.in);
            System.out.println(“Enter the FoodCode:”);
            FoodCode=sc.nextInt();
            System.out.println(“Enter the Food:”);
            Food=sc.nextLine();
            System.out.println(“Enter the Food Type:”);
            FType=sc.nextLine();
            GetSticker();
      }
      public void ShowFood()
      {
            System.out.println(“FoodCode:”+FoodCode);
            System.out.println(“Food:”+Food);
            System.out.println(“Food Type:”+FType);
            System.out.println(“Sticker:”+Sticker);
      }
      public static void main(String args[])
      {
            RESTRA ob=new RESTRA();
            ob.GetFood();
            ob.ShowFood();
      }
}

4. Define a class Seminar with the following specification:
Private members:
• SeminarI                            long
• Topic                                  String
• VenueLocation                   String
• Fee                                     float
• CalcFee()                             function to calculate Fee depending on VenueLocation

Encapsulation

Public members:
• Register() function to accept values for SeminarID, Topic, VenueLocation and call CalcFee() to calculate Fee.
• ViewSeminar() function to display all the data members on the screen.
Also create a main() method to create an object and show its implementation by calling the above methods.

Ans.
import java.util.*;
class Seminar
{
      private long SeminarID;
      private String Topic,VenueLocation;
      private float Fee;
      private void CalcFee()
      {
            if(VenueLocation.equalsIgnoreCase(“Outdoor”))
                  Fee=5000;
            else if(VenueLocation.equalsIgnoreCase(“Indoor Non-AC”))
                  Fee=6500;
            else if(VenueLocation.equalsIgnoreCase(“Indoor AC”))
                  Fee=7500;
      }
      public void Register()
      {
            Scanner sc=new Scanner(System.in);
            System.out.println(“Enter the Seminar ID:”);
            SeminarID=sc.nextInt();
            System.out.println(“Enter the Topic:”);
            Topic=sc.nextLine();
            System.out.println(“Enter the Venue Location:”);
            VenueLocation=sc.nextLine();
            CalcFee();
      }
      public void ViewSeminar()
      {
            System.out.println(“Seminar ID:”+SeminarID);
            System.out.println(“Topic:”+Topic);
            System.out.println(“Venue Location:”+VenueLocation);
            System.out.println(“Fee:”+Fee);
      }
      public static void main(String args[])
      {
            Seminar ob=new Seminar();
            ob.Register();
            ob.ViewSeminar();
      }
}

5. Define a class Graments with the following description:
Private members:
• GCode of type String
• GType of type String
• GSize of type integer
• GFabric of type String
• GPrice of type String
• A function Assign() which calculates and assigns the value of GPrice as follows:
For the GFabric as “COTTON”

Encapsulation

For GFabric other than “COTTON” the above mentioned GPrice gets reduced by 10%.
Public Members:
• A constructor to assign initial values of GCode, GType and GFabric with the words “NOT ALLOTED” and GSize and GPrice with 0.
• A function input() to input the values of the data members of GCode, GType, GFabric, GSize and invoke the Assign function.
• A function Display() which displays the content of all the data members for a Garment.
Also create a main() method to create an object and show its implementation by calling the above methods.

Ans.
import java.util.*;
class Garments
{
      private String GCode,GType,GFabric;
      private int GSize;
      private float GPrice;
      private void Assign()
      {
            if(GType.equalsIgnoreCase(“TROUSER”))
                  GPrice=1300;
            else if(GType.equalsIgnoreCase(“SHIRT”))
                  GPrice=1100;
            if(!(GFabric.equalsIgnoreCase(“COTTON”)))
                  GPrice=GPrice-10/100f*GPrice;
      }
      public Garments()
      {
            GCode=GType=GFabric=”NOT ALLOTED”;
            GPrice=GSize=0;
      }
      public void input()
      {
            Scanner sc=new Scanner(System.in);
            System.out.println(“Enter the GCode:”);
            GCode=sc.nextLine();
            System.out.println(“Enter the GType:”);
            GType=sc.nextLine();
            System.out.println(“Enter the GFabric:”);
            GFabric=sc.nextLine();
            System.out.println(“Enter the GSize:”);
            GSize=sc.nextInt();
            Assign();
      }
      public void Display()
      {
            System.out.println(“GCode:”+GCode);
            System.out.println(“GType:”+GType);
            System.out.println(“GFabric:”+GFabric);
            System.out.println(“GSize:”+GSize);
            System.out.println(“GPrice:”+GPrice);
      }
      public static void main(String args[])
      {
            Garments ob=new Garments();
            ob.input();
            ob.Display();
      }
}

6. Define a class Bus with the following specifications:
Private Members:
• BusNo – to store Bus Number
• From – to store place name of origin
• To – to store place name of the destination
• Type – to store Bus Type ‘O’ for ordinary, ‘E’ for Economy and ‘L’ for luxury
• Distance – to store the distance in kilometres
• Fare – to store the bus fare
• A function CalcFare() to calculate Fare as per the following criteria:

Encapsulation

Public Members:
• A constructor function to initialise Type as ‘O’ and Fare as 500.
• A function Allocate() to allow user to enter values for BusNo, From, To, Type and Distance.
  Also this function should call CalcFare() to calculate Fare.
• A function Show() to display the content of all the data members on the screen.
  Also create a main() method to create an object and show its implementation by calling the above methods.

Ans.
import java.util.*;
class Bus
{
      private String From,To;
      private char Type;
      private int BusNo;
      private float Distance,Fare;
      private void CalcFare()
      {
            if(Type==’O’)
                  Fare=15*Distance;
            else if(Type==’E’)
                  Fare=20*Distance;
            else if(Type==’L’)
                  Fare=24*Distance;
      }
      public Bus()
      {
            Type=’O’;
            Fare=500;
      }
      public void Allocate()
      {
            Scanner sc=new Scanner(System.in);
            System.out.println(“Enter the bus no.:”);
            BusNo=sc.nextInt();
            System.out.println(“Enter the origin:”);
            From=sc.nextLine();
            System.out.println(“Enter the destination:”);
            To=sc.nextLine();
            System.out.println(“Enter the Type of bus:”);
            Type=sc.next().charAt(0);
            System.out.println(“Enter the Distance:”);
            Distance=sc.nextFloat();
            CalcFare();
      }
      public void Show()
      {
            System.out.println(“the bus no.:”+BusNo);
            System.out.println(“the origin:”+From);
            System.out.println(“the destination:”+To);
            System.out.println(“Type of bus:”+Type);
            System.out.println(“Distance:”+Distance);
            System.out.println(“Fare:”+Fare);
      }
      public static void main(String args[])
      {
            Bus ob=new Bus();
            ob.Allocate();
            ob.Show();
      }
}

7. Design a class CABS with the following specifications:
Private Members:
• CNo – to store Cab Number
• Type – to store a character ‘A’, ‘B’ or ‘C’ as city type
• PKM – to store per Kilometer charges
• Dist – to store Distance travelled (in KM)
Public Members:
• A constructor function to initialise Type as ‘A’ and CNo as ‘1111’.
• A function Charges() to assign PKM as per the following table:

Encapsulation

• A function Register() to allow administrator to enter the values for CNo and Type. Also, this function should call Charges() for PKM charges.
• A function ShowCab() to allow user to the Distance and display CNo, Type, PKM, PKM*Distance (as Amount) on screen.
Create another class named Taxi in the same program to create the main() in which an object of the CABS class is to be created and the relevant member functions of it should be called to show its implementation.

Ans.
import java.util.*;
class CABS
{
      private char Type;
      private int CNo;
      private float PKM,Dist;
      public CABS()
      {
            Type=’A’;
            CNo=1111;
      }
      private void Charges()
      {
            if(Type==’A’)
                  PKM=25;
            else if(Type==’B’)
                  PKM=20;
            else if(Type==’L’)
                  PKM=15;
      }
      public void Register()
      {
            Scanner sc=new Scanner(System.in);
            System.out.println(“Enter the cab no.:”);
            CNo=sc.nextInt();
            System.out.println(“Enter the type:”);
            Type=sc.next().charAt(0);
            Charges();
      }
      public void ShowCab()
      {
            Scanner sc=new Scanner(System.in);
            float amount;
            System.out.println(“Enter the cab no.distance travelled:”);
            Dist=sc.nextFloat();
            amount=PKM*Dist;
            System.out.println(“the cab no.:”+CNo);
            System.out.println(“the type:”+Type);
            System.out.println(“PKM:”+PKM);
            System.out.println(“Amount:”+amount);
      }
      public static void main(String args[])
      {
            CABS ob=new CABS();
            ob.Register();
            ob.ShowCab();
      }
}

8. The following question consists of 2 parts, go through it carefully and answer them:
(i) Consider a package named “Number” exist, in which you need to create a class with adequate access specifier with the following specifications.
Class Name: Armstrong
Private Member:
N of int type
Public Member:
• Parameterised constructor to initialise N.
• Function isArmstrong() that checks whether the number in N is an Armstrong number or not and accordingly returns a true or a false.
(ii) Outside the package using suitable import statements create a class called “Check” that will have a main() function defined where an integer is taken from the user using Scanner and using the member functions of the “Number” class check whether it is an Armstrong number or not.

Ans.
(i)
package Number;
public class Armstrong
{
      private int N;
      public Armstrong(int n)
      {
            N=n;
      }
      public boolean isArmstrong()
      {
            int i,d,s=0;
            for(i=N;i>0;i/=10)
            {
                  d=i%10;
                  s+=d*d*d;
            }
            if (s==N)
                  return true;
            else
                  return false;
      }
}

(ii)
import Number.Armstrong;
import java.util.Scanner;
class Check
{
      public static void main(String args[])
      {
            Scanner sc=new Scanner(System.in);
            int n;
            System.out.println(“Enter a number:”);
            n=sc.nextInt();
            Armstrong ob=new Armstrong(n);
            if (ob.isArmstrong())
                  System.out.println(“Armstrong No.”);
            else
                  System.out.println(“Not an Armstrong No.”);
            }
      }

9. Define a class Employee having the following description:-
Private Members:
o int pan                                             to store personal account number
o String name                                      to store name
o double taxincome                              to store annual taxable income.
o double tax                                         to store tax that is calculated
Public Members:
• input() : Store the pan number, name, taxeincome
• calc() : Calculate tax for an employee
• display() : Output details of an employee i.e. display all the data members.
Write a program to compute the tax according to the given conditions and display the output as per 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
Also create a main() function and create 2 objects to calculate tax for 2 employees.

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:”);
            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);
      }
      public static void main(String args[])
      {
            Employee ob1=new Employee ();
            Employee ob2=new Employee ();
            ob1.input();
            ob2.input();
            ob1.calc();
            ob2. calc ();
            ob1.display();
            ob2. display ();
      }
}

10. Define a class Salary described as below that calculates the tax depending upon the salary of a teacher:
private data Members : Name, Address, Phone, Subject Specialization, Monthly Salary, Income Tax.
public 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/-.
Also create a main() function and create 2 objects to calculate tax for 2 teachers.

Ans.
import java.util.*;
class Salary
{
      String Name, Address,subSpe;
      double mSal,it;
      long phone;
      void input()
      {
            Scanner sc=new Scanner(System.in);
            System.out.println(“Enter your name:”);
            Name=sc.nextLine();
            System.out.println(“Enter your address:”);
            Address=sc.nextLine();
            System.out.println(“Enter Subject Specialization:”);
            subSpe=sc.nextLine();
            System.out.println(“Enter Phone No.:”);
            phone=sc.nextLong();
            System.out.println(“Enter monthly salary:”);
            mSal=sc.nextDouble();
      }
      void display()
      {
            System.out.println(“Name:”+Name);
            System.out.println(“Address:”+Address);
            System.out.println(“Subject Specialization:”+subSpe);
            System.out.println(“Phone No.:”+ phone);
            System.out.println(“Monthly salary:”+mSal);
      }
      void calc()
      {
            double aSal;
            aSal=12*mSal;
            if(aSal>175000)
                  it=5/100.0*(aSal-175000);
            else
                  it=0;
      }
      public static void main(String args[])
      {
            Salary ob1=new Salary();
            Salary ob2=new Salary();
            ob1.input();
            ob1.calc();
            ob1.display();
            ob2.input();
            ob2.calc();
            ob2.display();
      }
}

11. A special number is a number in which the sum of the factorial of each digit is equal to the number itself. For example, 145=1!+4!+5! =1+24+120
Design a class Special to check if a given number is a special number using the given members:
Class name : Special
• Class member:
n :Integer
• Static block:
Initialize n with 0
• Class functions:
o int factorial(int p) : calculate and return the factorial of p.
o void isSpecial() : check and display if the number ‘n’ is a special number.
Also create another class named Check that will contain the main() function to input a number and check whether the number is a Special Number or not.

Ans.
import java.util.*;
class Special
{
      static int n;
      static
      {
            n=0;
      }
      static int factorial(int p)
      {
            int i,f=1;
            for(i=1;i<=p;i++)
                  f=f*i;
            return f;
      }
      static void isSpecial()
      {
            int i,d,s=0;
            for(i=n;i>0;i/=10)
            {
                  d=i%10;
                  s+=factorial(d);
            }
            if(s==n)
                  System.out.println(“Special Number”);
            else
                  System.out.println(“Not a Special Number”);
      }
}
public class Check
{
      public static void main(String args[])
      {
            Scanner sc=new Scanner(System.in);
            System.out.println(“Enter a number:”);
            Special.n=sc.nextInt();
            Special.isSpecial();
      }
}

ICSE Encapsulation Solutions for class 10 Computer Application

We believe that every student can get good marks by solving the Encapsulation Solutions class 10 solution. So, let’s start to learn the Encapsulation Solutions ICSE class 10 solution for your exam.

You can also learn revision note for class 10 computer application prepared by our expert teachers at icseboards.com