Results 1 to 9 of 9

Thread: Mutator methods

  1. #1
    toterpunkt is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default Mutator methods

    i have created this mutator in my class:
    Java Code:
        public void set(double newLength, int newVolume)
        {
            bottleLength = newLength;
            bottleVolume = newVolume;
        }
    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:
    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());
    
        }
    }
    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:

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Java Code:
            myWater.set(newLength, newVolume);
    -Gary-

  3. #3
    toterpunkt is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by gcalvin View Post
    Java Code:
            myWater.set(newLength, newVolume);
    -Gary-
    That throws me an error each time.

    Do i have to change something in my class first in order for that to work in my tester?

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Except:
    Java Code:
            double newLength = Integer.parseInt (input);
    Don't you mean
    Java Code:
    [COLOR="Blue"]        double newLength = Double.parseDouble(input);
    [/COLOR]
    ?

    -Gary-

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by toterpunkt View Post
    That throws me an error each time.

    Do i have to change something in my class first in order for that to work in my tester?
    Have you read the error message? Are you going to make us guess what it is? Are you claiming that you don't get the error when you run the code as you posted it?

    -Gary-

  6. #6
    toterpunkt is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by gcalvin View Post
    Have you read the error message? Are you going to make us guess what it is? Are you claiming that you don't get the error when you run the code as you posted it?

    -Gary-
    I'm an idiot. I keep forgetting how the slightest typo will ruin the outcome of everything. Thank you for catching that you are awesome!
    Last edited by toterpunkt; 02-24-2011 at 09:11 AM.

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by toterpunkt View Post
    How would i get the modified data to "print out" ?


    Java Code:
            System.out.println("\n\nModified Data:" + newLength + newVolume);
            myWater.set(newLength, newVolume);
    i know that isn't correct but am i even close?
    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-

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    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-

  9. #9
    toterpunkt is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by gcalvin View Post
    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-

    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)
    Java Code:
            System.out.println("Modified Data:");
            myWater.set(newLength, newVolume);
            System.out.println("length: " + newLength + "volume: " + newVolume);
    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("\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 09:20 AM.

Similar Threads

  1. Please help with mutator
    By ethemartian in forum New To Java
    Replies: 21
    Last Post: 02-06-2011, 02:57 AM
  2. Accessor & Mutator methods understanding..
    By 6thDAY in forum New To Java
    Replies: 3
    Last Post: 08-14-2010, 08:27 PM
  3. Accessor/Mutator Question
    By noble in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 05:21 AM
  4. Replies: 6
    Last Post: 12-03-2008, 12:15 AM
  5. mutator method
    By dirtycash in forum New To Java
    Replies: 7
    Last Post: 11-22-2007, 11:29 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
  •