I keep getting errors in my code I wanted to know what's wrong with my code, and how exactly do I fix it for it to compi

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

I keep getting errors in my code I wanted to know what's wrong with my code, and how exactly do I fix it for it to compi

Post by answerhappygod »

I keep getting errors in my code I wanted to know what's wrong
with my code, and how exactly do I fix it for it to compile and
run
This is my java code:
class Rectangle{

//variables to store the values of the objects
int x;
int y;
int width;
int height;


//constructor to create objects
public Rectangle (int x, int y, int width, int
height){

//this keyword to resolve name
conflict
this.x = x;
this.y = y;
this.width = width;
this.height = height;

//making sure the width and height are
postive
if(width < 0 || height < 0)
System.out.println("Width
and height of rectangle must be positive");
}

//method to get height of the object
public int getHeight(){

//returning the height of thw
object
return height;
}

//method to get width of the object
public int getWidth(){

//returning the width of the
object
return width;
}

//method to get X coordinate of the object
public int getX(){

//returning the X coordinate of the
object
return x;
}

//method to get Y coordinate of the object
public int getY(){

//returning the Y coordinate of the
object
return y;
}

//Overriding equals() method to compare two objects
of class Rectangle
@Override
public boolean equals(Object o){

//if object o is compared to itself,
return true
if (o == this) {
return true;
}

//if o is not an instance of class
Rectangle, return false
if (!(o instanceof Rectangle)) {
return false;
}

public boolean compare(Rectangle
R) {

//Compare the values of objects and
return value accordingly
return Integer.compare(x, R.x) ==
0
&&
Integer.compare(y, R.y) == 0
&&
Integer.compare(width, R.width) == 0
&&
Integer.compare(height, R.height) == 0;

//typecast o to Rectangle to comapre
the values of two objects
Rectangle R = (Rectangle) o;

}

//driver code
public static void main(String[] args){

//creating two objects
Rectangle r1 = new Rectangle(2, 3, 4,
3);
Rectangle r2 = new Rectangle(4, 3, 4,
5);

//calling methods for object r1
System.out.println("X cordinate is = "
+ r1.getX());
System.out.println("Y cordinate is = "
+ r1.getY());
System.out.println("Width = " +
r1.getWidth());
System.out.println("Height = " +
r1.getHeight());

//checking if r1 and r2 are equal
if (r1.equals(r2))

System.out.println("Objects are Equal");
else

System.out.println("Objets are Unequal");

}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply