0% found this document useful (0 votes)
228 views2 pages

ICSE Class 10 Java Constructor Programs

The document provides specifications for 7 constructors programs involving classes. Program 1 involves a Factorial class with methods to input and display the factorial of a number. Program 2 involves a Prime class to check if a number is prime. Program 3 involves a Pay class to calculate employee salary details. Program 4 involves a Latin class to convert words to pig latin. Program 5 involves string operations like encoding characters. Programs 6 and 7 specify Furniture and Employee classes with methods to input, calculate, and display item and employee data.

Uploaded by

Debadrito Ray
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
228 views2 pages

ICSE Class 10 Java Constructor Programs

The document provides specifications for 7 constructors programs involving classes. Program 1 involves a Factorial class with methods to input and display the factorial of a number. Program 2 involves a Prime class to check if a number is prime. Program 3 involves a Pay class to calculate employee salary details. Program 4 involves a Latin class to convert words to pig latin. Program 5 involves string operations like encoding characters. Programs 6 and 7 specify Furniture and Employee classes with methods to input, calculate, and display item and employee data.

Uploaded by

Debadrito Ray
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

GLOBAL IQ COMPUTER EDUCATION CENTRE

P9, A. M. BOSE ROAD, KOLKATA-74


Ph: 25297261/ 9433103218
CONSTRUCTORS PROGRAMS
Prog1. Write a class Program with the following specifications:
Class Name: factorial
Data Members: int a;
Member Functions:
factorial( ): default constructor to initialize the data member
void input (int m) : to assign a with m
void display( ) : to print the factorial of the number.
Prog2. Write a class Program by using a class with the following specifications:
Class Name: prime
Data Members/ Instance variables: int n
Member Functions:
prime() : constructor to initialize n
void input(int x): to assign n with x
void display(): to check and print whether number n is prime or not.
Prog3. Write a class Program in Java with the following specifications:
Class name -- pay
Data members – String name, address, city
double salary
Member Functions –
pay ( ) – constructor to initialize the data members as given below
name: “Ashok Kumar”
address: “43, Ram Nagar”
city : “Jamshedpur”
salary : 6800.00
void outputdata( ) – to display initialized values
void calculate ( ) – to find and print the followings:
da = 15% of salary
hra = 10% of salary
pf = 12% of salary
gross = salary + da + hra
net = gross – pf
void display ( ) – to display the complete information
Prog4. Write a Program by using a class with the following specifications:
Class Name: latin
Data Members: String name – to contain a word
Member Functions:
latin( ) : default constructor to initialize the string
void input (String n) : to assign name with n
void display () : to display the piglatin form of the word.
Prog5. Write a class Program in Java with the following specifications:
Class Name: Stringop
Data Members: String str
Member Functions:
Stringop( ): to initialize str with NULL
void encode ( ): to replace and print each character of the string with second next character in ASCII table
example: A with C, B with D and so on………
void print ( ): to print each word of the string in a separate line

Page 1
GLOBAL IQ COMPUTER EDUCATION CENTRE
P9, A. M. BOSE ROAD, KOLKATA-74
Ph: 25297261/ 9433103218
Prog6. Define a class named Furniture with the following description:
Instance variables/ Data Members:

int item_code - stores the item code number


String item_name - stores the name of the item
double item_weight - stores the weight of the item (in kg)
int item_price - stores the cost of the item (in Rs.)

Member Methods:
i. Furniture ( ) - Default constructor to initialize data member to 0 / empty value
ii. void input ( ) - To input values for data member
iii. void calc ( ) - To modify item_price by reducing 10% of item_weight is more than 15 kg.
iv. void disp ( ) - Print all data members.

Prog7. Define a class Employee having the following description:


Data Members/ instance variables:
int pan- to store personal account number
String name- to store employee’s name
double taxincome- to store tax that is calculated
double tax- to store tax that is calculated
Member Functions:
Employee()- constructor to initialize pan to 0, name to “” and taxincome & tax to 0
void input ()- to store pan number, name and taxable income
void calculate()- to calculate tax of an employee
Total Annual Taxable Income Tax Rate
Upto Rs. 1,00,000 No Tax
From 1,00,001 to 1,50,000 10% of the income Exceeding Rs.1,00,000
From 1,50,001 to 2,50,000 Rs. 5000+20%of the income Exceeding
Rs.1,50,000
Above Rs.2,50,000 Rs. 25000+30%of the income Exceeding
Rs.2,50,000
void display()- to display the details in the following format
Pan No. Name Tax Income Tax
---------- -------- ---------------- ---------
Create an object in the main() method and call the above methods in it of ‘N’ number of
employees of an organization.

Page 2

Common questions

Powered by AI

The 'Furniture' class uses a method 'calc()' to adjust item prices, whereby if the 'item_weight' exceeds 15 kg, it reduces 'item_price' by 10% to accommodate for higher handling costs. This strategic pricing adjustment is encapsulated within a single method, promoting clarity and maintainability. To ensure all items are appropriately displayed, the 'disp()' method is implemented to print out all data members, ensuring consistency and thoroughness in output representation .

The class 'pay' calculates various salary components such as DA, HRA, and PF within the 'calculate()' method, alongside gross and net salary computations. By encapsulating these calculations within a single method, 'pay' offers a centralized point of salary computation, promoting ease of maintenance, as changes to calculation policies require updates at a singular location. However, this might lead to difficulties in isolating changes in individual components, potentially impacting maintainability if different rates need updates independently. This hard coupling of operations limits flexibility and the reuse of individual salary components in different contexts .

The 'latin' class creates a simple linguistic transformation by converting words into their pig latin form using object-oriented principles. The class encapsulates the transformation rules within methods that process a given word. This encapsulation allows for systematic modification of the string data member 'name' through methods 'input(String n)' and 'display()', which handle data input and output, showing how linguistic rules can be coded into systematic object-oriented processes .

The 'Employee' class stores sensitive tax data, which could be susceptible to unauthorized access if not properly encapsulated. Potential issues include exposure of sensitive information and manipulation risks. To improve security, private access modifiers should be used for member variables, and secure input/output handling practices should be employed to ensure data integrity. Additionally, implementing encryption for sensitive fields and using getter/setter methods can enhance data protection while maintaining encapsulation .

Using default constructors, such as those in 'prime' and 'pay', allows for the predefined initialization of class attributes, ensuring an object starts in a valid state without external input. This promotes object integrity and reduces errors related to uninitialized variables. However, relying solely on default constructors may limit flexibility, necessitating additional methods for setting or adjusting member variables post-instantiation. This approach can lead to increased method calls for initialization but ensures class design simplicity and consistency across object instantiations .

The 'Employee' class implements tax calculation using a 'calculate()' method that applies tiered tax rules based on income brackets. The rules are straightforward and established within fixed brackets, which aligns well with real-world tax systems that define clear taxable income ranges and corresponding rates. This rule application allows for systematic changes and scalability. However, the effectiveness in real-world scenarios may be limited if tax rules change frequently or vary dynamically, requiring potential refactoring or additional complexity in handling exceptions or condition variations .

The 'Stringop' class modifies string data using its 'encode()' method, which traverses the string characters and replaces each character with its second next character in the ASCII table. For instance, 'A' becomes 'C', and 'B' becomes 'D'. This simple form of encryption utilizes ASCII values to shift characters, demonstrating basic string manipulation and character encoding, providing a fundamental understanding of character data transformation .

The class 'prime' does not explicitly demonstrate polymorphism within its own implementation; however, it is set up in a manner that suggests potential for polymorphism. Polymorphism could be applied by extending this class with additional behaviors, such as an overridden 'display()' method to handle composite numbers differently or by using interfaces. The class demonstrates the separation of concerns by having distinct methods for input, initialization, and output, which could be polymorphically replaced or extended in subclasses .

The program classes leverage default initialization by using constructors without parameters, ensuring every object begins with preset values, such as the use of default constructors in 'factorial', 'prime', and 'pay'. This approach streamlines object instantiation by reducing the number of parameters required at creation, simplifying method call sequences, and ensuring all objects start with a valid state. This practice enhances stability and predictability in object behavior, but may necessitate additional setters to allow for state changes post-instantiation .

The 'factorial' class utilizes a default constructor that initializes the data member 'int a' to a default value. The class contains a method 'input(int m)' which assigns the integer 'm' to 'a'. Additionally, it has a 'display()' method that calculates and prints the factorial of the number stored in 'a'. This encapsulation allows the class to manage its own state and provide a clear interface for calculating the factorial of a number with minimal external dependencies .

You might also like