Question
Given: class Rectangle { private int x,y; private int width, height; public Rectangle() { this (0,0,1,1); //statement X } public Rectangle(int width, int height) {
Given:
class Rectangle
{
private int x,y;
private int width, height;
public Rectangle()
{
this (0,0,1,1); //statement X
}
public Rectangle(int width, int height)
{
this (0,0,width,height); //statement Y
}
public Rectangle(int x, int y, int width, int height)
{
this.x= x;
this.y=y;
this.width= width;
this.height= height;
}
public double getArea()
{
return height * width;
}
public double getPerimeter()
{
return 2 * (height + width);
}
}
class RectangleDemo
{
public static void main(String[] args)
{
Rectangle rectangle1 = new Rectangle(3,3);// statement Z
System.out.println("Area of Rectangle is "
+ rectangle1.getArea());
System.out.println("Perimeter of Rectangle is "
+ rectangle1.getPerimeter());
}
}
(a) What is constructor overloading? (b) What is the usage of this keyword in statement (X) and (Y)?
(c) Write all possible statements of (Z).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started