0% found this document useful (0 votes)
35 views

Oop Assignment 1

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)
35 views

Oop Assignment 1

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
You are on page 1/ 8

Object oriented

programming

Class Assignment #1

Submitted by :
Muhammad Zohaib (sp23-bcs-071)
Submitted to :
Mam Hasina Kainat
Question no 1
import java.util.Random;

class Circle {
public float radius;

public Circle() {
this.radius = 5.5f;
}

public Circle(float radius) {


this.radius = radius;
}

public float area() {


return (float) (Math.PI * radius * radius);
}

public float perimeter() {


return (float) (2 * Math.PI * radius);
}

public static void main(String[] args) {


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

for (int i = 0; i < circles.length; i++) {


float randomRadius = 1.0f + rand.nextFloat() * 9.0f;
circles[i] = new Circle(randomRadius);
System.out.println("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 < circles.length; 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 / circles.length;


float averagePerimeter = totalPerimeter / circles.length;

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


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

public int length, width;

public String color;

public Rectangle() {

this.length = 20;

this.width = 10;

this.color = "Blue";

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

this.length = length;

this.width = width;

this.color = 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");


System.out.println("Area of Rectangle 1: " + r1.area());

System.out.println("Area of Rectangle 2: " + r2.area());

System.out.println("Area of Rectangle 3: " + r3.area());

Rectangle maxRectangle = r1;

if (r2.area() > maxRectangle.area()) {

maxRectangle = r2;

if (r3.area() > maxRectangle.area()) {

maxRectangle = r3;

System.out.println("Rectangle with Maximum Area is of Color: " + maxRectangle.color);

}
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) {

this.colour = colour;

this.milage = milage;

this.price = price;

this.owner = 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) {

this.price = 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");

System.out.println("Cars with mileage between 100 and 100000:");

for (Car car : cars) {

if (car.getMilage() >= 100 && car.getMilage() <= 100000) {

System.out.println(car.getOwner() + " " + car.getColour() + " " + car.getMilage() + " " +


car.getPrice());

}
}

System.out.println("\nCars before price change:");

for (Car car : cars) {

System.out.println(car.getOwner() + " " + car.getColour() + " " + car.getMilage() + " " +


car.getPrice());

System.out.println("\nCars after 10% price reduction:");

for (Car car : cars) {

long newPrice = car.getPrice() - (car.getPrice() * 10 / 100);

car.setPrice(newPrice);

System.out.println(car.getOwner() + " " + car.getColour() + " " + car.getMilage() + " " +


car.getPrice());

You might also like