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 12-01-2012, 03:20 AM   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 help passing variable to another class

I'm trying to call the variable r from another class, but I'm having trouble making it.

Code:
public class Circle {
    
    final double PI = 3.14159;
    CircleTester temp = new CircleTester();
    double radius = temp.gettemprad();  //this is where i was gonna call a method but I was having trouble making one in my other class
    
    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){
       double r;
       Circle c = new Circle();
        Scanner scan = new Scanner(System.in);
        
        System.out.println("What is the radius of the circle");
        
        r = scan.nextDouble();
        
        //after I got r to the other class i was going to call the methods below
                
        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());
    
    }
    
}

Basically I want the user input (r) to go to the other class so I can get different measurements from it and then return those values back to the original class using System.out.print. Thanks.
CodyJava is offline   Reply With Quote
Old 12-03-2012, 04:10 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
Since the datatype is primitive, you cannot pass it around and change it. Only objects work via object pointer references in java (and only with mutable object types).
You can pass it to the circle's setRadius method to set the value in the class instance of Circle. Changing it within circle will not modify it in the main, for that you would need to retrieve the value again from the getRadius method (or whatever you are fetching).
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
CodyJava (12-03-2012)
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 04:14 AM.


Advertisement
Log in to turn off these ads.