Object-Oriented Programming Assignment
Object-Oriented Programming Assignment
The Circle class calculates the average area and perimeter by summing all areas and perimeters of circles in the array and dividing the totals by the number of circles. This calculation provides a single representative value of size and boundary length for the set of circles, which can offer insights into their collective characteristics or serve as a benchmark for comparing individual circles .
The Circle class initializes the radius of its objects using two constructors. The default constructor assigns a fixed radius value of 5.5. In contrast, the parameterized constructor allows for setting a custom radius, enabling more flexibility. This approach means that circles can have a default size or be created with a specific size as needed, facilitating diverse use cases .
The main method filters cars by iterating through the Car array and using an if statement to check whether each car's mileage is between 100 and 100,000. This condition is checked using the getMilage() method, and cars meeting this criterion are selected and printed with their owner's name, color, mileage, and price .
The Car class updates car prices by iterating over each car in the array and reducing the price by 10%. This is achieved by first calculating the new price as 90% of the current price (original price minus 10% of the original price) using the getPrice() method. The setPrice() method then updates the price attribute with this new value. This operation impacts the object's price field, permanently reducing each car's price by 10% .
The Car class employs encapsulation by defining attributes such as color, mileage, price, and owner as private variables accessed and modified via public methods like getColour(), getMilage(), getPrice(), getOwner(), and setPrice(). This design restricts direct external access to these attributes, safeguarding object integrity. Encapsulation ensures that attribute modifications are controlled and predictable, enhancing maintainability and security .
The Rectangle class uses overloaded constructors for object instantiation, enabling the creation of Rectangle objects with either default or specified attributes for length, width, and color. This strategy allows for strong flexibility and clarity, as objects can conform to default specifications or possess unique characteristics when instantiated with specific parameters. This approach supports various application scenarios without modifying class structure .
Polymorphism could be integrated into the Rectangle class by creating a hierarchy with a base class shape, allowing other shapes like circles or triangles to share and override common methods (e.g., area calculation). For the Car class, polymorphism can introduce different car types (e.g., ElectricCar, DieselCar) that might override methods related to pricing or mileage. This would enhance flexibility by allowing operations to dynamically interact with different object types based on runtime considerations, promoting extensibility and scaling .
The Circle class iterates over an array of Circle objects, comparing the radius of each circle with the current largest and smallest radii. Two index variables, maxRadiusIndex and minRadiusIndex, track the positions of the circles with the largest and smallest radii, respectively. During each iteration, if a circle's radius is greater than the current maximum, maxRadiusIndex is updated. Similarly, if a circle's radius is smaller than the current minimum, minRadiusIndex is updated .
Using an array to store Circle objects in the Circle class is significant due to its simplicity and efficiency in iterating through a fixed number of elements. Arrays provide fast access and an organized structure for managing multiple Circle instances. However, alternative data structures like ArrayLists could be used, offering dynamic resizing and more flexible handling of elements, albeit with potentially increased overhead .
The Rectangle class uses the area() method, which multiplies the length and width of a rectangle to return its area. In the main method, this area() function is applied to each rectangle object (r1, r2, r3). The program compares areas and keeps track of the rectangle with the greatest area using a variable, maxRectangle, initially set to the first rectangle. This variable is updated if subsequent rectangles have a larger area .