KnowledgeBoat Logo
|

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();
    }
}

Java

Java Constructors

10 Likes

Answer

150
20

Working

1. Class Definition:

  • The class report is defined with two integer variables: a and b.

2. Constructors:

  • Default constructor: report()
    This constructor sets the values of a and b to 10 and 15, respectively, when an object is created without any parameters.

  • Parameterized constructor: report(int x, int y)
    This constructor accepts two integer values x and y and assigns them to a and b.

3. Method print():

  • This method prints the product of the two variables a and b.

4. Static method main():
Inside the main() method:

  • First, an object r of class report is created using the default constructor.
  • Then, the print() method is called on r which prints the product 10 * 15 = 150.
  • Next, an object p is created using the parameterized constructor with values 4 and 5.
  • The print() method is called on p which prints the product 4 * 5 = 20.

Answered By

6 Likes


Related Questions