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.

Results 1 to 6 of 6

Thread: IsSquare Method

  1. #1
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post

    Question 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. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    279
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default 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.
    Last edited by John Joe; October 15th, 2017 at 07:42 AM.
    Whatever you are, be a good one

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

    Mounir (October 15th, 2017)

  4. #3
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    279
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default 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;
        }
    }
    Whatever you are, be a good one

  6. #5
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: IsSquare Method

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

  7. #6
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    279
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: IsSquare Method

    You're welcome
    Whatever you are, be a good one

Similar Threads

  1. Replies: 4
    Last Post: October 2nd, 2014, 06:26 AM
  2. Replies: 1
    Last Post: May 27th, 2014, 07:39 PM
  3. Replies: 2
    Last Post: May 27th, 2014, 12:36 PM
  4. Replies: 1
    Last Post: January 23rd, 2013, 07:29 AM
  5. [SOLVED] How to create a Java generic method, similar to a C++ template method?
    By Sharmeen in forum Object Oriented Programming
    Replies: 3
    Last Post: October 18th, 2012, 02:33 AM

Tags for this Thread