Oop Assignment 1
Oop Assignment 1
ASSIGNMENT: 1
NAME: QURAISHA AZAM
CLASS: BSDS-4A
1
COMSATS UNIVERSITY ISLAMABAD
QUESTION # 1
class Circle {
// Default constructor
public Circle() {
this.radius = 5.5f;
// Parameterized constructor
this.radius = radius;
return (float) (Math.PI * radius * radius); // Use Math.PI for more accuracy
} //class end
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;
}
// Main function
public static void main(String[] args) {
Circle[] circles = new Circle[5]; // Array of 5 circles
Random rand = new Random();
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());
}
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;
// 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");
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;
}
9
COMSATS UNIVERSITY ISLAMABAD
for (Car car : cars) {
car.displayCarDetails();
}
Output:
10