In this lesson, you will learn
Let’s discuss the different ways to move the (x,y) coordinate on the X and Y plane with help of an example.
The below figure shows how to pass parameters to a method, pass objects to a method, and return objects from the method.
package ch7.l9;
//Objects may be passed to methods.
class MyTest
{
int a, b;
MyTest(int i, int j)
{
a = i;
b = j;
}
// return true if obj is equal to the invoking object
boolean equalTo(MyTest obj)
{
if(obj.a == a && obj.b == b) {
return true;
}
else
return false;
}
}
public class PassingObjToMethod {
public static void main(String[] args) {
MyTest ob1 = new MyTest(10, 11);
MyTest ob2 = new MyTest(10, 11);
MyTest ob3 = new MyTest(-1, -1);
System.out.println("ob1 == ob2: " + ob1.equalTo(ob2));
System.out.println("ob1 == ob3: " + ob1.equalTo(ob3));
}
}
ob1 == ob2: true
ob1 == ob3: false
package ch7.l9;
class Rectangle {
double length;
double breadth;
// Constructor to initialize the dimensions of the rectangle
public Rectangle(double length, double breadth) {
this.length = length;
this.breadth = breadth;
}
// Method to calculate the area of the rectangle
public double calculateArea() {
return length * breadth;
}
}
public class PassingObject {
// Method to compare the areas of two rectangles
public void compareArea(Rectangle rect1, Rectangle rect2) {
double area1 = rect1.calculateArea();
double area2 = rect2.calculateArea();
if (area1 > area2) {
System.out.println("Rectangle 1 is larger with an area of " + area1);
} else if (area2 > area1) {
System.out.println("Rectangle 2 is larger with an area of " + area2);
} else {
System.out.println("Both rectangles have the same area of " + area1);
}
}
// Main method to run a simple test
public static void main(String[] args) {
// Creating two rectangle objects
Rectangle rectangle1 = new Rectangle(5.0, 2.0);
Rectangle rectangle2 = new Rectangle(3.0, 4.0);
// Creating an instance of RectangleOperations
PassingObject po = new PassingObject();
// Comparing the areas of the two rectangles
po.compareArea(rectangle1, rectangle2);
}
}
Rectangle 2 is larger with an area of 12.0
Good content and easy to understand
The content is very good and makes it easier to understand the subject
The lectures are very well structured , easy to understand and quite effective
Notes makes it handy to revise the content also jcode book has a wide range of courses from basic programminv to ai ml courses .
Good material
The content is impressive and also understandable.
You must be logged in to submit a review.