Name: Arinjoy Mervyn Gomes
Roll No: 2405485
Section: CSE-47
Branch: Computer Science & Engineering
Lab Assignment: 3
Program 1: Rectangle using Constructor
Program:
class Rectangle {
double length, breadth;
Rectangle(double l, double b) {
length = l;
breadth = b;
}
double area() {
return length * breadth;
}
public static void main(String[] args) {
Rectangle r1 = new Rectangle(5, 4);
Rectangle r2 = new Rectangle(6, 3);
[Link]("Area of Rectangle 1: " + [Link]());
[Link]("Area of Rectangle 2: " + [Link]());
}
}
Output:
Area of Rectangle 1: 20.0
Area of Rectangle 2: 18.0
Program 2: Rectangle using Method
Program:
class Rectangle {
double length, breadth;
void set(double l, double b) {
length = l;
breadth = b;
}
double area() {
return length * breadth;
}
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle();
[Link](5, 4);
[Link](6, 3);
[Link]("Area of Rectangle 1: " + [Link]());
[Link]("Area of Rectangle 2: " + [Link]());
}
}
Output:
Area of Rectangle 1: 20.0
Area of Rectangle 2: 18.0
Program 3: Count Objects using static
Program:
class CountObject {
static int count = 0;
CountObject() {
count++;
}
public static void main(String[] args) {
CountObject o1 = new CountObject();
CountObject o2 = new CountObject();
CountObject o3 = new CountObject();
[Link]("Number of objects created: " + count);
}
}
Output:
Number of objects created: 3
Program 4: Box Volume
Program:
class Box {
double length, width, height;
Box(double l, double w, double h) {
length = l;
width = w;
height = h;
}
double volume() {
return length * width * height;
}
}
class Demo {
public static void main(String[] args) {
Box b = new Box(5, 4, 3);
[Link]("Volume of box: " + [Link]());
}
}
Output:
Volume of box: 60.0
Program 5: Rectangle Area & Perimeter
Program:
import [Link];
class Rectangle {
double length, breadth;
void read() {
Scanner sc = new Scanner([Link]);
[Link]("Enter length: ");
length = [Link]();
[Link]("Enter breadth: ");
breadth = [Link]();
}
void calculate() {
[Link]("Area: " + (length * breadth));
[Link]("Perimeter: " + (2 * (length + breadth)));
}
void display() {
calculate();
}
public static void main(String[] args) {
Rectangle r = new Rectangle();
[Link]();
[Link]();
}
}
Output:
Enter length: 5
Enter breadth: 4
Area: 20.0
Perimeter: 18.0