0% found this document useful (0 votes)
19 views10 pages

Oop Assignment 1

Uploaded by

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

Oop Assignment 1

Uploaded by

Quraisha Azam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 10

COMSATS UNIVERSITY ISLAMABAD

DEPARTMENT OF COMPUTER SCIENCE

OBJECT ORIENTED PROGRAMMING

ASSIGNMENT: 1
NAME: QURAISHA AZAM

REGISTRATION NO.: SP23-BDS-042

CLASS: BSDS-4A

INSTRUCTOR’S NAME: MISS. HASEENA KAINAT

DATE OF SUBMISSION: 22ND OCTOBER, 2024

1
COMSATS UNIVERSITY ISLAMABAD

QUESTION # 1
class Circle {

public float radius;

// Default constructor

public Circle() {

this.radius = 5.5f;

// Parameterized constructor

public Circle(float radius) {

this.radius = radius;

// Method to calculate the area

public float area() {

return (float) (Math.PI * radius * radius); // Use Math.PI for more accuracy

// Method to calculate the perimeter

public float perimeter() {

return (float) (2 * Math.PI * radius); // Use Math.PI for more accuracy

} //class end

public static void main(String[] args) {

Circle c1 = new Circle(); // Default circle

Circle c2 = new Circle(6.6f); // Circle with specified radius

System.out.println("Circle 1 - Area: " + c1.area() + ", Perimeter: " + c1.perimeter());

System.out.println("Circle 2 - Area: " + c2.area() + ", Perimeter: " + c2.perimeter());

 Add an array of Circles (size 5) in the main function according to above code.

2
COMSATS UNIVERSITY ISLAMABAD
 You can modify the code, if required. Assign random values to radius.

 In the array, find and display the circles having the maximum and the minimum radiuses.

 Also display average area and average perimeter of all the circles.

Solution:
Code:
import java.util.Random;

class Circle {
public float radius;

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

// Parameterized constructor
public Circle(float radius) {
this.radius = radius;
}

// Method to calculate the area


public float area() {
return (float) (Math.PI * radius * radius); // Use Math.PI for more accuracy
}

// Method to calculate the perimeter


public float perimeter() {
return (float) (2 * Math.PI * radius); // Use Math.PI for more accuracy
}

// Main function
public static void main(String[] args) {
Circle[] circles = new Circle[5]; // Array of 5 circles
Random rand = new Random();

// Assign random values to radii


for (int i = 0; i < circles.length; i++) {
float randomRadius = rand.nextFloat() * 10 + 1; // Random radius between 1 and
10
circles[i] = new Circle(randomRadius);
}

// Display areas and perimeters of the circles

3
COMSATS UNIVERSITY ISLAMABAD
for (int i = 0; i < circles.length; i++) {
System.out.println("Circle " + (i+1) + " - Radius: " + circles[i].radius +
", Area: " + circles[i].area() + ", Perimeter: " + circles[i].perimeter());
}

// Find the circle with the minimum and maximum radius


Circle minCircle = circles[0];
Circle maxCircle = circles[0];
float totalArea = 0, totalPerimeter = 0;

for (Circle circle : circles) {


if (circle.radius < minCircle.radius) {
minCircle = circle;
}
if (circle.radius > maxCircle.radius) {
maxCircle = circle;
}
totalArea += circle.area();
totalPerimeter += circle.perimeter();
}

// Display circles with minimum and maximum radius


System.out.println("\nCircle with Minimum Radius - Radius: " + minCircle.radius +
", Area: " + minCircle.area() + ", Perimeter: " + minCircle.perimeter());

System.out.println("Circle with Maximum Radius - Radius: " + maxCircle.radius +


", Area: " + maxCircle.area() + ", Perimeter: " + maxCircle.perimeter());

// Display average area and perimeter


float averageArea = totalArea / circles.length;
float averagePerimeter = totalPerimeter / circles.length;

System.out.println("\nAverage Area: " + averageArea);


System.out.println("Average Perimeter: " + averagePerimeter);
}
}

Output:

4
COMSATS UNIVERSITY ISLAMABAD
QUESTION # 2

class Rectangle
{
public int length,width;
// add constructors here
public Rectangle (): length(20), width(10){
}
public Rectangle (int x, int y): length(x), width(y){
}
// by default length is 20 and width is 10
// add a function that will calculate and return area
public int area()
{return length*width;
}
} // end class
public static void main(String[] args) {
//create two rectangles and display their areas
Rectangle r1, r2(30,15);
System.out.println(r1.area() + “ ” + r2.area());
}
 In the above code , you need add another attribute for colour of a rectangle
 add/modify constructors/functions of your own choice to address the following
points. Create three objects of the Rectangle class and display the colour of a
rectangle having maximum area.

5
COMSATS UNIVERSITY ISLAMABAD
Solution:
Code:
class Rectangle {
public int length, width;
public String color;

// Default constructor with color


public Rectangle() {
this.length = 20;
this.width = 10;
this.color = "Red"; // Default color
}

// Parameterized constructor for length, width, and color


public Rectangle(int length, int width, String color) {
this.length = length;
this.width = width;
this.color = color;
}

// Function to calculate and return area


public int area() {
return length * width;
}

// Main function
public static void main(String[] args) {
// Create three rectangles with different dimensions and colors
Rectangle r1 = new Rectangle(); // Default constructor
Rectangle r2 = new Rectangle(30, 15, "Blue");
Rectangle r3 = new Rectangle(25, 20, "Green");

// Display areas of the rectangles


System.out.println("Rectangle 1 - Area: " + r1.area() + ", Color: " + r1.color);
System.out.println("Rectangle 2 - Area: " + r2.area() + ", Color: " + r2.color);
System.out.println("Rectangle 3 - Area: " + r3.area() + ", Color: " + r3.color);

// Find the rectangle with the maximum area


Rectangle maxRectangle = r1;

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


maxRectangle = r2;
}
if (r3.area() > maxRectangle.area()) {
maxRectangle = r3;
}

6
COMSATS UNIVERSITY ISLAMABAD
// Display the color of the rectangle with the maximum area
System.out.println("\nRectangle with the Maximum Area has Color: " +
maxRectangle.color);
}
}

Output:

QUESTION # 3
class Car{
public string colour;
public int milage, long price;
public Car()
{
colour="black";
milage=0;
price=120000000;
}
Public Car (string c, int m, long pr){
colour=c; milage=m; price=pr;
}
Public string getColour(){ return colour;
}
Public int getMilage(){ return milage;
}
Public long getPrice(){ return price;
}

7
COMSATS UNIVERSITY ISLAMABAD
} // end class
public static void main(String[] args) {
Car c1, c2("white", 100, 20000000);
if (c1.getPrice()>c2.getPrice())
System.out.println(c1.getColour()+ “” + c1.getMilage()+ “”+ c1.getPrice())
else
System.out.println(c2.getColour()+ “” + c2.getMilage()+ “”+ c2.getPrice())
}
}
 Create an array of 5 cars.
 Display the complete data of all the cars having mileage from 100 to 100000.
 You are supposed to add one more attribute that is owner of a car.
 Then change price of each car by decreasing it 10% and display data of all
cars before and after the price change.

Solution:
Code:
class Car {
public String colour;
public int milage;
public long price;
public String owner;

// Default constructor
public Car() {
colour = "black";
milage = 0;
price = 120000000;
owner = "Unknown"; // Default owner
}

// Parameterized constructor
public Car(String colour, int milage, long price, String owner) {
this.colour = colour;
this.milage = milage;
this.price = price;
this.owner = owner;
}
8
COMSATS UNIVERSITY ISLAMABAD
// Getter methods
public String getColour() {
return colour;
}

public int getMilage() {


return milage;
}

public long getPrice() {


return price;
}

public String getOwner() {


return owner;
}

// Method to decrease price by 10%


public void decreasePriceBy10Percent() {
price = price - (price / 10);
}

// Method to display car details


public void displayCarDetails() {
System.out.println("Owner: " + owner + ", Colour: " + colour + ", Milage: " + milage
+ ", Price: " + price);
}

public static void main(String[] args) {


// Create an array of 5 cars
Car[] cars = new Car[5];
cars[0] = new Car("white", 100, 20000000, "Alice");
cars[1] = new Car("blue", 15000, 30000000, "Bob");
cars[2] = new Car("red", 80000, 25000000, "Charlie");
cars[3] = new Car("green", 500, 18000000, "David");
cars[4] = new Car("black", 120000, 22000000, "Eve");

// Display cars with mileage between 100 and 100000


System.out.println("Cars with mileage between 100 and 100,000:");
for (Car car : cars) {
if (car.getMilage() >= 100 && car.getMilage() <= 100000) {
car.displayCarDetails();
}
}

// Display data before price change


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

9
COMSATS UNIVERSITY ISLAMABAD
for (Car car : cars) {
car.displayCarDetails();
}

// Apply 10% price reduction for all cars


for (Car car : cars) {
car.decreasePriceBy10Percent();
}

// Display data after price change


System.out.println("\nData after price change:");
for (Car car : cars) {
car.displayCarDetails();
}
}
}

Output:

10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy