Results 1 to 15 of 15
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By monkeyjr97

Thread: Need help getting variable from main class to another

  1. #1
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Need help getting variable from main class to another

    Hey I basically trying to get the user to input a value and then take that value and send it to another class to perform different things on it. Then get those values back by referencing to them in a system out print. The main problem is I can't make a variable that will allow me to use it in the main method and use it in another method in the same class so I can send it to the other class.

    Java 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();
            
                    
            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());
        
        }
        
    }
    In this class I want to get the value (r) from the about class.
    Java Code:
    public class Circle {
        
        final double PI = 3.14159;
        CircleTester temp = new CircleTester();
        double radius = temp.r;  // not sure how to call it 
        
        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;
       }
    }
    Any help is appreciated thanks.

  2. #2
    Lowest0ne is offline Member
    Join Date
    Apr 2012
    Posts
    21
    Rep Power
    0

    Default Re: Need help getting variable from main class to another

    A circle creates a circle tester which creates a circle... Talk about circles :)

    What is the purpose of circle tester? You don't test that something is a circle.

    I would make a constructor which takes a scanner as a parameter:
    Java Code:
    Circle(Scanner scan)
    {
      System.out.print("Enter the radius: ");
      radious = scan.nextDouble();
    }
    and then have a toString():
    Java Code:
    class Circle
    {
      public String toString()
      {
        return new String(Double.toString(radius); // Or whatever else you need
      }
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Need help getting variable from main class to another

    Quote Originally Posted by Lowest0ne View Post
    A circle creates a circle tester which creates a circle... Talk about circles :)

    What is the purpose of circle tester? You don't test that something is a circle.

    I would make a constructor which takes a scanner as a parameter:
    Java Code:
    Circle(Scanner scan)
    {
      System.out.print("Enter the radius: ");
      radious = scan.nextDouble();
    }
    and then have a toString():
    Java Code:
    class Circle
    {
      public String toString()
      {
        return new String(Double.toString(radius); // Or whatever else you need
      }
    }

    To the original poster, I strongly recommend that you disregard this well meaning but I think misleading advice. Do not have the Circle constructor interact with the user as that's not its responsibility and is a bad design. Hang on a sec and let me see if I can give you a better recommendation.

    Also regarding the toString, there is almost never any reason to call new String(...) as this bypasses the String pool.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Need help getting variable from main class to another

    OK, on looking at your code, I again verify that Circle should have absolutely no user interface code inside of it. Your Circle class looks pretty good and I wouldn't change it at all and in fact most of your code looks very good and is close to a solution.

    I suggest that in your main method you:
    • First create your Scanner object
    • Then prompt the user for a valid radius and place it in your r double variable.
    • Then use this r to create a Circle object that has this radius. Your Circle class has a constructor that will accept that double, so you should use it.
    • Then to get the results back call the methods of your Circle object, c, just as you're doing.

  5. #5
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Need help getting variable from main class to another

    Thank you both but I think I figured out how to send it to the other class. This was for an assignment and we were suppose to make a constructor the set radius equal to 0.0 but, I guess not use that and use the user input (I guess he was trying to see if we knew how). Any way now my problem is whenever I call those measurements they are all 0.0 so it's using the set radius value not the user input.

    Java Code:
    import java.util.Scanner;
    public class CircleTester {
        public static void main(String [] args){
           double r;
           
            Scanner scan = new Scanner(System.in);
            
            System.out.print("What is the radius of the circle:");
            r = scan.nextDouble();
            
            Circle c = new Circle();
            c.getRad(r);
            
            
            
                    
            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());
        
        }
        
    }
    Java Code:
    public class Circle {
        
        final double PI = 3.14159;
        CircleTester temp = new CircleTester();
        double radius;
    
        Circle(){                         //It's using this value but I want it to use the user input, but he told us to put this...
            radius = 0.0;
        }
        
        Circle(double newradius){
            newradius = radius;
        }
        
        public void getRad(double r){
            r = radius;
        }
        
        void setRadius(double r){
            radius = r;
        }
        double getRadius(){
            return radius;
        }
        
        double getArea(){
           return PI * radius * radius;
        }
        
       double getDiameter(){
           return radius * 2;
        }
       double getCircumference(){
           return 2 * PI * radius;
       }
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Need help getting variable from main class to another

    Please re-read my answer. You are using the wrong Circle constructor. Either change constructors or use a setter method, not a getter method.
    codyjava likes this.

  7. #7
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Need help getting variable from main class to another

    Okay I think I got it I got rid of c.getRadius(r) and put c.radius = r; which changes the value ha thanks. 1 assignment down 1 more to go I'll probably be posting here again thanks for all the help.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Need help getting variable from main class to another

    Quote Originally Posted by codyjava View Post
    Okay I think I got it I got rid of c.getRadius(r) and put c.radius = r; which changes the value ha thanks. 1 assignment down 1 more to go I'll probably be posting here again thanks for all the help.
    No, you don't want to directly manipulate class fields. Your code would be much better off if you use the constructor that takes a double as per my recommendations above or if you use a setter method.

  9. #9
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Need help getting variable from main class to another

    as Fubarable says:

    you have the constructor:

    Java Code:
    Circle(double newradius){
            newradius = radius;
    }
    and this takes an argument, what ever it maybe so pass this argument into the parameters of the constructot:
    Java Code:
    public static void main(String [] args){
           double r;
            
            Scanner scan = new Scanner(System.in);
             
            System.out.print("What is the radius of the circle:");
            r = scan.nextDouble();
             
            Circle c = new Circle();
            c.getRad(r); // no point to this
             }
    you want to pass r into the constructor so how would you go about doing this?
    at the moment you are creating the circle() which will return 0.0 as you made it do so in the circle() constructor.

  10. #10
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Need help getting variable from main class to another

    Okay yeah I know I'm not even using have of it I'm just doing it like half way by setting the actual value to it. Could you actual show me what the 2 of you mean I'm more of a visual learner sorry. I think what your sauing is send it to the Circle constructor and then use the newradius value? How would I do that. Thanks kinda a noob to all this.

  11. #11
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Need help getting variable from main class to another

    right what we have here is 2 constructors of circle:
    Circle()
    and
    Circle(double newradius)

    Java Code:
    //when you say:
    Circle c=new Circle();
    //you call the circle constructor that sets radius at 0.0 as defined in circle() hence why getRadius(returns 0.0)
    This is the one you want to call, substitue the above method for this one that takes a DOUBLE as an argument. this double will become the result
    of the Scanner input think about this

    so pass this into the consructor:

    Java Code:
    public static void main(String [] args){
           double r;
            
            Scanner scan = new Scanner(System.in);
             
            System.out.print("What is the radius of the circle:");
            r = scan.nextDouble();
             
            Circle c = new Circle(//pass argument: think about what this is);
           
             }

  12. #12
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Need help getting variable from main class to another

    I understand what your saying its not using the right one and some how Double is the key but I've tried putting Circle c = new Circle(double r); or double newradius and different things I think I just have the syntax wrong. Sorry...

  13. #13
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Need help getting variable from main class to another

    you are close!

    when you did Circle(double r), why did you add the double?

    you already defined r as a double using the scanner input? so after telling you that you dont need to say double r in the parameter.

    what should you do now?
    codyjava likes this.

  14. #14
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Need help getting variable from main class to another

    Wow okay got it I said Circle c = new Circle(r); and then changed Cricle(double newradius); to Circle(double r); along with radius = r; worked perfect thanks a ton you can help me on my other project too haha. I already posted. :P Thanks again.

  15. #15
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Need help getting variable from main class to another

    just so you know
    you could have kept it as
    Java Code:
    Circle(double newradius){
    newradius=radius; 
    }
    because you are calling this method and passing a double into, it will auto assign r as newradius.

Similar Threads

  1. Replies: 11
    Last Post: 10-30-2012, 09:28 AM
  2. Replies: 1
    Last Post: 06-27-2012, 02:55 PM
  3. Replies: 0
    Last Post: 10-23-2011, 10:49 PM
  4. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 08:30 AM
  5. Calling a variable from main to another class
    By itsme in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 03:35 PM

Posting Permissions

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