Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

compareTo ( ) object

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
This is a program that compiles but doesn't run. I get the error that the constructor rectangle[i]= new CompareRectangle is a malformed expression. Could someone help with this?
THANKS!!! Mary Ellen
CompareRectangle.java--This program will create a class named CompareRectangle
that will extend Rectangle and implement CompareObject. Implementation of the compareTo() method that will compare the rectangles on their areas will occur. A test class will be written to sort a list of CompareRectangle objects.*/
package Chapter7;
public class CompareRectangle extends Rectangle implements CompareObject
{
public static void main(String[] args)
{
//Create rectangles
CompareRectangle[] rectangle = new CompareRectangle[10];
for (int i = 0; i<rectangle.length; i++);>
rectangle[i] = new CompareRectangle(100*Math.random(),5.0, 8.0, "white");

//Construct a CompareRectangle with specified area and color
public CompareRectangle(double width, double height, double w, String c)
{
super(w, c);
}

//Implement compare method defined in CompareObject
public int compareTo(CompareObject otherObject)
{
Rectangle rectangle = (Rectangle)otherObject;
if(findArea() < rectangle.findArea())
return LESS;
else if (findArea() == rectangle.findArea())
return EQUAL;
else return GREATER;
}
public String toString()
{
return "The rectangle area is: " + findArea();
}
}
}
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
the second argument in the for loop should be boolean. here you are calling the constructor which does not return any thing. hence you are getting the error.
regards
pratap
reply
    Bookmark Topic Watch Topic
  • New Topic