Computer Applications
Consider the following Java class and answer the questions given below:
class Rectangle
{
int length, breadth;
Rectangle()
{
length = 5;
breadth = 3;
}
Rectangle(int l, int b)
{
length = l;
breadth = b;
}
void area()
{
System.out.println("Area: " + (length * breadth));
}
public static void main(String args[])
{
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle(8, 4);
r1.area();
r2.area();
}
}
(a) Name the type of constructors used in the above class.
(b) What is the output of the method main()?
Java Constructors
1 Like
Answer
(a) Default constructor & Parameterized constructor
(b) Output
Area: 15
Area: 32
Reason
The types of constructors used in the given program are:
1. Default Constructor:
Rectangle()
{
length = 5;
breadth = 3;
}
- A default constructor is a constructor that does not take any parameters.
- It initializes
length = 5
andbreadth = 3
.
2. Parameterized Constructor:
Rectangle(int l, int b)
{
length = l;
breadth = b;
}
- A parameterized constructor is a constructor that takes arguments (
int l
andint b
in this case). - It allows for initializing the instance variables
length
andbreadth
with specific values provided during object creation.
Output of the program
Rectangle r1 = new Rectangle();
Calls the default constructorRectangle()
→length = 5
andbreadth = 3
Rectangle r2 = new Rectangle(8, 4);
Calls the parameterized constructorRectangle(int l, int b)
→length = 8
andbreadth = 4
r1.area();
Prints value of Area →5 x 3
=15
r2.area();
Prints value of Area →8 x 4
=32
Answered By
2 Likes
Related Questions
Why do we need a constructor as a class member?
Write a program in Java to find the roots of a quadratic equation ax2+bx+c=0 with the following specifications:
Class name — Quad
Data Members — float a,b,c,d (a,b,c are the co-efficients & d is the discriminant), r1 and r2 are the roots of the equation.
Member Methods:
- quad(int x,int y,int z) — to initialize a=x, b=y, c=z, d=0
- void calculate() — Find d=b2-4ac
If d < 0 then print "Roots not possible" otherwise find and print:
r1 = (-b + √d) / 2a
r2 = (-b - √d) / 2aConsider the given program and answer the questions given below:
class temp { int a; temp() { a=10; } temp(int z) { a=z; } void print() { System.out.println(a); } void main() { temp t = new temp(); temp x = new temp(30); t.print(); x.print(); } }
(a) What concept of OOPs is depicted in the above program with two constructors?
(b) What is the output of the method main()?
Define a class named FruitJuice with the following description:
Data Members Purpose int product_code stores the product code number String flavour stores the flavour of the juice (e.g., orange, apple, etc.) String pack_type stores the type of packaging (e.g., tera-pack, PET bottle, etc.) int pack_size stores package size (e.g., 200 mL, 400 mL, etc.) int product_price stores the price of the product Member Methods Purpose FruitJuice() constructor to initialize integer data members to 0 and string data members to "" void input() to input and store the product code, flavour, pack type, pack size and product price void discount() to reduce the product price by 10 void display() to display the product code, flavour, pack type, pack size and product price