Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-30-2012, 08:05 PM   PM User | #1
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
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.
CodyJava is offline   Reply With Quote
Old 11-30-2012, 08:53 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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)?
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
CodyJava (11-30-2012)
Old 11-30-2012, 09:20 PM   PM User | #3
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
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.
CodyJava is offline   Reply With Quote
Old 11-30-2012, 09:39 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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 09:41 PM..
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
CodyJava (11-30-2012)
Old 11-30-2012, 10:06 PM   PM User | #5
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
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());
    
    }
}
CodyJava is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:17 AM.


Advertisement
Log in to turn off these ads.