Computer Applications
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();
}
}
Answer
150
20
Working
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.
Related Questions
Write the Java statement for creating an object named 'sifra' of the class 'Robot', which takes three double parameters.
Convert the given loop into exit-controlled loop.
int a, b; for(a = 10, b = 1; a >= 1; a -= 2){ b += a; b++; } System.out.print(b);(a) Name one String method which results in positive integer only.
(b) Name one String method which results in a character.
class area{ double area(double r, double h){ double l, a; a = 22.0 / 7 * r * l; l = Math.sqrt(r * r + h * h); return a; } }Specify the type of the error in the above program, correct and write the program to be error free.