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 12 of 12

Thread: Passing Double Value from Class to Class

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Posts
    6
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Passing Double Value from Class to Class

    Hello Java Forums, I am currently starting a Java Course in school and was hoping to get a little help. I won't be one to post an entire assignment, I am simply looking to pass a double value from one class to another to separate the final outputs.

    Here is my first class

     
    package salesperson_part1;
     
    import java.io.IOException; // For BufferReader
    import java.io.InputStreamReader;  // For BufferReader
    import java.io.BufferedReader; // For BufferReader
     
     
    public class Salesperson_Part1 {
     
        public static void main(String[] args) { 
     
        int fixedSalary = 50000; // SalesPerson Fixed Salary
     
       System.out.println("Total Annual Compensation is an accumulation of your "
               + "fixed salary, $" + fixedSalary + ",\nplus a 15 percent commission "
                        + "on all sales for the year.");
     
         double salesAmount; //Amount sold for the year 
            System.out.print("\nWhat were your total sales amount for the year? "); 
            try { 
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
                String line = in.readLine().trim(); 
                if (line == null || line.length() == 0) { 
                    System.out.println("Canceled."); 
                    return; 
                } 
                salesAmount = Double.parseDouble(line); 
            } catch (NumberFormatException exception) { 
                System.out.println("Error: Not a valid sales amount."); 
                return; 
            } catch (IOException exception) { 
                System.out.println("Error: " + exception.getLocalizedMessage()); 
                return; 
            } 
        }
    }

    And here is the second:

    package salesperson_part1;
     
    class output {
     
         double commission = (salesAmount * 0.15);
            System.out.print("\nYour total commission is $" + commission);
     
            double AnnualCompensation = commission + 50000;
     
            System.out.print("\nYour total Anual Compensation is $" + 
                    AnnualCompensation + "\n\n");
     
    }

    So how can I get "SalesAmount" accessible in my second class in order to perform these operations upon it?

    Thank you so much, I am sure after I understand this first class passing method, it will facilitate it in the future for me

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    132
    My Mood
    Asleep
    Thanks
    4
    Thanked 9 Times in 9 Posts

    Default Re: Passing Double Value from Class to Class

    where would you call your second class ?
    Whatever you are, be a good one

  3. The Following User Says Thank You to John Joe For This Useful Post:

    Deployment (December 6th, 2017)

  4. #3
    Junior Member
    Join Date
    Dec 2017
    Posts
    6
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Passing Double Value from Class to Class

    Quote Originally Posted by John Joe View Post
    where would you call your second class ?
    Im not entirely sure, can you help?

  5. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    132
    My Mood
    Asleep
    Thanks
    4
    Thanked 9 Times in 9 Posts

    Default Re: Passing Double Value from Class to Class

    Sure. But at least I need to know at which part should the second class get called ? In first class ? Which line ? Is it after this line ?
    salesAmount = Double.parseDouble(line);
    Whatever you are, be a good one

  6. The Following User Says Thank You to John Joe For This Useful Post:

    Deployment (December 6th, 2017)

  7. #5
    Junior Member
    Join Date
    Dec 2017
    Posts
    6
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Passing Double Value from Class to Class

    Quote Originally Posted by John Joe View Post
    Sure. But at least I need to know at which part should the second class get called ? In first class ? Which line ?
    The second class should be called at the end of the first. Once the SalesAmount has been collected from the user

  8. #6
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    132
    My Mood
    Asleep
    Thanks
    4
    Thanked 9 Times in 9 Posts

    Default Re: Passing Double Value from Class to Class

    To call the second class's method, you have to create second class object in first class and invoke it.
    salesAmount = Double.parseDouble(line);
    output o = new output();
    o.sales(salesAmount);    //pass salesAmount to second class's method
    Create a sales method in second class
    public class output{
    	public void sales(double salesAmount) {
    		double commission = (salesAmount * 0.15);
    		System.out.print("\nYour total commission is $" + commission);
    		double AnnualCompensation = commission + 50000;
    		System.out.print("\nYour total Anual Compensation is $" + AnnualCompensation + "\n\n");
    	     }
    }
    Whatever you are, be a good one

  9. The Following User Says Thank You to John Joe For This Useful Post:

    Deployment (December 6th, 2017)

  10. #7
    Junior Member
    Join Date
    Dec 2017
    Posts
    6
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Passing Double Value from Class to Class

    Quote Originally Posted by John Joe View Post
    To call the second class's method, you have to create second class object in first class and invoke it.
    salesAmount = Double.parseDouble(line);
    output o = new output();
    o.sales(salesAmount);    //pass salesAmount to second class's method
    Create a sales method in second class
    public class output{
    	public void sales(double salesAmount) {
    		double commission = (salesAmount * 0.15);
    		System.out.print("\nYour total commission is $" + commission);
    		double AnnualCompensation = commission + 50000;
    		System.out.print("\nYour total Anual Compensation is $" + AnnualCompensation + "\n\n");
    	     }
    }
    Am I putting the "invoke" in the correct location?

    package salesperson_part1;
     
    import java.io.IOException; // For BufferReader
    import java.io.InputStreamReader;  // For BufferReader
    import java.io.BufferedReader; // For BufferReader
     
     
    public class Salesperson_Part1 {
     
        public static void main(String[] args) { 
     
        int fixedSalary = 50000; // SalesPerson Fixed Salary
     
        //Brief Description for User
       System.out.println("Total Annual Compensation is an accumulation of your "
               + "fixed salary, $" + fixedSalary + ",\nplus a 15 percent commission "
                        + "on all sales for the year.");
     
         double salesAmount; //Amount sold for the year 
     
         //Collect Sales Data
            System.out.print("\nWhat were your total sales amount for the year? ");
     
            //Checks for correct entry value
            try { 
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
                String line = in.readLine().trim(); 
                if (line == null || line.length() == 0) { 
                    System.out.println("Canceled."); 
                    return; 
                } 
                salesAmount = Double.parseDouble(line);  //Declar salesAmount
            } catch (NumberFormatException exception) { 
                System.out.println("Error: Not a valid sales amount."); 
                return; 
            } catch (IOException exception) { 
                System.out.println("Error: " + exception.getLocalizedMessage()); 
                return; 
            } 
     
            salesAmount = Double.parseDouble(line);
            output o = new output();
            o.sales(salesAmount);    //pass sales amount to second class's method
     
        }
     
    }

    I am getting a "cannot find symbol 'output'" error

  11. #8
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    132
    My Mood
    Asleep
    Thanks
    4
    Thanked 9 Times in 9 Posts

    Default Re: Passing Double Value from Class to Class

    No. Add these two lines
    output o = new output();
    o.sales(salesAmount);    //pass salesAmount to second class's method
    after this line
    salesAmount = Double.parseDouble(line);  //Declar salesAmount
    Whatever you are, be a good one

  12. The Following User Says Thank You to John Joe For This Useful Post:

    Deployment (December 6th, 2017)

  13. #9
    Junior Member
    Join Date
    Dec 2017
    Posts
    6
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Passing Double Value from Class to Class

    Quote Originally Posted by John Joe View Post
    No. Add these two lines
    output o = new output();
    o.sales(salesAmount);    //pass salesAmount to second class's method
    after this line
    salesAmount = Double.parseDouble(line);  //Declar salesAmount

    That works perfectly. Thank you for your help John!

    I plan to review this code and do a few more tests myself to make sure that I engrain this information into my head. Sorry if I seem like a noob now.

    Currently on week 2 of my 5 week Java crash course, should have some Intermediate knowledge within the next few weeks

    Thanks again,
    - Deployment

  14. The Following User Says Thank You to Deployment For This Useful Post:

    John Joe (December 6th, 2017)

  15. #10
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    132
    My Mood
    Asleep
    Thanks
    4
    Thanked 9 Times in 9 Posts

    Default Re: Passing Double Value from Class to Class

    Welcome. Enjoy coding
    Whatever you are, be a good one

  16. The Following User Says Thank You to John Joe For This Useful Post:

    Deployment (December 6th, 2017)

  17. #11
    Junior Member
    Join Date
    Dec 2017
    Posts
    6
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Passing Double Value from Class to Class

    Quote Originally Posted by John Joe View Post
    No. Add these two lines
    output o = new output();
    o.sales(salesAmount);    //pass salesAmount to second class's method
    after this line
    salesAmount = Double.parseDouble(line);  //Declar salesAmount
    So what does the "o" stand for in these commands?

    - Deployment

  18. #12
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    132
    My Mood
    Asleep
    Thanks
    4
    Thanked 9 Times in 9 Posts

    Default Re: Passing Double Value from Class to Class

    Quote Originally Posted by Deployment View Post
    So what does the "o" stand for in these commands?
    The o stands for the second class object .
    Whatever you are, be a good one

Similar Threads

  1. Passing variable from one class to another
    By kingsta in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 16th, 2013, 10:57 AM
  2. Please help with passing an array to a new class
    By axelrose in forum Object Oriented Programming
    Replies: 1
    Last Post: July 6th, 2012, 06:08 PM
  3. passing refeerence of class
    By jack_nutt in forum Object Oriented Programming
    Replies: 2
    Last Post: September 30th, 2011, 03:14 PM
  4. Passing a value to another class
    By Semple in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 05:49 PM
  5. Replies: 0
    Last Post: April 11th, 2010, 09:56 AM