Results 1 to 20 of 33
- 04-27-2011, 11:32 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
Need Help. with two simple things
OK I have my Circle.java that extends to GeomerticObject.java and my testCircle. the thing is i do not know how to print out the compareTO and the equals in my testCircle.java. If you can just show me one of both ill fix everything else. i attempted it as you can see but its not right :(.
- 04-27-2011, 11:34 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Would you mind posting the code directly instead of as attachments?
- 04-28-2011, 12:03 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
i don't know how to do that. can you tell me. i am new to this
- 04-28-2011, 12:04 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
//Listing 14.1
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
protected GeometricObject() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with color and filled value */
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
* the get method is named isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
/** Return a string representation of this object */
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}
- 04-28-2011, 12:05 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
im sorry here it is
/****************************************
*
* Student Name: LastName, FirstName MiddleName
* Student Email:
* Assignment number:
* Date Due:
* Date Submitted:
* Program Name: TestCircle.java
* Program Description: TestCircle.java
*
****************************************/
public class TestCircle {
public static void main(String[] args) {
Circle circle1 = new Circle(3.0);
Circle circle2 = new Circle(10.0);
Circle circle3 = new Circle(3.0);
String circle1 = new String("3.0");
String circle2 = "10.0";
String circle3 = "3.0";
circle1.getColor();
circle2.getColor();
circle3.getColor();
circle1.isFilled();
circle2.isFilled();
circle3.isFilled();
circle1.getDateCreated();
circle2.getDateCreated();
circle3.getDateCreated();
System.out.println("Circle 1: radius = " + circle1.getRadius());
System.out.println("Circle 2: radius = " + circle2.getRadius());
System.out.println("Circle 3: radius = " + circle3.getRadius());
System.out.println("Circle1 equals Circle 2: " + circle1.equals(circle2));
System.out.println("Circle1 equals Circle 3: " + circle1.equals(circle3));
System.out.println("Circle2 equals Circle 3: " + circle2.equals(circle3));
System.out.println("Circle1 compares to Circle2: " + Max.max(circle1, circle2));
System.out.println("Circle1 compares to Circle3: " + Max.max(circle1, circle3));
System.out.println("Circle2 compares to Circle3: " + Max.max(circle2, circle3));
}
}
- 04-28-2011, 12:10 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
What did you want the printed output to look like?
- 04-28-2011, 12:17 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I apologize, I forgot to include how to use code tags
[code ]<-omit space
YOUR CODE HERE
[/code]
Also, would you mind clarifying the problem for me?
- 04-28-2011, 12:24 AM #8
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
ok. What i was trying to do is have the equals() print True or False based off of the comparison of the three circles. Same thing with the compareTo but instead of the True/False i was trying to get that to print 1, -1, 0
- 04-28-2011, 12:25 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
Java Code:/**************************************** * * Student Name: LastName, FirstName MiddleName * Student Email: * Assignment number: * Date Due: * Date Submitted: * Program Name: Circle.java * Program Description: Circle.java * ****************************************/ public class Circle extends GeometricObject { private double radius; public boolean equals(Circle Circle1) { return this.radius == Circle1.getRadius(); } public int compareTo(Object o) { if (getRadius() > ((Circle)o).getRadius()) return 1; else if (getRadius() < ((Circle)o).getRadius()) return -1; else return 0; } //Keep methods from Listing 14.2 public Circle() { } public Circle(double radius) { this.radius = radius; } /** Return radius */ public double getRadius() { return radius; } /** Set a new radius */ public void setRadius(double radius) { this.radius = radius; } /** Return area */ public double getArea() { System.out.println("getArea() in Circle class."); return radius * radius * Math.PI; } /** Return diameter */ public double getDiameter() { return 2 * radius; } /** Return perimeter */ public double getPerimeter() { System.out.println("getPerimeter() in Circle class."); return 2 * radius * Math.PI; } /* Print the circle info */ public void printCircle() { System.out.println("The circle is created " + getDateCreated() + " and the radius is " + radius); } }
- 04-28-2011, 12:33 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Are you getting errors? I believe to have a compareTo method you need to implement comparable(or comparator, double check which one it is). The code looks good otherwise.
- 04-28-2011, 12:40 AM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
There is no 'Max' class. Math.max(..) returns the larger of the two arguments. For compare to you should call the compareTo method, e.g. circle1.compareTo(circle2). You don't need to implement Comparable just for this (although you might just as well).
- 04-28-2011, 12:44 AM #12
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
i tried to do that with compareTO ill try it again ....and i have to create another class?
- 04-28-2011, 12:45 AM #13
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
o ok nevermind i see. thats what i did for equals to but it do not work
- 04-28-2011, 12:48 AM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The compareTo method can be simplified to use the built in integers compareTo method.
notice I referred to radius directly. You can do this because you are referring to this, which is the class you are in. You only have to use getters on objects, it's safe to access the variables of the class like the call to radius.Java Code:Circle c = (Circle)o; return radius.compareTo(c.getRadius());
You don't have to make a new class, it may be helpful to implement comparator, although I suppose it's not necessary.
- 04-28-2011, 12:51 AM #15
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
The method is 'compareTo' not 'compareTO'.
Er, no, you don't have to... Comparable is an interface that declares the compareTo method. Useful for storing objects in ordered collections, or sorting lists of objects, and so-on. Your Circle (or GeometricObject) class could implement it (you already have the compareTo method in place), but I wouldn't worry about it for now.....and i have to create another class?
- 04-28-2011, 12:53 AM #16
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
- 04-28-2011, 12:56 AM #17
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
When i put this in my class circle it give me this error
double cannot be dereferenced
- 04-28-2011, 01:00 AM #18
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I try to be as lazy as possible with comparing stuff if I can. Fred would you mind showing memthe code that caused the error?
- 04-28-2011, 01:03 AM #19
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
ok well i used the circle1.compareTo(circle2). it works but its not giving me the -1, 1, 0 . what should i do? is it my code in my circle class or the System.out.print in the testcircle thats giving me the regular doubles.
- 04-28-2011, 01:06 AM #20
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
this is what i did sunny but dlorde said it was fine i just changed it back
Java Code:public class Circle extends GeometricObject { private double radius; public boolean equals(Circle Circle1) { return this.radius == Circle1.getRadius(); } public int compareTo(Object o) { Circle c = (Circle)o; return radius.compareTo(c.getRadius()); } //Keep methods from Listing 14.2 public Circle() { } public Circle(double radius) { this.radius = radius; } /** Return radius */ public double getRadius() { return radius; } /** Set a new radius */ public void setRadius(double radius) { this.radius = radius; } /** Return area */ public double getArea() { System.out.println("getArea() in Circle class."); return radius * radius * Math.PI; } /** Return diameter */ public double getDiameter() { return 2 * radius; } /** Return perimeter */ public double getPerimeter() { System.out.println("getPerimeter() in Circle class."); return 2 * radius * Math.PI; } /* Print the circle info */ public void printCircle() { System.out.println("The circle is created " + getDateCreated() + " and the radius is " + radius); } }
Similar Threads
-
Couple hopefully simple things.
By drucey in forum New To JavaReplies: 10Last Post: 11-18-2010, 08:44 PM -
Checking if 2 things are false
By ile4 in forum New To JavaReplies: 4Last Post: 11-16-2010, 10:40 AM -
Things you do if your not in Front of the Computer.
By Eku in forum Forum LobbyReplies: 28Last Post: 09-15-2010, 10:21 AM -
XML, and other things.
By Tortex in forum New To JavaReplies: 5Last Post: 03-28-2010, 05:53 PM -
so whats going on? (things aren't showing up)
By Adrien in forum AWT / SwingReplies: 9Last Post: 02-20-2010, 07:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks