2(v)
Consider and give the output of the following program:
class report{
int a, b;
report(){
a = 10;
b = 15;
}
report(int x, int y){
a = x;
b = y;
}
void print(){
System.out.println(a * b);
}
static void main(){
report r = new report();
r.print();
report p = new report(4, 5);
p.print();
}
}Show SolutionHide Solution↓
150
20
1. Class Definition:
- The class
reportis defined with two integer variables:aandb.
2. Constructors:
Default constructor:
report()
This constructor sets the values ofaandbto 10 and 15, respectively, when an object is created without any parameters.Parameterized constructor:
report(int x, int y)
This constructor accepts two integer valuesxandyand assigns them toaandb.
3. Method print():
- This method prints the product of the two variables
aandb.
4. Static method main():
Inside the main() method:
- First, an object
rof classreportis created using the default constructor. - Then, the
print()method is called onrwhich prints the product 10 * 15 = 150. - Next, an object
pis created using the parameterized constructor with values 4 and 5. - The
print()method is called onpwhich prints the product 4 * 5 = 20.