Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Write a class encapsulating the concept of a circle: Help Needed

  1. #1
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Write a class encapsulating the concept of a circle: Help Needed

    Prompt: Write a class encapsulating the concept of a circle, assuming a circle has the following attributes: a Point representing the center of the circle, and the radius of the circle, and integer. Include a constructor, the accessors and mutators, and methods toString and equals. Also include methods returning the perimeter ( 2 𝜋 𝑟 ) and area ( 𝜋 𝑟^2) of the circle. Write a client (application) class to test all the methods in your class.

    I started out trying to thing how to do this and I mapped out a certain idea but do not know how to incorporate the point represent the center of the circle. I am not sure how to proceed further.. Please give me your wise guidance:

     
    import java.awt.*;
    public class Circle {
     
    public static void main(String[] args) {
     
    final double PI = 3.14;       
    int x,y, radius = 4;
    double area;
    double perimeter;
     
    area = PI * radius * radius;
    perimeter = 2 * PI * radius;
     
    // Output Results
    System.out.println("The area of the circle is " + area );
    System.out.println("The perimeter of the circle is " + perimeter );
    }
     
        int x, y, radius; 
        public Circle(int x, int y, int radius) {  
        this.x = x;
        this.y = y;
        this.radius = radius;
        } 
    }
     
    // For some reason this code has problems.
    // I tried integrating it into the top code and 
    // That failed, not sure why though. 
     
    class public Circle(int x, int y, int radius) {
    this.x = x;
    this.y = y;
    this.radius = radius;
    }
     
    public Circle() {
    this.x = 3;
    this.y = 3;
    this.radius = 6;
    }

    I do not know what to do. I have viewed tutorials, but that makes it even more confusing.... I hope you can assist me.
    Thank you so much for coping with a noob like me.
    Thanks.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    284
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: Write a class encapsulating the concept of a circle: Help Needed

    but do not know how to incorporate the point represent the center of the circle.
    what do you mean by that?
    do you need to know where exactly the center of circle located?
    if that's the case, then, should your circle be in cartesian plane?
    then your circle would have coordinates center(x, y)?

    // For some reason this code has problems.
    // I tried integrating it into the top code and
    // That failed, not sure why though.

    class public Circle(int x, int y, int radius) {
    this.x = x;
    this.y = y;
    this.radius = radius;
    }
    well that is obviously a compilation error. that is invalid syntax
    should it be public class ....?


    your requirement wants to have another to test your Circle class.
    So i think you must have another class that has public static void main
    something like this:
    public class Circle {
     
        int diameter;
        public Circle(int diameter) {
              this.diameter = diameter
        }
     
        // create some methods here
    }

    then test it with other class:
    public class TestCircle {
     
         public static void main(String[] args) {
                Circle circle = new Circle(20);
                // do some stuff here
         }
    }

    by the way, what is the purpose of having the variable x and y?
    Last edited by dicdic; March 19th, 2014 at 11:15 PM.

  3. The Following User Says Thank You to dicdic For This Useful Post:

    Knowledge_Feeds_The_Mind (March 20th, 2014)

  4. #3
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Write a class encapsulating the concept of a circle: Help Needed

    I do not think I need to know exactly, put pretty close to it.
    Yes. "A toString method that prints out the location of the circle (the x, y coordinates of
    the Point) and the radius."

     
    public class TestCircle { 
     
    public static void main(String[] args) {
     
    final double PI = 3.14;  
     
    int x,y, radius = 4;
     
    double area = 24;
     
    double perimeter = 30; 
     
    area = PI * radius * radius;
    perimeter = 2 * PI * radius;
     
    // Output Results
    System.out.println("The area of the circle is " + area );
    System.out.println("The perimeter of the circle is " + perimeter );
    }
    }
     
    public class Circle {
     
    int radius;
    int x,y;
    public Circle( int x, int y, int radius) {
       this.x = 2;
       this.y = 2;   
       this.radius = 4;
     
    }
    }

  5. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    284
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: Write a class encapsulating the concept of a circle: Help Needed

    Okay I get it now. So the x and y are the coordinates.

    "A toString method that prints out the location of the circle (the x, y coordinates of
    the Point) and the radius."
    I think the requirement wants you to override the toString method of Object
    If you are using netbeans or eclipse, just press Ctrl + spacebar and find the toString and click enter.
    that will override the toString method and you must return what you want to print out with that Circle class.
    If you are not using any IDE, just create a public method named toString that has return type of String


    public class TestCircle {

    public static void main(String[] args) {

    final double PI = 3.14;

    int x,y, radius = 4;

    double area = 24;

    double perimeter = 30;

    area = PI * radius * radius;
    perimeter = 2 * PI * radius;

    // Output Results
    System.out.println("The area of the circle is " + area );
    System.out.println("The perimeter of the circle is " + perimeter );
    }
    }

    public class Circle {

    int radius;
    int x,y;
    public Circle( int x, int y, int radius) {
    this.x = 2;
    this.y = 2;
    this.radius = 4;

    }
    }
    I guess you have compilation error with your code?
    each java file can contain only one public class. except for nested public class.
    it would be better if you put your Circle class in another java file.

  6. The Following User Says Thank You to dicdic For This Useful Post:

    Knowledge_Feeds_The_Mind (March 20th, 2014)

  7. #5
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Write a class encapsulating the concept of a circle: Help Needed

    So I reworked the code and now I think I only have to find a way to integrate the second code into the first code



     
    //First Code
     
    import java.awt.Point;
     
    public class NewClass {
     
    public static void main(String[] args) {
     
    final double PI = 3.14;  
     
    int x = 2;
    int y = 2;
    int center = 0;
    int radius = 4;
     
    double area = 24;
     
    double perimeter = 30; 
     
    area = PI * radius * radius;
    perimeter = 2 * PI * radius;
     
     
    // Output Results
    System.out.println("The area of the circle is " + area );
    System.out.println("The perimeter of the circle is " + perimeter );
    System.out.println("The Circle Center is 0, Radius is 4, coordinates are (2,2)");
     
    }
    }
     
    // Second Code
     
    class Point {
      int x;
      int y;
     
      public Point (int x, int y) {
         this.x = x;
         this.y = y;
      }
      // Getter/Setters and other methods
    }
     
    public class Circle {
     
      Point center = null;
      int radius = 0;
     
      public Circle(int x, int y, int rad) {
          center = new Point(x, y);
          radius = rad;
      }
    }

    I need help with these things:

    A Circle constructor that takes a Point object and a radius value as parameters.

    Accessors and mutators (getters and setters) for the Point object and the radius.

    A toString method that prints out the location of the circle (the x, y coordinates of
    the Point) and the radius.

    An equals method that returns true if two circles have the same Point location
    and radius.

    An application class that creates at least one instance of a Circle object.

    Thanks

  8. #6
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    284
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: Write a class encapsulating the concept of a circle: Help Needed

    class Point {
    int x;
    int y;

    public Point (int x, int y) {
    this.x = x;
    this.y = y;
    }
    // Getter/Setters and other methods
    }
    you already have created your own/customized Point class. So why do you have to import java.awt.Point?

    Circle class:
    You need to provide getters for Point and radius in your Circle class.
    A Circle constructor that takes a Point object and a radius value as parameters.
    I think it would be better if the argument of your constructor would be:
    public Circle(Point center, int rad)
    since that what the requirement says.

    Point class
    And in your Point class, you need to override the toString method of Object.
    just create a method public String toString() in Point class. then return the coordinates
    For Example:
    public String toString() {
         return "(" + x + ", " + y + ")";
    }
    and after, try to create Object Point and print it.
    For Example:
    Point center = new Point(2,3);
    System.out.println(center);
    the output of the above code will be (2, 3)

  9. The Following User Says Thank You to dicdic For This Useful Post:

    Knowledge_Feeds_The_Mind (March 20th, 2014)

  10. #7
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Write a class encapsulating the concept of a circle: Help Needed

    So I sat down and reran the entire thing.
    I thought I had it but nope, did not happen

    public class NewCircle {
     
    public static void main(int radius, int x, int y) {
     
    final double PI = 3.14;  
    x = 2;
    y = 2;
    final point center = null; //Says, "assigned value never used"
    radius = 4;
     
    double area = 24;
    double perimeter = 30; 
     
    area = PI * radius * radius;
    perimeter = 2 * PI * radius;
     
    System.out.println("Area of the circle is " + area );
    System.out.println("Perimeter of the circle is " + perimeter );
     
     class Point {
         int x;
         int y;
     
      public Point (final int x, final int y) {
         this.x = x;
         this.y = y;
     
     class Circle{ 
     public String toString() {
     
     center = new Point(2,2); //Cannot assign value to final variable center
     int radius = 4;
     
      System.out.println(center);
      System.out.println("The Circle Center: 0, Radius: 4, coordinates: (2,2)"); 
      return " (" + x +", " + y +")";
     }
     }
     }
     }
    }
    }
    The system seems to contradict itself:
    Whenever I assign "final" to center, the system states. "Local variable center is accessed from within inner class; needs to be declared final" and then when I reassign final to center it states, "Cannot assign a value to final variable center."
    Thank you so much for your help and advice
    Last edited by Knowledge_Feeds_The_Mind; March 21st, 2014 at 01:10 AM. Reason: Pi showed up wrong

  11. #8
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    284
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: Write a class encapsulating the concept of a circle: Help Needed

    final point center = null;
    you declared the class Point with uppercase P
    java is a case sensitive programming language.
    then it should be final Point center not final point center.


    center = new Point(2,2); //Cannot assign value to final variable center
    because you made it final. just remove the final keyword,

  12. The Following User Says Thank You to dicdic For This Useful Post:

    Knowledge_Feeds_The_Mind (March 21st, 2014)

  13. #9
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Write a class encapsulating the concept of a circle: Help Needed

    I finalized my code, but I cannot run it, the program says that there is no main class?
    "public static void main(int radius, int x, int y) {" I thought that this was my main class....
    Do you know what may be causing this?

     
    import java.awt.Point;
     
    public class Circle {
     
        Point center = null;
        int radius = 4; 
     
    public String toString() {
        int x = 2;
        int y = 2;
        return "(" + x + ", " + y + ")";
    } 
     
    public static void main(int radius, int x, int y) {
     
        final double PI = 3.14;  
        x = 2;
        y = 2;
        Point center = new Point(2,2);
        radius = 4;
     
        double area = 24;
        double perimeter = 30; 
     
        area = PI * radius * radius;
        perimeter = 2 * PI * radius;
     
        System.out.println("Area of the circle is " + area );
        System.out.println("Perimeter of the circle is " + perimeter );
        System.out.println(center);
        System.out.println("The Circle Center:(2,2), Radius: 4,");
     
    class Point {
         int x;
         int y;
     
      public Point (final int x, final int y) {
         this.x = x;
         this.y = y;
     
       }
      }
     }
    }
    Many Thanks.
    -Vex

  14. #10
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    284
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: Write a class encapsulating the concept of a circle: Help Needed

    I finalized my code, but I cannot run it, the program says that there is no main class?
    sorry for late reply. I don't do anything related to programming during weekends
    the reason is you don't have main method in your class.
    public static void main(String[] var) is a special method in java. you cannot modify its parameter (except for the variable name of String array)

  15. The Following User Says Thank You to dicdic For This Useful Post:

    Knowledge_Feeds_The_Mind (March 24th, 2014)

Similar Threads

  1. JADE---Sending an Concept including an concept
    By HansDampf in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 25th, 2014, 12:22 AM
  2. [SOLVED] Write a class named Calculator add four methods to that class
    By zahid32 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 30th, 2014, 06:06 AM
  3. Replies: 15
    Last Post: May 2nd, 2013, 05:29 AM
  4. Can't create inner class object outside the outer class help needed?
    By Dark knight in forum Java Theory & Questions
    Replies: 2
    Last Post: July 31st, 2012, 02:34 AM