Page 124 - CITS - CSA - TP (Volume 2) - Hindi
P. 124
कं ूटर सॉ वेयर ए ीके शन - CITS
System.out.println(“Area of the rectangle: “ + rectangle.calculateArea());
}
}
आउटपुट:
ीकरण:
• इस ो ाम म , हम लंबाई और चौड़ाई िवशेषताओं के साथ एक वग Rectangle प रभािषत करते ह ।
• हम Rectangle वग के िलए एक िडफ़ॉ कं र दान करते ह , जो लंबाई और चौड़ाई िवशेषताओं को शू पर आरंभ करता है।
• मु िविध म , हम िडफ़ॉ क र का उपयोग करके Rectangle वग का एक उदाहरण बनाते ह ।
• हम आयत के े फल की गणना करने और उसे दिश त करने के िलए Rectangle वग की calculateArea िविध को कॉल करते ह ।
टा 2: आरंभीकरण के साथ िडफ़ॉ कं र: वृ का े फल
//Area of a Circle using Default Constructor with Initialization
class Circle {
double radius;
// Default constructor
Circle() {
radius = 1.0; // Initialize radius to a default value
}
// Method to calculate the area of the circle
double calculateArea() {
return Math.PI * radius * radius;
}
}
public class Circle_Demo {
public static void main(String[] args) {
// Creating an instance of Circle using the default constructor
Circle circle = new Circle();
// Displaying the area of the circle
System.out.println(“Area of the circle: “ + circle.calculateArea());
110
CITS : IT & ITES - कं ूटर सॉ वेयर ए ीके शन - अ ास 97

