Results 1 to 15 of 15
  1. #1
    foxity.cf is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default help passing user input to constructor parameters

    I have to get two numbers as user input and then run multiple calculations on them. Everything worked fine when I supplied the parameters myself by creating a new object, but now when trying to use input, I'm not exactly sure how to get the input to the constructor parameters where I need them?

    Java Code:
    /** 
     * a class that computes multiple values using a pair of numbers 
     */ 
    
    public class Pair
    {
    	private static double firstNum; 
    	private static double secondNum; 
    	
    /**
     * constructor to create a pair of numbers  	
     * @param first is value of first number of pair 
     * @param second is value of second number of pair
     */
    	
    	public Pair (double first, double second) 
    	{
    		firstNum = first; 
    		secondNum = second; 
    	}
    	
    /**
     * calculates the sum of the pair of numbers 	
     * @return the sum of the two numbers of the pair 
     */
    	public static double getSum()
    	{
    		return firstNum + secondNum; 
    	}
    
    /**
     * calculates the difference of the pair of numbers	
     * @return the difference of the two numbers of the pair
     */
    	public static double getDifference()
    	{
    		return firstNum - secondNum; 
    	}
    	
    /**
     * calculates the product of the pair of numbers	
     * @return the product of the two numbers of the pair
     */
    	
    	public static double getProduct()
    	{
    		return firstNum * secondNum; 
    	}
    	
    /**
     * calculates the average of the pair of numbers 	
     * @return the average of the two numbers of the pair
     */
    	
    	public static double getAverage()
    	{
    		return (firstNum + secondNum) /2; 
    	}
    	
    /**
     * calculates the distance (absolute value of the difference) of the pair of numbers 
     * @return the distance of the two numbers of the pair
     */
    	
    	public static double getDistance()
    	{
    		return Math.abs(firstNum - secondNum); 
    	}
    	
    /**
     * calculates the maximum number out of the two	numbers
     * @return the maximum number out of the pair 
     */
    	
    	public static double getMax()
    	{
    		return Math.max(firstNum,  secondNum); 
    	}
    	
    /**
     * calculates the minimum number of the two numbers	
     * @return the minimum number out of the pair
     */
    	
    	public static double getMin()
    	{
    		return Math.min(firstNum,  secondNum); 
    	}
    		
    	
    }



    I have to create a tester class which is where I place the main method which gets the user input




    Java Code:
    import java.util.Scanner;
    
    
    public class PairTester 
    {
      public static void main (String [] args) 
      {
    	 
    	  
    	  Scanner in = new Scanner(System.in) ;
    	  
    	  System.out.println("Enter a number: "); 
    	  double first = in.nextDouble();
    	  
    	  System.out.println("Enter a second number: "); 
    	  double second = in.nextDouble(); 
    	  
    	  
    	  System.out.println(Pair.getSum()) ;
    	  System.out.println(Pair.getDifference()) ;
    	  System.out.println(Pair.getProduct()) ;
    	  System.out.println(Pair.getDistance()); 
    	  System.out.println(Pair.getAverage()) ;
    	  System.out.println(Pair.getMax()) ;
    	  System.out.println(Pair.getMin()) ;
    	
    	  
    	  
    	  
    	  
      }
    }

  2. #2
    foxity.cf is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default Re: help passing user input to constructor parameters

    I know I have done this before where I placed a method getUserInput and then put the scanner code in that method. Then I called it in the main method and worked fine. Then I was able to avoid using static for the variables and the methods, but I don't have my computer with me with all my previous code assignments, and i forget how I did it. Any help???

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default Re: help passing user input to constructor parameters

    You already have two double type numbers (you got it from the user); nothing keeps you from constructing a Pair object from them (and forget about all that static stuff):

    Java Code:
    Pair pair= new Pair(first, second);
    System.out.println("sum: "+pair.sum()); // make it do a calculation
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    foxity.cf is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default Re: help passing user input to constructor parameters

    thanks jas, the reason I made them static is cause the compiler kept throwing errors at me that they needed to be static. Thanks for your help

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: help passing user input to constructor parameters

    The compiler is not very bright.
    It can tell you something is wrong, but it can only make a guess at a solution.
    That guess (especially for basic things) is quite often incorrect, as it is often a quick fix simply to get the thing to compile.
    Do not rely on the compiler to tell you how to fix your sompilation problems.

    Making your code 'static' is generally not a good idea.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    foxity.cf is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default Re: help passing user input to constructor parameters

    yeah, you're right I should have moved getting the users inout into a method in the pair class so I didn't have to make everything static. I just forgot how I did that last time, I got my computer back now though

  7. #7
    Nouish is offline Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0

    Default Re: help passing user input to constructor parameters

    Quote Originally Posted by foxity.cf View Post
    thanks jas, the reason I made them static is cause the compiler kept throwing errors at me that they needed to be static. Thanks for your help
    You have no field "Pair" so this references the methods statically:

    Java Code:
    System.out.println(Pair.getSum()) ;
    System.out.println(Pair.getDifference()) ;
    System.out.println(Pair.getProduct()) ;
    System.out.println(Pair.getDistance()); 
    System.out.println(Pair.getAverage()) ;
    System.out.println(Pair.getMax()) ;
    System.out.println(Pair.getMin()) ;

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: help passing user input to constructor parameters

    Quote Originally Posted by foxity.cf View Post
    yeah, you're right I should have moved getting the users inout into a method in the pair class so I didn't have to make everything static.
    Well, not really.
    A normal design would separate the IO from the model.
    Your Pair class is your model. It shouldn't care what interaction with the outside world there is.
    Read in the doubles as you are at the moment (in the main() method), then create a Pair object (Pair p = new Pair()) and either pass the doubles in as long as there is a constructor that accepts them, or use setters to set the two values.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default Re: help passing user input to constructor parameters

    I wonder if my reply #3 was written in invisible ink again ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: help passing user input to constructor parameters

    Who said that?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default Re: help passing user input to constructor parameters

    Quote Originally Posted by Tolls View Post
    Who said that?
    Yoohoo! I'm here! Look overhere!

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    foxity.cf is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default Re: help passing user input to constructor parameters

    I have not created a pair object because I'm not sure how to do this while enabling the users input to be used in the methods. I'm very new to java, about 3-4 weeks, so I'm still learning how everything works. If I create a pair object, would I somehow pass the users input into the object, or to the methods?????

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default Re: help passing user input to constructor parameters

    See my reply #3; it tells you how to create a Pair object (you already have a suitable constructor; that's how you pass those two numbers to it) and how to make it do the wanted calculations.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    foxity.cf is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default Re: help passing user input to constructor parameters

    Ok Jos, Thanks, I will try it that way and see how it works. Thanks a bunch for your time

  15. #15
    foxity.cf is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default Re: help passing user input to constructor parameters

    Thanks a bunch Jos, I used your example and made it work. I appreciate everyones help. Still new to this but this forum definitely helps to get a better understanding by getting recommendations from people who know what they're doing in the coding area!!

    ;-) Thanks again to Tolls too, and all.
    Last edited by foxity.cf; 02-02-2013 at 06:29 PM. Reason: got it to work

Similar Threads

  1. Creating an object based on user input parameters
    By monkeyhead in forum New To Java
    Replies: 5
    Last Post: 11-15-2011, 12:59 AM
  2. Replies: 0
    Last Post: 11-03-2011, 08:33 AM
  3. Help - passing user input
    By jonytek in forum New To Java
    Replies: 4
    Last Post: 02-15-2011, 07:02 AM
  4. Passing Parameters to servlet
    By praveen_1987 in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 06-30-2010, 12:30 PM
  5. passing parameters between two applets
    By veena1612 in forum Java Applets
    Replies: 1
    Last Post: 05-27-2008, 10:29 AM

Posting Permissions

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