Results 1 to 15 of 15
- 01-28-2013, 07:25 AM #1
Member
- Join Date
- Jan 2013
- Posts
- 7
- Rep Power
- 0
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()) ; } }
- 01-28-2013, 07:28 AM #2
Member
- Join Date
- Jan 2013
- Posts
- 7
- Rep Power
- 0
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???
- 01-28-2013, 07:47 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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):
kind regards,Java Code:Pair pair= new Pair(first, second); System.out.println("sum: "+pair.sum()); // make it do a calculation
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-29-2013, 06:06 AM #4
Member
- Join Date
- Jan 2013
- Posts
- 7
- Rep Power
- 0
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
- 01-29-2013, 09:51 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 01-29-2013, 09:45 PM #6
Member
- Join Date
- Jan 2013
- Posts
- 7
- Rep Power
- 0
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
- 01-29-2013, 11:11 PM #7
Member
- Join Date
- Dec 2012
- Posts
- 11
- Rep Power
- 0
Re: help passing user input to constructor parameters
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()) ;
- 01-30-2013, 09:33 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: help passing user input to constructor parameters
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.
- 01-30-2013, 12:06 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: help passing user input to constructor parameters
I wonder if my reply #3 was written in invisible ink again ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-30-2013, 12:29 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: help passing user input to constructor parameters
Who said that?
Please do not ask for code as refusal often offends.
- 01-30-2013, 12:57 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 02-02-2013, 02:49 AM #12
Member
- Join Date
- Jan 2013
- Posts
- 7
- Rep Power
- 0
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?????
- 02-02-2013, 10:39 AM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-02-2013, 05:03 PM #14
Member
- Join Date
- Jan 2013
- Posts
- 7
- Rep Power
- 0
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
- 02-02-2013, 05:19 PM #15
Member
- Join Date
- Jan 2013
- Posts
- 7
- Rep Power
- 0
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 05:29 PM. Reason: got it to work
Similar Threads
-
Creating an object based on user input parameters
By monkeyhead in forum New To JavaReplies: 5Last Post: 11-14-2011, 11:59 PM -
Adding Data from user input to ArrayList and creating constructor
By Renxx in forum New To JavaReplies: 0Last Post: 11-03-2011, 07:33 AM -
Help - passing user input
By jonytek in forum New To JavaReplies: 4Last Post: 02-15-2011, 06:02 AM -
Passing Parameters to servlet
By praveen_1987 in forum JavaServer Faces (JSF)Replies: 0Last Post: 06-30-2010, 11:30 AM -
passing parameters between two applets
By veena1612 in forum Java AppletsReplies: 1Last Post: 05-27-2008, 09:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks