How can you use the compareTo method with static variables?
-
right now i'm trying to progam a program where you enter two radius' and in the object file it prints out the circumference and area using the toString method and then compares the two circles using the compareTo method. my problem is if i don't make the circumference method and Area method static the compare thingy always comes out the same and if i take out the static the compareTo method works but since i need to program with static methods it doesn't help me....see this is my code... Object file public class Circle { private final static double PI = 3.1415; private static double radius; //private static double Circumference; //private static double Area; private double Circumference; private double Area; //public static double Area (double r) public double Area (double r) { radius = r; Area = PI * radius * radius; return Area; } // public static double Circumference (double r) public double Circumference (double r) { radius = r; Circumference = PI * radius * 2; return Circumference; } public String toString () { return "Circumference is :" + Circumference + "\nArea is: " + Area; } public int compareTo (Object mycircle) { Circle other = (Circle) mycircle; if (other.Area == this.Area) return 0; else if (other.Area > this.Area) return -1; else return 1; } } Main // The "CircleMain2" class. /*************************************… 1.design a object named circle.java that will calculated the area and circumference of a circle 2. static variable for PI 3.Static methods for calculating area and circumference 4. toString () method that returns area and circumference of a circle 5. Object must implement comparable and have comepareTo() method that determines larger circle 6. Design a main that will create two circles, ask user for radius, return circumference and area out of each and sut comepareTo() to determinelarger /*************************************… import cs1.Keyboard; public class CircleMain { public static void main (String[] args) { Circle mycircle = new Circle (); Circle mycircle2 = new Circle (); //**************************************… System.out.print ("Enter a radius for Circle #1 :"); double r = Keyboard.readDouble (); mycircle.Area (r); mycircle.Circumference (r); System.out.println (mycircle + "\n"); //**************************************… System.out.print ("Enter a radius for Circle #2 :"); r = Keyboard.readDouble (); mycircle2.Area (r); mycircle2.Circumference (r); System.out.println (mycircle2 + "\n"); //**************************************… if (mycircle.compareTo (mycircle2) == 0) System.out.println ("Same"); else if (mycircle.compareTo (mycircle2) > 0) System.out.println ("Circle #1 is greater than Circle #2"); else System.out.println ("Circle #2 is greater than Circle #1"); // Place your code here } // main method } // CircleMain2 class
-
Answer:
Try to avoid using static variables unless you mean to share that data between classes (not objects) or are declaring constants (static final). You have > private static double radius; This means that the class has a variable called radius which makes it look as though all instances have the same radius. Whenever you set the radius on one instance, you'll be setting it on all of them so your compare method won't appear to work. This should be: > private double radius; Also, the convention for variable names is that they start with a lower case letter. So, where you've used Circumference, you should really use circumference. Class names have the upper case letter at the beginning. This is just a convention, but it will help you to quickly see when you are reference to primitive variables/instances of objects versus classes.
Grace L at Yahoo! Answers Visit the source
Related Q & A:
- How can I use SSL with django?Best solution by Stack Overflow
- How can I use XMP in Sharepoint metadata?Best solution by Stack Overflow
- How can I use mongo-sync?Best solution by github.com
- Can I use a variable to name other variables?Best solution by Stack Overflow
- How can we get SBJSON connectionDidFinishLoading method inside array data in another class?Best solution by stackoverflow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.