Results 1 to 6 of 6
  1. #1
    alihht is offline Member
    Join Date
    Nov 2009
    Posts
    19
    Rep Power
    0

    Default equals method :::::HELP:::::

    Hi,

    I have two classes :

    Rectangle1:

    PHP Code:
    import java.util.*;
    
    public class Rectangle1
    {
    	/**
    	 *Private variables:
    	 *	#RecLength is a variable to store the length of the rectangle that the user will enter.
    	 *	#RecWidth is a variable to store the width of the rectangle that the user will enter.
    	 *	#RecArea is a variable to store the area of the rectangle after the calculation.
    	 *	#RecParimeter is a variable to store the perimeter of the rectangle after the calculation.*/
    	private int RecLength;
    	private int RecWidth;
    	private int RecArea;
    	private int RecPerimeter;
    	
    	Scanner keyboard = new Scanner(System.in);
    
    	
    	public void rectangleLength()
    	{
    		/**
    		 *Method's function:
    		 *	#To get a number greater than 0 from the user.
    		 *	#The loop used to: re-asking the user to enter a number greater than 0 if the user enter any number less than 1.
    		 *	#Void method to set the length of the rectangle. It will not return any value.*/
    		while (RecLength < 1)
    		{
    	
    		System.out.println("Please, enter the length of the first rectangle: ");
    		RecLength = keyboard.nextInt();
    		
    		}
    	}
    	
    	public void rectangleWidth()
    	{
    		/**
    		 *Method's function:
    		 *	#To get a number greater than 0 from the user.
    		 *	#The loop used to: re-asking the user to enter a number greater than 0 if the user enter any number less than 1.
    		 *	#Void method to set the width of the rectangle. It will not return any value.*/
    		while (RecWidth < 1)
    		{
    		
    		System.out.println("Please, enter the width of the first rectangle: ");
    		RecWidth = keyboard.nextInt();
    		
    		}
    	}
    	
    	public int rectangleArea()
    	{
    		/**
    		 *Method's function:
    		 *	#To calculate the area of the rectangle by using the inputs "The length and the width".
    		 *	#Accessor method to get the area of the rectangle. It will return the value of the area.*/
    		RecArea = RecLength * RecWidth;
    		
    		return RecArea;
    	}
    	
    	public int rectanglePerimeter()
    	{
    		/**
    		 *Method's function:
    		 *	#To calculate the Perimeter of the rectangle by using the inputs "The length and the width".
    		 *	#Accessor method to get the Perimeter of the rectangle. It will return the value of the Perimeter.*/
    		RecPerimeter = (2 * (RecLength + RecWidth));
    		
    		return RecPerimeter;
    	}
    	
    	 public boolean equals (Rectangle1 otherObject)
        {
            return
                (this.RecArea == otherObject.RecArea &&
                this.RecPerimeter == otherObject.RecPerimeter);
        }
    
    	
    }
    Rectanlge2:

    PHP Code:
    import java.util.*;
    
    public class Rectangle2
    {
    	/**
    	 *Private variables:
    	 *	#RecLength is a variable to store the length of the rectangle that the user will enter.
    	 *	#RecWidth is a variable to store the width of the rectangle that the user will enter.
    	 *	#RecArea is a variable to store the area of the rectangle after the calculation.
    	 *	#RecParimeter is a variable to store the perimeter of the rectangle after the calculation.*/
    	private int RecLength;
    	private int RecWidth;
    	private int RecArea;
    	private int RecPerimeter;
    	
    	Scanner keyboard = new Scanner(System.in);
    
    	
    	public void rectangleLength()
    	{
    		/**
    		 *Method's function:
    		 *	#To get a number greater than 0 from the user.
    		 *	#The loop used to: re-asking the user to enter a number greater than 0 if the user enter any number less than 1.
    		 *	#Void method to set the length of the rectangle. It will not return any value.*/
    		while (RecLength < 1)
    		{
    	
    		System.out.println("Please, enter the length of the second rectangle: ");
    		RecLength = keyboard.nextInt();
    		
    		}
    	}
    	
    	public void rectangleWidth()
    	{
    		/**
    		 *Method's function:
    		 *	#To get a number greater than 0 from the user.
    		 *	#The loop used to: re-asking the user to enter a number greater than 0 if the user enter any number less than 1.
    		 *	#Void method to set the width of the rectangle. It will not return any value.*/
    		while (RecWidth < 1)
    		{
    		
    		System.out.println("Please, enter the width of the second rectangle: ");
    		RecWidth = keyboard.nextInt();
    		
    		}
    	}
    	
    	public int rectangleArea()
    	{
    		/**
    		 *Method's function:
    		 *	#To calculate the area of the rectangle by using the inputs "The length and the width".
    		 *	#Accessor method to get the area of the rectangle. It will return the value of the area.*/
    		RecArea = RecLength * RecWidth;
    		
    		return RecArea;
    	}
    	
    	public int rectanglePerimeter()
    	{
    		/**
    		 *Method's function:
    		 *	#To calculate the Perimeter of the rectangle by using the inputs "The length and the width".
    		 *	#Accessor method to get the Perimeter of the rectangle. It will return the value of the Perimeter.*/
    		RecPerimeter = (2 * (RecLength + RecWidth));
    		
    		return RecPerimeter;
    	}
    	
    	public boolean equals (Rectangle2 otherObject)
        {
            return
                (this.RecArea == otherObject.RecArea &&
                	this.RecPerimeter == otherObject.RecPerimeter);
        }
        
       
    }
    And a demo Class with a main method to run the program:

    PHP Code:
    import java.util.*;
    
    public class RectangleDemo
    {
    	public static void main(String[] args)
    	{
    		/**
    		 *Making an object of Recangle1 class and Rectanlge2 class "AreaAndPerimeter1"&"AreaAndPerimeter2".
    		 *Calling "rectangleLength" & "rectangleWidth" methods to the main method to ask the user to enter the length and the width to start the calculation.
    		 *Using "println" method to display the results of the area and the perimeter.*/
    		Rectangle1 AreaAndPerimeter1 = new Rectangle1();
    		Rectangle2 AreaAndPerimeter2 = new Rectangle2();
    		System.out.println(":::::Note::::: The length and the width must be greater than 0.");
    		AreaAndPerimeter1.rectangleLength();
    		AreaAndPerimeter1.rectangleWidth();
    		System.out.println("The area of the first rectangle is: " + AreaAndPerimeter1.rectangleArea());
    		System.out.println("The perimeter of the first rectangle is: " + AreaAndPerimeter1.rectanglePerimeter());
    		System.out.println("***************************");
    		AreaAndPerimeter2.rectangleLength();
    		AreaAndPerimeter2.rectangleWidth();
    		System.out.println("The area of the first rectangle is: " + AreaAndPerimeter2.rectangleArea());
    		System.out.println("The perimeter of the first rectangle is: " + AreaAndPerimeter2.rectanglePerimeter());
    		System.out.println("***************************");
    		System.out.println("***************************");
    		if (AreaAndPerimeter1.equals(AreaAndPerimeter2))
    			 System.out.println ("The area & the perimeter of the rectangles match.");
            else
                System.out.println ("The area & the perimeter of the rectangles don't match.");
                
    	}
    }
    I don't know why the equal method doesn't work correctly?? can anybody help me, please??

    Regards,

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    At the moment you have two equals() methods: one that compares instances of Rectangle1 and another that compares instances of Rectangle2. The if statement that begins "if (AreaAndPerimeter1.equals(AreaAndPerimeter2))" doesn't make a lot of sense.

    Why have two classes anyway?

  3. #3
    alihht is offline Member
    Join Date
    Nov 2009
    Posts
    19
    Rep Power
    0

    Default

    I have to classes because I'm trying to make a project from the book, and the requirements of the project from the book are two classes.

    OK, tell me what should I do here. I search in the book and I applied what's inside. What you said about it "Doesn't make a lot of sense" is from the book. So, what should I do to let it make a lot of sense? Tell me what's the problem and why it doesn't make sense?

    Thank you for your reply, anyways.

    Regards,

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Perhaps you should say what the requirements are then.

    It doesn't make sense to use equals() to compare instances of the two different classes as you do in the RectangleDemo class because you have not defined a method that compares an instance of one class with an instance of another.

  5. #5
    ziaf is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    There some problem with ur equals method.
    In the first class, equals method accept an object of first class.
    Again in the second class , equals method accept an object of second class.
    So when u invoke
    .........if (AreaAndPerimeter1.equals(AreaAndPerimeter2)) .......
    it actually looks for a method equals accepting an object of Rectange2 which is not present in the first class.
    Hence the default method equals in the class Object is getting invoked. So you will never ever get a proper answer
    Last edited by ziaf; 03-09-2010 at 08:10 AM.

  6. #6
    alihht is offline Member
    Join Date
    Nov 2009
    Posts
    19
    Rep Power
    0

    Default

    Aha, I see.

    Know I can see how it doesn't make sense.

    Thank ya.

Similar Threads

  1. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 08:20 PM
  2. How to call the equals method
    By random0munky in forum New To Java
    Replies: 1
    Last Post: 10-12-2009, 06:33 PM
  3. equals method
    By mani_miit in forum Advanced Java
    Replies: 7
    Last Post: 09-09-2009, 11:26 PM
  4. equals method
    By timkd127 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 07:52 PM
  5. Creating a new equals() method help
    By Dave0703 in forum New To Java
    Replies: 2
    Last Post: 09-21-2008, 06:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •