Computer Applications
Consider the following program segment and answer the questions below:
class calculate
{
int a; double b;
calculate()
{
a=0;
b=0.0;
}
calculate(int x, double y)
{
a=x;
b=y;
}
void sum()
{
System.out.println(a*b);
}}
Name the type of constructors used in the above program segment?
User Defined Methods
ICSE Sp 2025
11 Likes
Answer
The type of constructors used in the above program segment are:
- Default constructor
- Parameterized constructor
Explanation:
The types of constructors used in the given program are:
1. Default Constructor:
calculate() {
a = 0;
b = 0.0;
}
- A default constructor is a constructor that does not take any parameters.
- It initializes the instance variables
aandbwith default values (0and0.0respectively).
2. Parameterized Constructor:
calculate(int x, double y) {
a = x;
b = y;
}
- A parameterized constructor is a constructor that takes arguments (
int xanddouble yin this case). - It allows for initializing the instance variables
aandbwith specific values provided during object creation.
Answered By
4 Likes
Related Questions
Predict the output of the following code snippet:
char ch='B', char chr=Character.toLowerCase(ch); int n=(int)chr-10; System.out.println((char)n+"\t"+chr);A student is trying to convert the string present in x to a numerical value, so that he can find the square root of the converted value. However the code has an error. Name the error (syntax / logical / runtime). Correct the code so that it compiles and runs correctly.
String x= "25"; int y=Double.parseDouble(x); double r=Math.sqrt(y); System.out.println(r);Consider the following program segment and answer the questions given below:
int x[][] = {{2,4,5,6}, {5,7,8,1}, {34, 1, 10, 9}};(a) What is the position of 34?
(b) What is the result of x[2][3] + x[1][2]?
Define a class with the following specifications:
Class name: Bank
Member variables:
double p — stores the principal amount
double n — stores the time period in years
double r — stores the rate of interest
double a — stores the amountMember methods:
void accept () — input values for p and n using Scanner class methods only.
void calculate () — calculate the amount based on the following conditions:Time in (Years) Rate % Upto 1⁄2 9 > 1⁄2 to 1 year 10 > 1 to 3 years 11 > 3 years 12 void display () — display the details in the given format.
Principal Time Rate Amount XXX XXX XXX XXXWrite the main method to create an object and call the above methods.