Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Need turning user input into an object

    I need to ask a user for the radius of a circle. Which will then be turned into an object that i can use in a different class to find different measurements. My question is how do i set the user input to an object. Also how do I refernce back to the class to print out those values. Thanks.


    Code:
    public class Circle {
        double radius;
        final double PI = 3.14159;
        
        Circle(double newradius){
            newradius = radius;
        }
        Circle(){
            radius = 0.0;
        }
        void setRadius(double inputradius){
            radius = inputradius;
        }
        double getRadius(){
            return radius;
        }
        
        double getArea(){
           return PI * radius * radius;
        }
        
       double getDiameter(){
           return radius * 2;
        }
       double getCircumference(){
           return 2 * PI * radius;
       }
    }
    Code:
    import java.util.Scanner;
    public class CircleTester {
        public static void main(String [] args){
           
            Scanner scan = new Scanner(System.in);
            
            System.out.println("What is the radius of the circle");
            
            
            System.out.println("The circle's area is ");
            
            System.out.println("The circle's diameter is ");
            
            System.out.println("The circumference is ");
        }
    }
    Thanks again.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You don't. You create an instance and populate it with primitive data provided by the user (and I doubt you'd ask the user to manually enter serialized data like you can from a file).
    So simply ask for the nextDouble off of the scanner, and instantiate the Circle with that provided double: Circle c = new Circle(scan.nextDouble());. That will give you your circle so you can operate on the instance in the following output statements. It doesn't have any error handling in it, but I assume this is for an assignment (in which case error handling has not likely been covered)?

  • Users who have thanked Fou-Lu for this post:

    CodyJava (11-30-2012)

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Yeah we haven't covered that, but thanks that worked. Sorry I'm kinda confused on this one but is that taking the user input and setting it equal to c? I was hopping to set it equal to radius from the other class. Any idea how I do that. Thanks.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Nope, what that does is accepts a double from the input, and creates a new instance of Circle providing it with that double.
    The only way to modify it (such as taking the radius from another class), is to construct a new instance of circle since there is currently no way to mutate the value of the radius. So if you wanted to create a new circle twice the size of an existing circle, you can use the get method on it: Circle doubleSizeCircle = new Circle(smallerCircleInstance.getRadius());. Now, the current class definition you have for Circle has a package level access to the variables, but I always assume that the variables will be private unless its final.

    Edit:
    To expand a bit, since the scope of your current circle's radius property is package accessible, you can actually modify it directly:
    PHP Code:
    Circle c = new Circle();
    c.radius 4.2
    However, this is not the most ideal since it would also allow me to do:
    PHP Code:
    c.radius = -4.2
    which is not a valid raidus.
    Last edited by Fou-Lu; 11-30-2012 at 10:41 PM.

  • Users who have thanked Fou-Lu for this post:

    CodyJava (11-30-2012)

  • #5
    New Coder
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Okay than you for your help so far. I'm not sure if I understand are you saying I can't change the value of radius in the other class?

    Here's the constructors I'm trying to call
    Code:
    public class Circle {
        double radius;
        final double PI = 3.14159;
        
        Circle(double newradius){
            newradius = radius;
        }
        Circle(){
            radius = 0.0;
        }
        void setRadius(double inputradius){
            radius = inputradius;
        }
        double getRadius(){
            return radius;
        }
        
        double getArea(){
           return PI * radius * radius;
        }
        
       double getDiameter(){
           return radius * 2;
        }
       double getCircumference(){
           return 2 * PI * radius;
       }
    }
    Here I'm trying to take the user input and send if back to the Circle class.
    Code:
    import java.util.Scanner;
    public class CircleTester {
        public static void main(String [] args){
           
            Scanner scan = new Scanner(System.in);
            Circle c = new Circle();
            System.out.println("What is the radius of the circle");
            
            c.radius = scan.nextDouble(); //can you do it this way 
                    
               //Below I'm trying to call the values from the other class not sure if their right
    
            System.out.println("The circle's area is "+c.getArea());
            
            System.out.println("The circle's diameter is "+c.getDiameter());
            
            System.out.println("The circumference is "+c.getCircumference());
        
        }
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •