0% found this document useful (0 votes)
7 views8 pages

Object-Oriented Programming Assignment

assignment 1

Uploaded by

zd64811
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Object-Oriented Programming Assignment

assignment 1

Uploaded by

zd64811
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Object oriented

programming

Class Assignment #1

Submitted by :
Muhammad Zohaib (sp23-bcs-071)
Submitted to :
Mam Hasina Kainat
Question no 1
import [Link];

class Circle {
public float radius;

public Circle() {
[Link] = 5.5f;
}

public Circle(float radius) {


[Link] = radius;
}

public float area() {


return (float) ([Link] * radius * radius);
}

public float perimeter() {


return (float) (2 * [Link] * radius);
}

public static void main(String[] args) {


Circle1[] circles = new Circle[5];
Random rand = new Random();

for (int i = 0; i < [Link]; i++) {


float randomRadius = 1.0f + [Link]() * 9.0f;
circles[i] = new Circle(randomRadius);
[Link]("Circle " + (i + 1) + " - Radius: " + circles[i].radius +
", Area: " + circles[i].area() +
", Perimeter: " + circles[i].perimeter());
}

int maxRadiusIndex = 0, minRadiusIndex = 0;


float totalArea = 0, totalPerimeter = 0;

for (int i = 0; i < [Link]; i++) {


totalArea += circles[i].area();
totalPerimeter += circles[i].perimeter();

if (circles[i].radius > circles[maxRadiusIndex].radius) {


maxRadiusIndex = i;
}

if (circles[i].radius < circles[minRadiusIndex].radius) {


minRadiusIndex = i;
}
}

float averageArea = totalArea / [Link];


float averagePerimeter = totalPerimeter / [Link];

[Link]("\nCircle with Largest Radius: " + circles[maxRadiusIndex].radius);


[Link]("Circle with Smallest Radius: " + circles[minRadiusIndex].radius);
[Link]("Average Area: " + averageArea);
[Link]("Average Perimeter: " + averagePerimeter);
}
}
Question no 2
class Rectangle {

public int length, width;

public String color;

public Rectangle() {

[Link] = 20;

[Link] = 10;

[Link] = "Blue";

public Rectangle(int length, int width, String color) {

[Link] = length;

[Link] = width;

[Link] = color;

public int area() {

return length * width;

public class Main {

public static void main(String[] args) {

Rectangle r1 = new Rectangle();

Rectangle r2 = new Rectangle(30, 15, "Red");

Rectangle r3 = new Rectangle(25, 20, "Green");


[Link]("Area of Rectangle 1: " + [Link]());

[Link]("Area of Rectangle 2: " + [Link]());

[Link]("Area of Rectangle 3: " + [Link]());

Rectangle maxRectangle = r1;

if ([Link]() > [Link]()) {

maxRectangle = r2;

if ([Link]() > [Link]()) {

maxRectangle = r3;

[Link]("Rectangle with Maximum Area is of Color: " + [Link]);

}
Question No 3
class Car {

public String colour;

public int milage;

public long price;

public String owner;

public Car() {

colour = "black";

milage = 0;

price = 120000000;

owner = "Unknown";

public Car(String colour, int milage, long price, String owner) {

[Link] = colour;

[Link] = milage;

[Link] = price;

[Link] = owner;

public String getColour() {

return colour;

public int getMilage() {

return milage;

}
public long getPrice() {

return price;

public String getOwner() {

return owner;

public void setPrice(long newPrice) {

[Link] = newPrice;

public class Main {

public static void main(String[] args) {

Car[] cars = new Car[5];

cars[0] = new Car("white", 500, 20000000, "Alice");

cars[1] = new Car("blue", 1000, 18000000, "Bob");

cars[2] = new Car("red", 95000, 25000000, "Charlie");

cars[3] = new Car("green", 150000, 22000000, "David");

cars[4] = new Car("yellow", 75000, 19000000, "Eve");

[Link]("Cars with mileage between 100 and 100000:");

for (Car car : cars) {

if ([Link]() >= 100 && [Link]() <= 100000) {

[Link]([Link]() + " " + [Link]() + " " + [Link]() + " " +


[Link]());

}
}

[Link]("\nCars before price change:");

for (Car car : cars) {

[Link]([Link]() + " " + [Link]() + " " + [Link]() + " " +


[Link]());

[Link]("\nCars after 10% price reduction:");

for (Car car : cars) {

long newPrice = [Link]() - ([Link]() * 10 / 100);

[Link](newPrice);

[Link]([Link]() + " " + [Link]() + " " + [Link]() + " " +


[Link]());

Common questions

Powered by AI

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 .

You might also like