Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: IsSquare Method

1. ## IsSquare Method

HI

I have this question :

Suppose we have a Rectangle class that begins:

class Rectangle {

private Point upperLeft; // the upper left corner point
private double height; // the height of the rectangle
private double width; // the width of the rectangle
isSquare method

Write an isSquare method that returns true if the rectangle is a square (and false otherwise).

I have wrote this code but it doesnt work....

package assignement01;

import java.awt.Point;
import java.util.Scanner;

public class Assignement01 {

public static void main(String[] args) {
// TODO code application logic here

Scanner x = new Scanner(System.in);
System.out.print("Enter Rectangle height: ");
double height = x.nextDouble();
System.out.print("Enter Rectangle width: ");
double width = x.nextDouble();

class Rectangle {

private Point upperLeft; // the upper left corner point
private double height; // the height of the rectangle
private double width; // the width of the rectangle

public boolean isSquare() {
if (height == width){
boolean answer = true;
System.out.println(" The answer is" + answer);
}
else{
boolean answer = false;
System.out.println(" The answer is" + answer);

}
}
}
}
}

2. ## Re: IsSquare Method

There are few errors in your code:
1. No need to declare two classes. One classes have enough.
2. isSquare method will not get called as you are not defining it.

3. ## The Following User Says Thank You to John Joe For This Useful Post:

Mounir (October 15th, 2017)

4. ## Re: IsSquare Method

Hi

Thanks for youre help.

The thing is that if it was me, i wouldnt add the first class, but as said on the subject we wave to use the rectangle class otherwise it will be wrong.

2. how do i call de isSquare method then ?

Many Thanks

5. ## Re: IsSquare Method

Try this
```public class Rectangle {

private Point upperLeft; // the upper left corner point
private double height; // the height of the rectangle
private double width; // the width of the rectangle

public static void main(String[] args) {
// TODO code application logic here
Rectangle r = new Rectangle();
Scanner x = new Scanner(System.in);
System.out.print("Enter Rectangle height: ");
double height = x.nextDouble();
System.out.print("Enter Rectangle width: ");
double width = x.nextDouble();
r.isSquare(height, width); // call method
}

public boolean isSquare(double height,double width) {
boolean answer;
if (height == width) {
answer = true;
System.out.println(" The answer is" + answer);
} else {
answer = false;
System.out.println(" The answer is" + answer);
}
return answer;
}
}```

6. ## Re: IsSquare Method

Yes it work ! I've seen were was my mistake ! Thanks a lot

7. ## Re: IsSquare Method

You're welcome