0% found this document useful (0 votes)
125 views

Chapter 2 Class As Basis of All Computation

This document contains questions and answers related to classes in Java. It begins with definitions of key terms like class, composite data type, user-defined data type, and primitive data type. It then discusses class attributes like access specifiers. Several questions cover class concepts such as static members, instance variables, and the new operator. Programming questions at the end provide examples of classes with methods to accept input, perform calculations, and display output.

Uploaded by

Vanita Guntuka
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views

Chapter 2 Class As Basis of All Computation

This document contains questions and answers related to classes in Java. It begins with definitions of key terms like class, composite data type, user-defined data type, and primitive data type. It then discusses class attributes like access specifiers. Several questions cover class concepts such as static members, instance variables, and the new operator. Programming questions at the end provide examples of classes with methods to accept input, perform calculations, and display output.

Uploaded by

Vanita Guntuka
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

ICSE 2022-23

Class X
COMPUTER APPLICATION

Chapter 2 - Class as a basis of all computations

Q.1. Why is a class known as a Composite data type?


Ans.
1. Built in data types contain pre-defined features, to be applied during the
programming.
2. The user may require applying a logical tool that cannot be created by using the
features of a specific primitive data type. For example: a class Student with rollno,
name, std, each with a different primitive datatype.
3. Several primitive data types are used for class data members that are grouped
together to carry out a specific operation.
4. The programmer creates class with the help of various primitive data types thus a
class is called as a composite data type.

Q.2. What is User defined data type?


Ans. 1. User defined data type:- These data types are created by the user.
2. They are available in accordance to their visibility modes throughout the
program.

Q.3. Name the two types of data used in a class.


Ans. 1. User-Defined Data
2. Primitive Data

Q.4. Mention any two attributes required for class declaration.


Ans. 1. Public access specifier
2. class keyword

Q.5. Distinguish between User-defined data type and Primitive data type.
Ans.
User-Defined data type Primitive data type
1. These data types are created 1. These are built-in data types
by the user which are provided by the
system.
2. They are available in 2. These data types are
accordance to their visibility available with the system.
modes throughout the
program.

Q.6. State any two advantages of User-defined classes?


Ans. 1. User is responsible for the validity of data.
2. Implementation details can be hidden.

Q.7. Explain access specifiers. Or name and explain various categories of access
specifiers.
Ans. The Access specifies are the terms used to specify the extent of usage of the class
member in the program. They are also termed as visibility modes which indicate
the program area where class member is visible.
Three categories are public, private and protected.
Public : class member specified as public can be used outside the visibility of the
class.(every where in the program)
Private: class member specified as private can be used only within the scope of a
class.
Protected: Protected members are used in the class as private members which can
only be used within the class but can be applied to another sub class during
inheritance.
The following table shows the accessibility of all access specifiers in java.
Here,
Yes= denotes access is allowed.
No= denotes access is not allowed.
Package = denotes project in java.

Class | Package | Subclass | Subclass |Outside|


| | |(same package)|(diff package)|Class
(diff package)|
————————————+———————+—————————+——————————----+—————————----—+————————
public | Yes | Yes | Yes | Yes | Yes |
————————————+———————+—————————+—————————----—+—————————----—+————————
protected | Yes | Yes | Yes | Yes | No |
————————————+———————+—————————+————————----——+————————----——+————————
default | Yes | Yes | Yes | No | No |
————————————+———————+—————————+————————----——+————————----——+————————
private | Yes | No | No | No | No |
------------+-------+---------+--------------+--------------+--------

Q.8. What is the use of the new operator?


Ans.
1. new operator: - It is used to create a class object or an array.
2. It allocates memory and returns the reference of the memory at run
time.

Q.9. Explain Static data member.


Ans.
1. Static data member: - It is an instant variable which is global in the class and
does not belong to any specific object of the class.
2. There is only one copy of a static data member.
3. This copy is commonly used by all the objects of that class type.

Q.10. Explain Static member method.


Ans.
1. Static member method: - It is a member method which uses only
static data members or static instant variables of the objects.
2. A main method in java is a static member method.

Q11. Explain Local Variable.


Ans: A variable that is accessible only from the function or block in which it is declared
is called a local variable. Its scope is from the line it is declared in until the closing
curly brace of the method within which it is declared.

Q.12. Which of the following functions are illegal and why?


1. class abc(int b)
2. public class pqr(int a)
3. private int x;

Ans. 1. It is illegal because a class cannot have a parameter in its declaration.


2. It is illegal because a class cannot have a parameter in its declaration.
3. It is legal as a variable has been given a data type and an access specifier.

Q.13. Why can't every class be termed as user defined data type?

Ans. The classes that contain public static void main(String args[]) method are not
considered as user defined data type. Only the classes that don't contain this method
are called user defined data type. The presence of public static void main(String args[])
method in a class, converts it into a Java application so it is not considered as a user
defined data type.

Q.14: Explain instance variables. Give an example.

Variables that are declared inside a class without using the keyword 'static' and outside
any member methods are termed instance variables. Each object of the class gets its
own copy of instance variables. For example, in the below class:

class Cuboid {
private double height;
private double width;
private double depth;
private double volume;

public void input(int h, int w, int d) {


height = h;
width = w;
depth = d;
}

public void computeVolume() {


volume = height * width * depth;
System.out.println("Volume = " + volume);
}
}
height, width, depth and volume are instance variables.

Programs

Question 1
Write a class program to accept two numbers as instance variables. Use the
following functions for the given purposes:
Class Name — Calculate
void inputdata() — to input both the values
void calculate() — to find sum and difference
void outputdata() — to print sum and difference of both the numbers
Use a main method to call the functions.
import java.util.Scanner;

public class Calculate


{
private int a;
private int b;
private int sum;
private int diff;

public void inputdata() {


Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
a = in.nextInt();
System.out.print("Enter second number: ");
b = in.nextInt();
}

public void calculate() {


sum = a + b;
diff = a - b;
}

public void outputdata() {


System.out.println("Sum = " + sum);
System.out.println("Difference = " + diff);
}

public static void main(String args[]) {


Calculate obj = new Calculate();
obj.inputdata();
obj.calculate();
obj.outputdata();
}
}

Question 2
Write a class program with the following specifications:
Class Name — Triplet
Data Members — int a, b, c
Member Methods:
void getdata() — to accept three numbers
void findprint() — to check and display whether the numbers are Pythagorean
Triplets or not.

import java.util.Scanner;

public class Triplet


{
private int a;
private int b;
private int c;

public void getdata() {


Scanner in = new Scanner(System.in);
System.out.print("Enter a: ");
a = in.nextInt();
System.out.print("Enter b: ");
b = in.nextInt();
System.out.print("Enter c: ");
c = in.nextInt();
}

public void findprint() {


if ((Math.pow(a, 2) + Math.pow(b, 2)) == Math.pow(c, 2)
|| (Math.pow(b, 2) + Math.pow(c, 2)) == Math.pow(a, 2)
|| (Math.pow(a, 2) + Math.pow(c, 2)) == Math.pow(b, 2))
System.out.print("Numbers are Pythagorean Triplets");
else
System.out.print("Numbers are not Pythagorean Triplets");
}

public static void main(String args[]) {


Triplet obj = new Triplet();
obj.getdata();
obj.findprint();
}
}

Question 3
Define a class Employee having the following description:

Data Members Purpose


int pan To store personal account number
String name To store name
double taxincome To store annual taxable income
double tax To store tax that is calculated
Member functions Purpose
void input() Store the pan number, name, taxable income
void cal() Calculate tax on taxable income
void display() Output details of an employee
Calculate tax based on the given conditions and display the output as per the
given format.

Total Annual Taxable Income Tax Rate


Up to ₹2,50,000 No tax
From ₹2,50,001 to ₹5,00,000 10% of the income exceeding ₹2,50,000
From ₹5,00,001 to ₹10,00,000 ₹30,000 + 20% of the income exceeding
₹5,00,000
Above ₹10,00,000 ₹50,000 + 30% of the income exceeding ₹10,00,000
Output:

Pan Number Name Tax-Income Tax


.......... .... .......... ...
.......... .... .......... ...
import java.util.Scanner;

public class Employee


{
private int pan;
private String name;
private double taxincome;
private double tax;

public void input() {


Scanner in = new Scanner(System.in);
System.out.print("Enter pan number: ");
pan = in.nextInt();
in.nextLine();
System.out.print("Enter Name: ");
name = in.nextLine();
System.out.print("Enter taxable income: ");
taxincome = in.nextDouble();
}

public void cal() {


if (taxincome <= 250000)
tax = 0;
else if (taxincome <= 500000)
tax = (taxincome - 250000) * 0.1;
else if (taxincome <= 1000000)
tax = 30000 + ((taxincome - 500000) * 0.2);
else
tax = 50000 + ((taxincome - 1000000) * 0.3);
}

public void display() {


System.out.println("Pan Number\tName\tTax-Income\tTax");
System.out.println(pan + "\t" + name + "\t"
+ taxincome + "\t" + tax);
}

public static void main(String args[]) {


Employee obj = new Employee();
obj.input();
obj.cal();
obj.display();
}
}

Question 4
Define a class Discount having the following description:

Data Members Purpose


int cost to store the price of an article
String name to store the customer's name
double dc to store the discount
double amt to store the amount to be paid
Member functions Purpose
void input() Stores the cost of the article and name of the customer
void cal() Calculates the discount and amount to be paid
void display() Displays the name of the customer, cost, discount and amount to
be paid
Write a program to compute the discount according to the given conditions and
display the output as per the given format.

List Price Rate of discount


Up to ₹5,000 No discount
From ₹5,001 to ₹10,000 10% on the list price
From ₹10,001 to ₹15,000 15% on the list price
Above ₹15,000 20% on the list price
Output:

Name of the customer Discount Amount to be paid


.................... ........ .................
.................... ........ .................
import java.util.Scanner;

public class Discount


{
private int cost;
private String name;
private double dc;
private double amt;

public void input() {


Scanner in = new Scanner(System.in);
System.out.print("Enter customer name: ");
name = in.nextLine();
System.out.print("Enter article cost: ");
cost = in.nextInt();
}

public void cal() {


if (cost <= 5000)
dc = 0;
else if (cost <= 10000)
dc = cost * 0.1;
else if (cost <= 15000)
dc = cost * 0.15;
else
dc = cost * 0.2;

amt = cost - dc;


}

public void display() {


System.out.println("Name of the customer\tDiscount\tAmount to be paid");
System.out.println(name + "\t" + dc + "\t" + amt);
}

public static void main(String args[]) {


Discount obj = new Discount();
obj.input();
obj.cal();
obj.display();
}
}

Question 5
Define a class Telephone having the following description:

Data Members Purpose


int prv, pre to store the previous and present meter readings
int call to store the calls made (i.e. pre - prv)
String name to store name of the consumer
double amt to store the amount
double total to store the total amount to be paid
Member functions Purpose
void input() Stores the previous reading, present reading and name of the
consumer
void cal() Calculates the amount and total amount to be paid
void display() Displays the name of the consumer, calls made, amount and total
amount to be paid
Write a program to compute the monthly bill to be paid according to the given
conditions and display the output as per the given format.

Calls made Rate


Up to 100 calls No charge
For the next 100 calls 90 paise per call
For the next 200 calls 80 paise per call
More than 400 calls 70 paise per call
However, every consumer has to pay ₹180 per month as monthly rent for availing
the service.

Output:

Name of the customer Calls made Amount to be paid


.................... .......... .................
.................... .......... .................
import java.util.Scanner;

public class Telephone


{
private int prv;
private int pre;
private int call;
private String name;
private double amt;
private double total;

public void input() {


Scanner in = new Scanner(System.in);
System.out.print("Enter Customer Name: ");
name = in.nextLine();
System.out.print("Enter previous reading: ");
prv = in.nextInt();
System.out.print("Enter present reading: ");
pre = in.nextInt();
}
public void cal() {
call = pre - prv;
if (call <= 100)
amt = 0;
else if (call <= 200)
amt = (call - 100) * 0.9;
else if (call <= 400)
amt = (100 * 0.9) + (call - 200) * 0.8;
else
amt = (100 * 0.9) + (200 * 0.8) + ((call - 400) * 0.7);

total = amt + 180;


}

public void display() {


System.out.println("Name of the customer\tCalls made\tAmount to be paid");
System.out.println(name + "\t" + call + "\t" + total);
}

public static void main(String args[]) {


Telephone obj = new Telephone();
obj.input();
obj.cal();
obj.display();
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy