3 Replies - 303 Views - Last Post: 14 May 2014 - 02:04 PM Rate Topic: -----

#1 brittishsnow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 11-October 13

Having issues implementing new interface into code?

Posted 13 May 2014 - 10:11 AM

Hi I am having issues implementing my CompareTo interface in this code. Everything that I have tried says that the Rectangles are equal no matter what. Any help or suggestions is greatly appreciated


Here is my Rectangle Class
/**
 * @author mstow
 *
 */
public class Rectangle {
		double length; //defines the variable length as a double 
		double width; // defines the variable width 
		public Rectangle ()  // defines the variable of width and length as 1
		{
			length = 1;
			width = 1;
			
		}
		public Rectangle (double w, double l) //shortens the variables to one letter 
		{
			width = w;
			length = l;
		}
		public static void displayAreaFormula() //gives the area formula
		{
			System.out.println("The formula for the area of a rectangle is a=l*w"); //prints the area formula
		}
		public static void displayPerimeterFormula() //gives the perimeter formula
		{
			System.out.println("The formula for the perimeter of a rectangle is a=((l*2) + (w*2))"); //prints the perimeter formula
		}
		public double area () //Calculates the area
		{
			double a = (length * width); //area formula
			return (double) a; //returns the calculated area
		}
		public double perimeter () //calculates the perimeter
		{
			double p = ((2 * length) + (2 * width)); //perimeter formula
			return (double) p; //returns the calculated perimeter
		}
		public int  compareTo (Rectangle den,Rectangle  kitchen){
			double denarea = den.area(); //declares denarea as the area of the den
			double kitchenarea = kitchen.area(); //declares kitchenarea as the area of the kitchen 
			double denperimeter = den.perimeter(); //declares denperimeter as the perimeter of the den
			double kitchenperimeter = kitchen.perimeter(); //declares kitchenperimeter as the perimeter of the kitchen
			if (kitchenperimeter == denperimeter && kitchenarea == denarea)  {
				return(0);
			}else if  (kitchenperimeter == denperimeter && kitchenarea <denarea) {
				return(-1);
			} else if (kitchenperimeter == denperimeter && kitchenarea >denarea) {
				return(1);
			}else if (kitchenperimeter >denperimeter && kitchenarea ==denarea) {
				return(2);
			}else if (kitchenperimeter < denperimeter && kitchenarea ==denarea) {
				return(-2);
			} else if (kitchenperimeter >denperimeter && kitchenarea >denarea) {
				return(3);
			} else if (kitchenperimeter <denperimeter && kitchenarea <denarea) {
				return(-3);
			} else if (kitchenperimeter >denperimeter && kitchenarea <denarea) {
				return(4);
			} else if (kitchenperimeter <denperimeter &&kitchenarea >denarea) {
				return(-4);
			}
			return 0;
			}
}



And here is my Tester Class

/**
 * 
 */

/**
 * @author mstow
 *
 */
public class Tester {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Rectangle den = new Rectangle (14, 12); //sets the dimensions of den
		Rectangle kitchen = new Rectangle (10, 12);//sets the dimensions of kitchen
		Rectangle.displayAreaFormula(); //displays the area formula
		Rectangle.displayPerimeterFormula();//displays the perimeter formula
		System.out.println("Rectangle area: " + den.area()); //gives you the area of the den
		System.out.println("Rectangle perimeter: " + den.perimeter());//gives you the perimeter of the den
		double denarea = den.area(); //declares denarea as the area of the den
		double kitchenarea = kitchen.area(); //declares kitchenarea as the area of the kitchen 
		double denperimeter = den.perimeter(); //declares denperimeter as the perimeter of the den
		double kitchenperimeter = kitchen.perimeter(); //declares kitchenperimeter as the perimeter of the kitchen
		if (den.compareTo(kitchen, kitchen) == 0) {
			System.out.println("These rooms  are equal in both perimeter and area.");
			
		} else if (den.compareTo(kitchen) == -1) {
			
			System.out.println("These disk are equal in radius but  are not equal in thickness.");
		}else if (den.compareTo(kitchen) == 1) {
			
			System.out.println("These disk are not  equal in radius but are equal in thickness.");
		}else if (den.compareTo(kitchen) == 2) {
			
			System.out.println("These disk are not equal in radius but  are equal in thickness.");
		}else if (den.compareTo(kitchen) == -2) {
			
			System.out.println("These disk are not  equal in radius and  are not equal in thickness.");
		}else if (den.compareTo(kitchen) == 3) {
			
			System.out.println("These disk are not equal in radius and  are not equal in thickness.");
		}else if (den.compareTo(kitchen) == -3) {
			
			System.out.println("These disk are not equal in radius and  are not equal in thickness.");
		}else if (den.compareTo(kitchen) == 4) {
			
			System.out.println("These disk are equal in radius and  are not equal in thickness.");
		}else if (den.compareTo(kitchen) == -4) {
			
			System.out.println("These disk are equal in radius and are not equal in thickness.");
		}else {
			System.out.println("ID10T ERROR");
		
		}
		
	}

}



Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Having issues implementing new interface into code?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,289
  • Joined: 18-April 07

Re: Having issues implementing new interface into code?

Posted 13 May 2014 - 10:34 AM

if (den.compareTo(kitchen, kitchen) == 0) {



Why would this ever be false? You are comparing kitchen to kitchen. They are the same. I think you meant den.compareTo(den,kitchen) == 0... because the way you setup your compareTo instead of comparing the incoming value to the class itself, you used the two parameters and thus are comparing parameter 1 to parameter 2. Since both are kitchen, they are always the same.

:)
Was This Post Helpful? 1
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • Posts: 3,166
  • Joined: 05-April 11

Re: Having issues implementing new interface into code?

Posted 13 May 2014 - 01:12 PM

Which leads to another question
Why is your compareTo method accepting two Rectangle's?
public int  compareTo (Rectangle den,Rectangle  kitchen)


A compareTo method is supposed to compare the instance itself against the one given argument
There even exists a generic Comparable interface in the Java API which does exactly that
public int compareTo (Rectangle other)


Was This Post Helpful? 1
  • +
  • -

#4 brittishsnow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 11-October 13

Re: Having issues implementing new interface into code?

Posted 14 May 2014 - 02:04 PM

View PostCasiOo, on 13 May 2014 - 01:12 PM, said:

Which leads to another question
Why is your compareTo method accepting two Rectangle's?
public int  compareTo (Rectangle den,Rectangle  kitchen)


A compareTo method is supposed to compare the instance itself against the one given argument
There even exists a generic Comparable interface in the Java API which does exactly that
public int compareTo (Rectangle other)


I am tring to compare the attributes of each rectangle
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1