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() {
[Link] = 5.5f;
// Parameterized constructor
public Circle(float radius) {
[Link] = radius;
// Method to calculate the area
public float area() {
return (float) ([Link] * radius * radius); // Use [Link] for more accuracy
// Method to calculate the perimeter
public float perimeter() {
return (float) (2 * [Link] * radius); // Use [Link] 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
[Link]("Circle 1 - Area: " + [Link]() + ", Perimeter: " + [Link]());
[Link]("Circle 2 - Area: " + [Link]() + ", Perimeter: " + [Link]());
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 [Link];
class Circle {
public float radius;
// Default constructor
public Circle() {
[Link] = 5.5f;
}
// Parameterized constructor
public Circle(float radius) {
[Link] = radius;
}
// Method to calculate the area
public float area() {
return (float) ([Link] * radius * radius); // Use [Link] for more accuracy
}
// Method to calculate the perimeter
public float perimeter() {
return (float) (2 * [Link] * radius); // Use [Link] 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 < [Link]; i++) {
float randomRadius = [Link]() * 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 < [Link]; i++) {
[Link]("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 ([Link] < [Link]) {
minCircle = circle;
}
if ([Link] > [Link]) {
maxCircle = circle;
}
totalArea += [Link]();
totalPerimeter += [Link]();
}
// Display circles with minimum and maximum radius
[Link]("\nCircle with Minimum Radius - Radius: " + [Link] +
", Area: " + [Link]() + ", Perimeter: " + [Link]());
[Link]("Circle with Maximum Radius - Radius: " + [Link] +
", Area: " + [Link]() + ", Perimeter: " + [Link]());
// Display average area and perimeter
float averageArea = totalArea / [Link];
float averagePerimeter = totalPerimeter / [Link];
[Link]("\nAverage Area: " + averageArea);
[Link]("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);
[Link]([Link]() + “ ” + [Link]());
}
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() {
[Link] = 20;
[Link] = 10;
[Link] = "Red"; // Default color
}
// Parameterized constructor for length, width, and color
public Rectangle(int length, int width, String color) {
[Link] = length;
[Link] = width;
[Link] = 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
[Link]("Rectangle 1 - Area: " + [Link]() + ", Color: " + [Link]);
[Link]("Rectangle 2 - Area: " + [Link]() + ", Color: " + [Link]);
[Link]("Rectangle 3 - Area: " + [Link]() + ", Color: " + [Link]);
// Find the rectangle with the maximum area
Rectangle maxRectangle = r1;
if ([Link]() > [Link]()) {
maxRectangle = r2;
}
if ([Link]() > [Link]()) {
maxRectangle = r3;
}
6
COMSATS UNIVERSITY ISLAMABAD
// Display the color of the rectangle with the maximum area
[Link]("\nRectangle with the Maximum Area has Color: " +
[Link]);
}
}
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 ([Link]()>[Link]())
[Link]([Link]()+ “” + [Link]()+ “”+ [Link]())
else
[Link]([Link]()+ “” + [Link]()+ “”+ [Link]())
}
}
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) {
[Link] = colour;
[Link] = milage;
[Link] = price;
[Link] = 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() {
[Link]("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
[Link]("Cars with mileage between 100 and 100,000:");
for (Car car : cars) {
if ([Link]() >= 100 && [Link]() <= 100000) {
[Link]();
}
}
// Display data before price change
[Link]("\nData before price change:");
9
COMSATS UNIVERSITY ISLAMABAD
for (Car car : cars) {
[Link]();
}
// Apply 10% price reduction for all cars
for (Car car : cars) {
car.decreasePriceBy10Percent();
}
// Display data after price change
[Link]("\nData after price change:");
for (Car car : cars) {
[Link]();
}
}
}
Output:
10