Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Getting Two Classes to talk to each others objects

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getting Two Classes to talk to each others objects

    I need help with trying to talk with updateCustom() and updateStandard() in my TC.java file.

    I've tried calling in TA.java Tip calling = new Tip(); but I'm either getting one of the two errors cannot resolve method or symbol. How do I get this fixed?

    TA.java

     private TextWatcher billEditTextWatcher = new TextWatcher()
        {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                // convert billEditText's text to a double
                try
                {
                    currentBillTotal = Double.parseDouble(s.toString());
                }
                catch (NumberFormatException e)
                {
                    currentBillTotal = 0.0; // default if an exception occurs
                }
                // update the standard and custom tip EditTexts
                updateStandard(); // update the 10, 15 and 20% EditTexts
                updateCustom(); // update the custom tip EditTexts
            }

    TC.java

    class Tip
    {
    private void updateStandard()
        {
            // calculate bill total with a ten percent tip
            double tenPercentTip = currentBillTotal * .1;
            double tenPercentTotal = currentBillTotal + tenPercentTip;
     
            // set tipTenEditText's text to tenPercentTip
            tip10EditText.setText(String.format("%.02f", tenPercentTip));
     
            // set totalTenEditText's text to tenPercentTotal
            total10EditText.setText(String.format("%.02f", tenPercentTotal));
     
            // calculate bill total with a fifteen percent tip
            double fifteenPercentTip = currentBillTotal * .15;
            double fifteenPercentTotal = currentBillTotal + fifteenPercentTip;
     
            // set tipFifteenEditText's text to fifteenPercentTip
            tip15EditText.setText(String.format("%.02f", fifteenPercentTip));
     
            // set totalFifteenEditText's text to fifteenPercentTotal
            total15EditText.setText(String.format("%.02f", fifteenPercentTotal));
     
            // calculate bill total with a twenty percent tip
            double twentyPercentTip = currentBillTotal * .20;
            double twentyPercentTotal = currentBillTotal + twentyPercentTip;
     
            // set tipTwentyEditText's text to twentyPercentTip
            tip20EditText.setText(String.format("%.02f", twentyPercentTip));
     
            // set totalTwentyEditText's text to twentyPercentTotal
            total20EditText.setText(String.format("%.02f", twentyPercentTotal));
     
            // updates total from the total to what each person will pay
            totalPerDiner = totalDueWithTip / numberOfPeople;
        }
     
        // updates the custom tip and total EditTexts
        private void updateCustom()
        {
            // set customTipTextView's text to match the position of the SeekBar
            customTipTextView.setText(currentCustomPercent + "%");
     
            // calculate the custom tip amount
            double customTipAmount = currentBillTotal * currentCustomPercent * .01;
     
            // calculate the total bill, including the custom tip
            double customTotalAmount = currentBillTotal + customTipAmount;
     
            // display the tip and total bill amounts
            tipCustomEditText.setText(String.format("%.02f", customTipAmount));
            totalCustomEditText.setText(String.format("%.02f", customTotalAmount));
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Getting Two Classes to talk to each others objects

    You haven't explained your problem or asked your question (Is there a question?) well enough for us to help you. The code doesn't match the titles you've given it or the explanations. Post specifically what needs to be done, what you've tried, what happened - including the text of any errors, etc., and ask a specific question.

    To help you 'fix' something, we need to know what's broken, and you haven't shown that.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting Two Classes to talk to each others objects

    Quote Originally Posted by GregBrannon View Post
    You haven't explained your problem or asked your question (Is there a question?) well enough for us to help you. The code doesn't match the titles you've given it or the explanations. Post specifically what needs to be done, what you've tried, what happened - including the text of any errors, etc., and ask a specific question.

    To help you 'fix' something, we need to know what's broken, and you haven't shown that.
    I've done all that? lol

    But in case you need better info. I need the calculations part in its own file and class, I need help trying to get updateCustom (); and updateStandard(); in TA.java to send the users input to TC.java, so it can do its calculation and report back the results, but I don't know how to do so.

    I've tried setting up an object like tip callingTip = tip();

    CallingTip. UpdateCustom (); but either get cannot resolve symbol or method and sometimes it wont compile or do any calulations How do I go about passing my info over? I'm not really sure I understand passing things and using multiple files.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Getting Two Classes to talk to each others objects

    I'm sure it's clear to you but remember that we know nothing about your project other than what you tell us.

    However, in general terms:

    1. Pass a reference of one class' object to the other class' object through the constructor or setter from the first or a getter in the second,
    2. Use the reference of the first class' object to obtain data through getters in the first class.

    I created a simple demo to show how this can be done using one of the approaches described above:
    // a simple class to pass an instance of itself to a second class. the second
    // class will then send a value to the first
    public class TestClass 
    {
        SecondClass secondClass;
        int value;
     
        // default constructor
        public TestClass()
        {
            new SecondClass( this );
     
        } // end default constructor
     
        // method setValue() sets value to the argument int
        public void setValue( int value )
        {
            this.value = value;
     
        } // end method setValue()
     
        // method main() to 
        public static void main(String args[])
        {
            new TestClass();
     
        } // end method main()
     
    } // end class TestClass
     
    // accepts an reference to an instance of the first class and uses that
    // reference to pass a value back to the first
    class SecondClass
    {
        TestClass testClass;
     
        // a constructor that accepts a reference to the first class as
        // a parameter
        public SecondClass( TestClass testClass )
        {
            this.testClass = testClass;
     
            // sets the value of a field in the first class
            // ('this.' is unnecessary here in the constructor but is
            // used for clarity.)
            this.testClass.setValue( 5 );
     
        } // end constructor
     
    } // end class SecondClass

Similar Threads

  1. Objects in an Array/Classes Help
    By penguin0 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 29th, 2013, 10:45 PM
  2. Help with code using classes/objects/etc
    By missmay in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 22nd, 2013, 10:52 PM
  3. Objects and Classes
    By Worst Programmer Ever in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 4th, 2013, 03:13 PM
  4. Objects and Classes
    By Kristenw17 in forum Object Oriented Programming
    Replies: 1
    Last Post: April 23rd, 2013, 12:43 AM
  5. Understanding Classes and Objects
    By AustinStanley in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 9th, 2012, 11:35 AM