Results 1 to 9 of 9
Thread: Mutator methods
- 02-24-2011, 07:22 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
Mutator methods
i have created this mutator in my class:
in my tester i am using JOptionPane to get user input and would like to have the user see a pop up that says "Enter new length" "enter new volume." how can i get those inputs transferred through so when the user inputs them it automatically sees it as part of the mutator?Java Code:public void set(double newLength, int newVolume) { bottleLength = newLength; bottleVolume = newVolume; }
i would like for whatever the user puts in for "enter new length" to be printed out in place of the myWater.set(9,16).... as it is now i have to manually go into my file and change (9,16) to change it all. :confused:Java Code:import javax.swing.JOptionPane; public class BottledWaterCalculatorTester { public static void main(String[] args) { String country; int population; int aroundEarth; double bottleLength; int bottleVolume; String input; country = JOptionPane.showInputDialog("Please enter your country: "); input = JOptionPane.showInputDialog("Enter population amount:"); population = Integer.parseInt (input); input = JOptionPane.showInputDialog("Number of circumfrences:"); aroundEarth = Integer.parseInt (input); input = JOptionPane.showInputDialog("Length of bottle:"); bottleLength = Double.parseDouble (input); input = JOptionPane.showInputDialog("Volume of bottle:"); bottleVolume = Integer.parseInt(input); BottledWaterCalculator myWater = new BottledWaterCalculator(country, population, aroundEarth, bottleLength, bottleVolume); System.out.println("Country: " + myWater.getCountry() + "\nPopulation: " + myWater.getPopulation() + "\nNumber of Circumferences: " + myWater.getAroundEarth() + "\nLength of bottle: " + myWater.getBottleLength() + "\nVolume of bottle: " + myWater.getBottleVolume() +"\n\nNumber of bottles used: " + myWater.bottlesUsed() +"\nWater consumed: " + myWater.waterConsumed()); [B] input = JOptionPane.showInputDialog("Enter New Length:"); double newLength = Integer.parseInt (input); input = JOptionPane.showInputDialog("Enter New volume:"); int newVolume = Integer.parseInt (input); System.out.println("\n\nModified Data:"); myWater.set(9,16);[/B] System.out.println("\n\n NEW Length of bottle: " + myWater.getBottleLength() + "\n NEW Volume of bottle: " + myWater.getBottleVolume() + "\n\n NEW Number of bottles used: " + myWater.bottlesUsed() +"\n NEW Water consumed: " + myWater.waterConsumed()); } }
- 02-24-2011, 07:51 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
-Gary-Java Code:myWater.set(newLength, newVolume);
- 02-24-2011, 07:53 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
- 02-24-2011, 07:53 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Except:
Don't you meanJava Code:double newLength = Integer.parseInt (input);
?Java Code:[COLOR="Blue"] double newLength = Double.parseDouble(input); [/COLOR]
-Gary-
- 02-24-2011, 07:56 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
- 02-24-2011, 08:08 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
- 02-24-2011, 08:14 AM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
The code you posted originally calls myWater.set(), and then has a long System.out.println() that calls various getter methods. That seems like the right thing to do. Doesn't it work? What does it do that you don't expect? What about the code you're posting now?
-Gary-
- 02-24-2011, 08:17 AM #8
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
I should amend that. When I said "the right thing" I should have said "a reasonable thing". Really, there's no reason to put all of that into one println() statement. Break it up into multiple println() statements.
-Gary-
- 02-24-2011, 08:17 AM #9
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
I figured it out after having just posted my question by doing the following: (before it was adding newLength and newVolume because of how i wrote it in)
But i really didn't need to do that since i had already made println statements for "NEW length of bottle" that would have done the same thing:Java Code:System.out.println("Modified Data:"); myWater.set(newLength, newVolume); System.out.println("length: " + newLength + "volume: " + newVolume);
Java Code:System.out.println("\n\nModified Data:"); myWater.set(newLength, newVolume); System.out.println("NEW Length of bottle: " + myWater.getBottleLength() + "NEW Volume of bottle: " + myWater.getBottleVolume() + "NEW Number of bottles used: " + myWater.bottlesUsed() +"NEW Water consumed: " + myWater.waterConsumed());Last edited by toterpunkt; 02-24-2011 at 08:20 AM.
Similar Threads
-
Please help with mutator
By ethemartian in forum New To JavaReplies: 21Last Post: 02-06-2011, 01:57 AM -
Accessor & Mutator methods understanding..
By 6thDAY in forum New To JavaReplies: 3Last Post: 08-14-2010, 07:27 PM -
Accessor/Mutator Question
By noble in forum New To JavaReplies: 4Last Post: 02-02-2010, 04:21 AM -
Trying to understand accessor and mutator methods
By DC200 in forum New To JavaReplies: 6Last Post: 12-02-2008, 11:15 PM -
mutator method
By dirtycash in forum New To JavaReplies: 7Last Post: 11-22-2007, 10:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks